
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2016 08:57 PM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-08-2016 02:20 AM
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'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-08-2016 06:32 AM
Thanks Ram,
I am getting closer now.
Two questions
1) Why did we commented out below
- /*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;*/
2) I didn't understand this
- return '{\"categories\":[{\"Name":\"John\", \"Value\":\"Doe\"}, {\"Name":\"Anna\", \"Value\":\"Smith\"},{\"Name":\"Peter\", \"Value\":\"Jones\"}]}';
The response what i got is like this.
{"categoriess":[{"code":"AA","cages":[{"code":"AA:0A:00EQ1","accounts":[{"accountName":"Communication Services","lock":false,"accountNumber":"1812"}],"cabinets":[{"code":"0001"},{"code":"0002"},{"code":"0003"}]},{"code":"AA:0A:00EQ20","accounts":[{"accountName":"Communication Services","lock":false,"accountNumber":"1812"}],"cabinets":[{"code":"0004"},{"code":"0005"},{"code":"0006"}]}]},{"code":"AM","cages":[{"code":"AM:0A:00EQ10","accounts":[{"accountName":"Customer Services","lock":false,"accountNumber":"1812"}],"cabinets":[{"code":"0007"},{"code":"0008"},{"code":"0009"}]},
Thanks for your effort in helping me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-08-2016 07:12 AM
Hi Raghu,
I tested this code, before putting it here. As i don't have a ready-made web service available, which produces JSON, I commented those lines and instead of that, put sample JSON.
Once you get the JSON from web-service and passed to client, you need to convert this json into object and needs to use this. You can check validity of your JSON in below link.
As per that, it has 2 categories with codes AA and AM. Do you want to add these options to your dropdownlist? if so, you can write code like below(Assuming you already written client script as I showed in above post).
- var objJSON = JSON.parse(answer);
- for(var loop = 0 ; loop < objJSON.categoriess.length;loop++)
- {
- alert(objJSON.categories[loop].Name);
- g_form.addOption('u_category',objJSON.categoriess[loop].code,objJSON.categories[loop].code);
- }

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2016 06:07 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2016 09:21 PM
Once you are able to test your REST message, then you'll want to construct the ServiceNow script around it. For example, a business rule to fetch data from your remote service when a record reaches a certain state, or a UI action that someone can click a button and retrieve the information. That's going to be part of the
Outbound REST Web Service - ServiceNow Wiki
ServiceNow Developers - search for "scripting outbound REST"

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2016 09:26 PM
Chuck,
I want to populate the data in above mentioned drop down's on loading the form.
Not when it reaches certain state. That is the main issue.