
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-23-2017 12:12 PM
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?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-27-2017 07:21 AM
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);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-23-2017 12:33 PM
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:
-Rob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-23-2017 05:22 PM
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:
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-27-2017 07:21 AM
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);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-27-2017 02:43 PM
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.