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

Not a problem. Create a BEFORE business rule that fetches the data on insert only.



The links provided should help get you started. Here are a couple more to help you get started with business rules:


Business Rules - ServiceNow Wiki


Business Rules Best Practices - ServiceNow Wiki  


chcuk,



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();


}




}