API to get SCTASK variables

Hari1
Mega Sage

Hi,

I have a requirement to get SCTASK catalog variables. Can this be achieved?

 

1 ACCEPTED SOLUTION

Hi Ankur,

Was able to achieve this by using the below API.

https:<instance url>/api/now/table/sc_task?sysparm_query=number=<sc_task number>&sysparm_display_val...

We can directly add the variables.<variablename> in the link.

View solution in original post

7 REPLIES 7

I am using an API GET method to get an order from the sc_task table in another SN instance.  I need to populate the fields on my form in my instance with fields and variables from the sc_task form in the other instance.  I am using a scheduled job that calls the GET method to make the API call using the endpoint that points to all the fields AND variables from the sc_task form in the other instance.  My code is below, I am able to get the REQ, RITM, and TASK number.  However, I still need to get the variables and populate them on my form.  I am able to see the variables in the results from the Test Get when I run it so the variables are coming over and they are coming over as variables.city but I just can't get them to populate the fields on my form.  I have tried using:

gr.setValue('city',getData[i].variables.city);

and

gr.city = getData[i].variables.city;

I have even tried getData[i].request_item.city but that doesn't work either.  I will create a new thread but just wanted to reply here and thank you for answering.

 

try {
  var r = new sn_ws.RESTMessageV2('x_amspe_fulfill.DaaS_Test', 'Default GET');
  var response = r.execute();
  var responseBody = JSON.parse(response.getBody());
  var httpStatus = response.getStatusCode();
  var getData = responseBody.result;
  // gs.info("DaaS order Data: "+ getData.length+" @@ "+JSON.stringify(getData));
  // for loop to get the data in the array
  for (var i = 0; i < getData.length; i++) {
  // gs.info("DaaS order Data_1: "+getData[i].number+" @@ "+getData[i].state);
  var gr = new GlideRecord("x_amspe_fulfill_daas_fulfillment");
  gr.initialize();
    gr.client_ticket_number = getData[i].request.display_value; //REQ
    gr.client_item_number = getData[i].request_item.display_value; //RITM
    gr.client_task = getData[i].number; //SCTASK
    gr.setValue('city',getData[i].request_item.variables.city); // = getData[i].variables.city;
    gs.info('ADDRESS CITY ' + getData[i].request_item.variables.city);
    gr.request_type = 'new_device';
    gr.insert();
}

} catch (ex) {
var message = ex.message;
}

You need to parse the json response you got and then get the values

Example below

try {
    var r = new sn_ws.RESTMessageV2('x_amspe_fulfill.DaaS_Test', 'Default GET');
    var response = r.execute();
    var responseBody = JSON.parse(response.getBody());
    var httpStatus = response.getStatusCode();
    var getData = responseBody.result;
    // gs.info("DaaS order Data: "+ getData.length+" @@ "+JSON.stringify(getData));
    // for loop to get the data in the array
    for (var i = 0; i < getData.length; i++) {
        // gs.info("DaaS order Data_1: "+getData[i].number+" @@ "+getData[i].state);
        var gr = new GlideRecord("x_amspe_fulfill_daas_fulfillment");
        gr.initialize();
        gr.client_ticket_number = getData[i].request.display_value; //REQ
        gr.client_item_number = getData[i].request_item.display_value; //RITM
        gr.client_task = getData[i].number; //SCTASK
        gr.setValue('city',getData[i].request_item.variables.city); // = getData[i].variables.city;
        gs.info('ADDRESS CITY ' + getData[i].request_item['variables.city']);
        gr.request_type = 'new_device';
        gr.insert();
    }

} catch (ex) {
    var message = ex.message;
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader