Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Create Client script from script include and rest API

arpita roy
Tera Contributor

Hi All,

 

Use case - Populate the prediction result (assignment group) in field message when short description is entered before submitting the form.

the approach is i am trying to get the prediction from a PI model in rest api. then create a script include for rest api to make it client callable and then call the function in client script.

 

So far Rest API and Script include i have created. i need help to move forward with client script and please let me know if i have made any mistakes-

 

Rest API-

 

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

    // implement resource here
    var QueryParams = request.queryParams;
    var shortdesc = QueryParams.short_description.toString();
    var desc = QueryParams.description.toString();
    var mlsolutionName = 'ml_XYZ';
    var mlSolution = sn_ml.ClassificationSolutionStore.get(mlsolutionName);
    var options = {};
    options.top_n = 1;
    options.apply_threshold=false;
    var inp = [{short_description: shortdesc,
                description: desc,
                }];
    var result = mlSolution.getLatestVersion().predict(inp,options);
    response.setStatus(200);
    var re = JSON.parse(result);
    var rs = re['1'][0]['predictedValue'];
    response.setBody({
    assignment_group : rs,
    confidence : re['1'][0]['confidence'],
    predictedsysid : re['1'][0]['predictedSysId']
    });

})(request, response);

 

Script Include-

 

var getAPIurl = Class.create();
getAPIurl.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    type: 'getAPIurl',

    get_api_url: function(){
             var api = '/api/itmhe/predict_ag';
             var instUrl = gs.getProperty('glide.servlet.uri');
             var Final_api = instUrl + api;
             return Final_api;
},

});
 
Thanks in Advance.

 

 

 

0 REPLIES 0