fetching data from rest API

Raghu Loganatha
Kilo Guru

Hi ,

We have started to work on integrations and REST API's recently.
We are totally new and stuck , we could use some help.

There are couple fields in my record as below.

1) u_task

2) u_category

3) u_code

all the above are drop down.

We have a REST API which i added to rest message record and we are able to get the response.
We need to fetch that data and load the form with the values from it.

Can any one provide some help with the script what i can use?

Your help is much appreciated.


Thanks,
Raghu.

1 ACCEPTED SOLUTION

Assuming that you are using SNOW default forms and using client script, you are trying to add "options" to dropdowns, here is an example, which will add options to a control "u_category".



Note: I am assuming the response returned by your API/Web service is already in JSON format. If not, Please copy that response here. I put few alert statements for debugging. Remove them later, once you get it working.



ClientScript:



function onLoad() {


    //Type appropriate comment here, and begin script below


var ga = new GlideAjax('Test');


ga.addParam('sysparm_name','getCategories');


ga.getXML(getCategoriesOutput);


}


function getCategoriesOutput(response) {


    var answer = response.responseXML.documentElement.getAttribute("answer");


  alert(answer);


    var objJSON = JSON.parse(answer);


  for(var loop = 0 ; loop < objJSON.categories.length;loop++)


  {


  alert(objJSON.categories[loop].Name);


  g_form.addOption('u_category',objJSON.categories[loop].Value,objJSON.categories[loop].Name);


  }


}



Server-side script include



var Test = Class.create();


Test.prototype = Object.extendsObject(AbstractAjaxProcessor, {


getCategories: function()


  {


  try {


  /*var r = new sn_ws.RESTMessageV2('x_equi2_trouble_ti.Trouble Ticket Post', 'post');


  var response = r.execute();


  var responseBody = response.getBody();


  var httpStatus = response.getStatusCode();


  return responseBody;*/


  return '{\"categories\":[{\"Name":\"John\", \"Value\":\"Doe\"}, {\"Name":\"Anna\", \"Value\":\"Smith\"},{\"Name":\"Peter\", \"Value\":\"Jones\"}]}';


  }


  catch(ex) {


  var message = ex.getMessage();


  return "";


  }


  },


      type: 'Test'


});


View solution in original post

16 REPLIES 16

Hi Raghu,



Here are your steps.



1. Create a script include and inside that create a function, which will call the API of other application. Once you get response, you need to parse it to JSON and pass it to client.


1. Create a client script for that table -> Onload event -> In the script, use "GlideAjax" to call function created in above step. Once you receive response from server, you will parse it and add options to "dropdown".



GlideAjax - ServiceNow Wiki



Syntax for calling another web-service:



  var finalHost = "yoururl";


  var r = new sn_ws.RESTMessageV2();


  r.setHttpMethod("get");


  r.setEndpoint(finalHost);


  //r.setQueryParameter("locatenow", "true");


r.setBasicAuth(username,password);


  var response = r.execute();


  var responseBody = response.getBody();


// If response is in json format, directly you can send to client.


return responseBody;


If you response is not in JSON, you need to convert the response into JSON.



At client side, you can parse JSON string to object by



var newoptions = JSON.parse(jsonstring);


for(var loop = 0; loop < newoptions.length; loop++)


{


// add to dropdown
}


Ram,




I have this business rule, How can i parse the data and show it in drop down??




function onBefore(current, previous) {


 


try {


var r = new sn_ws.RESTMessageV2('x_equi2_trouble_ti.Trouble Ticket Post', 'post');


var response = r.execute();


var responseBody = response.getBody();


var httpStatus = response.getStatusCode();


}


catch(ex) {


var message = ex.getMessage();


}




}


In Business rules, I am not sure, whether we can access controls, as you want to add "options" to that dropdown. I will wait till chuck responds. If you use "client script" instead of business rule as I described in my above post, Once you get the response, if its already in JSON format, you can directly pass to client by "return responseBody". At client-side i will parse like below.



var newoptions = JSON.parse(jsonstring);


for(var loop = 0; loop < newoptions.length; loop++)


{


        //g_form.addOption() method to add option to dropdown (Or) you can use javascript methods to add options to list.


}


GlideForm (g form) - ServiceNow Wiki




If you are not aware of how to call "Server-side" function form client-side, check this below URL.



GlideAjax - ServiceNow Wiki


Ram,



I trying to use script include and glideajax client script's.
Can you provide me withe the scripts for my requirement?
I am struggling to get this done.


Assuming that you are using SNOW default forms and using client script, you are trying to add "options" to dropdowns, here is an example, which will add options to a control "u_category".



Note: I am assuming the response returned by your API/Web service is already in JSON format. If not, Please copy that response here. I put few alert statements for debugging. Remove them later, once you get it working.



ClientScript:



function onLoad() {


    //Type appropriate comment here, and begin script below


var ga = new GlideAjax('Test');


ga.addParam('sysparm_name','getCategories');


ga.getXML(getCategoriesOutput);


}


function getCategoriesOutput(response) {


    var answer = response.responseXML.documentElement.getAttribute("answer");


  alert(answer);


    var objJSON = JSON.parse(answer);


  for(var loop = 0 ; loop < objJSON.categories.length;loop++)


  {


  alert(objJSON.categories[loop].Name);


  g_form.addOption('u_category',objJSON.categories[loop].Value,objJSON.categories[loop].Name);


  }


}



Server-side script include



var Test = Class.create();


Test.prototype = Object.extendsObject(AbstractAjaxProcessor, {


getCategories: function()


  {


  try {


  /*var r = new sn_ws.RESTMessageV2('x_equi2_trouble_ti.Trouble Ticket Post', 'post');


  var response = r.execute();


  var responseBody = response.getBody();


  var httpStatus = response.getStatusCode();


  return responseBody;*/


  return '{\"categories\":[{\"Name":\"John\", \"Value\":\"Doe\"}, {\"Name":\"Anna\", \"Value\":\"Smith\"},{\"Name":\"Peter\", \"Value\":\"Jones\"}]}';


  }


  catch(ex) {


  var message = ex.getMessage();


  return "";


  }


  },


      type: 'Test'


});