Is it possible to call a scripted REST API from a client script?

William Busby
Tera Guru

We've upgraded to Istanbul and are beginning the process of deploying scripted REST APIs for external integrations. At the same time we'd like to leverage the same calls from certain client scripts but I'm unable to find any information about how this might be done. Any help?

1 ACCEPTED SOLUTION

williambusby here are code examples from my getReferenceAdvanced solution.   Setup your scripted rest to provide a JSON string response, I find that easier to deal with especially with multiple values.



Establish an object (key and value pair) and put data into it:


var answerList = {};


answerList.key = value;



Then at the very end of your script encode this object into a JSON string to pass back to the client script:


var answer = new JSON().encode(answerList);


return answer.toString();



Once that is setup then here is an example client script that calls the "getreferenceadvanced" scripted REST.   Notice that it parses the JSON string to create an object that you can then leverage in your code like setValue() functions.   As noted in the comments you can also pass the session token for authentication so you don't have to pass in credentials for your REST service.


var fieldValue = g_form.getValue(fieldName);



//Call the getreferenceadvanced scripted REST web service to get the data requested


var restEndPoint = "api/snc/getreferenceadvanced/";


restEndPoint = restEndPoint + fieldValue;


var serverRequest = new XMLHttpRequest();


serverRequest.open("get", restEndPoint, false);



//The getreferenceadvanced scripted REST web service requires authentication so pass user session token


serverRequest.setRequestHeader("X-UserToken", g_ck);


serverRequest.setRequestHeader("Accept", "application/json");


serverRequest.setRequestHeader("Content-Type", "application/json");


serverRequest.send();


if (serverRequest.status === 200) {


      var serverResponse = JSON.parse(serverRequest.response);


      var refResponse = JSON.parse(serverResponse.result);


     


      g_form.setValue("FIELD-NAME", refResponse.key);  


}


View solution in original post

7 REPLIES 7

robpickering
ServiceNow Employee
ServiceNow Employee

Technically, no you cannot, because Client Scripts are evaluated in the client's browser, so they would actually be making the REST call, which probably isn't what you want.



However, you can certainly build a Script Include which the Client Script will call, which would have the Script Include itself calling the REST API and then passing the results back down to the Client.



You would do this through a "combination client script" leveraging GlideAjax which includes both Client executable code and server exectuable code.   You can read more here:


Client scripts good practices



-Rob


Michael Ritchie
ServiceNow Employee
ServiceNow Employee

This is absolutely possible.   This is how AngularJs works with client performing Rest calls. replying remotely but here is a quick post on the concept:


Session IDs and Angular apps



My getReferenceAdvanced solution utilizes a scripted rest call from a client script via UI script. I can provide more details if needed next week.


getReferenceAdvanced, g_form.getReference and GlideAjax Alternatives


williambusby here are code examples from my getReferenceAdvanced solution.   Setup your scripted rest to provide a JSON string response, I find that easier to deal with especially with multiple values.



Establish an object (key and value pair) and put data into it:


var answerList = {};


answerList.key = value;



Then at the very end of your script encode this object into a JSON string to pass back to the client script:


var answer = new JSON().encode(answerList);


return answer.toString();



Once that is setup then here is an example client script that calls the "getreferenceadvanced" scripted REST.   Notice that it parses the JSON string to create an object that you can then leverage in your code like setValue() functions.   As noted in the comments you can also pass the session token for authentication so you don't have to pass in credentials for your REST service.


var fieldValue = g_form.getValue(fieldName);



//Call the getreferenceadvanced scripted REST web service to get the data requested


var restEndPoint = "api/snc/getreferenceadvanced/";


restEndPoint = restEndPoint + fieldValue;


var serverRequest = new XMLHttpRequest();


serverRequest.open("get", restEndPoint, false);



//The getreferenceadvanced scripted REST web service requires authentication so pass user session token


serverRequest.setRequestHeader("X-UserToken", g_ck);


serverRequest.setRequestHeader("Accept", "application/json");


serverRequest.setRequestHeader("Content-Type", "application/json");


serverRequest.send();


if (serverRequest.status === 200) {


      var serverResponse = JSON.parse(serverRequest.response);


      var refResponse = JSON.parse(serverResponse.result);


     


      g_form.setValue("FIELD-NAME", refResponse.key);  


}


Thank you very much, looks promising. Will try to set this up later this week. I just assumed it was possible but had been unable to find any examples.