Skip to main content

Scripted REST API Examples

 Scripted REST API Examples

 POST example 1:

 

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

   

    // implement resource here

    //-- see:

    //--   https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/app-store/dev_portal/API_reference/ScriptableServiceRequestBody/reference/r_SSRB_data.html

    //var queryParams = request.queryParams;

    //var pathParams = request.pathParams;

    //gs.log('PATH params: ' + path;

    //gs.log('PATH param2: ' + path.description, 'wsRESTtest')

   

    var Reqbody = request.body;

    //var test_resp = pathParams.RequestDetailsJson;

    var sName='';

    var sID='';

    var arrYesNo=false;

    var requestData = Reqbody.data;

    if (requestData instanceof Array) {

        sName = requestData[0].name; // 'user1'

        sID = requestData[0].id; // '1234'

        arrYesNo=true;

    } else {

        sName = requestData.name; // 'user1'

        sID=requestData.id;

    

    }

   

    response.setContentType('application/json');

    response.setStatus(200);

    var writer = response.getStreamWriter();

    writer.writeString('sName:sID ' + sName + ',' + sID + ',arrYesNo:' + arrYesNo);

    //writer.writeString('resp: ');

   

})(request, response);

 

 POST example 2 - using query parameters:

 

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

   

    // implement resource here

    //-- see:

    //--   https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/app-store/dev_portal/API_reference/ScriptableServiceRequestBody/reference/r_SSRB_data.html

   

        response.setContentType('application/json');

        response.setStatus(200);

    var queryParams = request.queryParams;

var sparamtest = queryParams.TestVar1;

   

        var writer = response.getStreamWriter();

       

        writer.writeString('resp: OK: ' + sparamtest);

   

       

    })(request, response);

 

 

Comments

  1. The example provides a practical look at processing request data in a ServiceNow scripted REST API. The code demonstrates how request.body.data can be handled when the incoming payload may contain either a single object or an array, with instanceof Array used to determine the structure before extracting values such as name and id.

    The response-handling portion also shows how the REST response can be configured with an application/json content type, an HTTP 200 status, and a stream writer for returning output. This combination gives developers a useful practical example when working with RESTful API Training and understanding request-response processing.

    ReplyDelete
  2. The example is also relevant to full-stack development because REST APIs commonly connect frontend applications with backend services. Understanding how request payloads are structured and processed provides a useful foundation for learners progressing through JavaScript Full Stack Course.

    ReplyDelete
  3. The handling of JSON-style request data can also be connected to applications built using the MEAN stack, where client-side JavaScript communicates with backend services through APIs. These concepts provide practical context for MEAN Stack Developer Course.

    ReplyDelete

Post a Comment

Popular posts from this blog

URL link in addInfoMessage

var ga=new GlideAjax('gld_HR_ajax'); ga.addParam('sysparm_name', 'checkEmployeeNumber_hrProfile'); ga.addParam('sysparm_hrprofilenumber', g_form.getValue('number')); ga.addParam('sysparm_employeenumber', newValue); ga.getXMLAnswer(function(answer) { if (answer!='undefined' && answer!=''){ var navURL="<a style='text-decoration:underline;color:blue' href=hr_profile.do?sysparm_query=number=" + answer + ">" + answer + "</a><img width='3' src='images/s.gif'/>"; var sMsg='The employee number entered already exists on another HR Profile ' + navURL; //alert(sMsg); g_form.showErrorBox('employee_number', 'error - please check'); g_form.addInfoMessage(sMsg); } });

ServiceNow check for null or nil or empty (or not)

Haven't tested these all recently within global/local scopes, so feel free to have a play! option 1 use an encoded query embedded in the GlideRecord , e.g.  var grProf = new GlideRecord ( 'x_cls_clear_skye_i_profile' ); grProf . addQuery ( 'status=1^ owner=NULL ' ); grProf . query (); even better use the glideRecord  addNotNullQuery or addNullQuery option 2 JSUtil.nil / notNil (this might be the most powerful. See this link ) example: if ( current . operation () == 'insert' && JSUtil . notNil ( current . parent ) && ! current . work_effort . nil ())  option 3 there might be times when you need to get inside the GlideRecord and perform the check there, for example if the code goes down 2 optional routes depending on null / not null can use gs.nil : var grAppr = new GlideRecord ( 'sysapproval_approver' ); var grUser = new GlideRecord ( 'sys_user' ); if ( grUser . get ( 'sys_id' , current . approver )){...