Script Help - get response from REST, parse it and display the details on the catalog item
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2023 10:39 AM - edited 10-12-2023 12:55 PM
Hi All,
I have a flow action created where I am getting response from 3rd party tool. I am parsing that response and returning the values of the response in an array.
Step 1: Getting response from REST API in step 1 and in step 2 parsing the response.
Step 2: Wrote a client callable script include to pass the resources ID values into an array:
var STDMCatalogAPIUTILS = Class.create();
STDMCatalogAPIUTILS.prototype = {
initialize: function() {},
STDMGetCatalogAPIDetailsUseCase1: function(response_body) {
var responsebody = response_body;
var ResourceArrSameHash = [];
for (var i = 0; i < responsebody.data.length; i++) {
var ResourceID = responsebody.data[i].id;
var HashID = responsebody.data[i].hash;
var MaxDuration = responsebody.data[i].maxDuration;
//Trying Use case 1: Resource ID exists in ServiceNow and hash ID is SAME (in this case, resources needs to be bundled into an array object to be displayed back to the user on the catalog:)
var gr = new GlideRecord('x_stdm_access_mgmt_resources');
gr.addQuery('resource_id', ResourceID);
gr.addQuery('hash', HashID);
gr.query();
if (gr.next()) {
ResourceArrSameHash.push(ResourceID);
}
}
gs.info("priniting UseCase1 " + ResourceArrSameHash.join(','));
return ResourceArrSameHash.join(',');
},
Step 3: I want to display these Resource ID values to the user on the catalog form. The below variable is refernce variable referencing to resources custom table.
I wrote an On change client script but it is returning answer as null:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (newValue != '') {
//Type appropriate comment here, and begin script below
var ga = new GlideAjax('STDMCatalogAPIUTILS');
alert("req ID");
ga.addParam('sysparm_name', 'STDMGetCatalogAPIDetailsUseCase1');
ga.getXMLAnswer(doPopulate);
}
function doPopulate(answer) {
var ID = answer;
alert("Answer is "+ answer);
g_form.setValue('select_resource_to_request_access', answer);
}
}
Please help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2023 12:23 PM
Hi geet,
If you want to call that script include from your client script, It needs to be defined like:
var STDMCatalogAPIUTILS = Class.create();
STMD CatalogAPIUTILS.prototype = Object.extendsObject(AbstractAjaxProcessor, {
I see you also call that from a 'flow action'. If making that change causes problems in your 'flow action', you may need to re-structure you script include, so main logic is in one, that can be called from another for the 'flow action' and client script using GlideAjax.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2023 01:02 PM
Hi,
I have tried what you have suggested but the answer is still null in the alert.
I checked logs for script include and it is returning the resource IDs
But it is not displaying those in the answer.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2023 02:35 PM - edited 10-12-2023 02:36 PM
Hi geet,
Script Includes use that use GlideAjax (so client side code can access the server side code) use what I've described and include statements to retrieve the parameters. I should have added that, like:
var someVariable = this.getParameter('sysparm_someVariable');
Your Client Script would have statement to set those what is required by the SI. such as:
var dtValue = g_form.getValue('follow_up');
var ga = new GlideAjax('global.datetimeutils');
ga.addParam('sysparm_name', 'zeroSeconds');
ga.addParam('sysparm_dtvalue', dtValue);
ga.getXML(doPopulate); // from your example
Those are from a client script I have in my instance, as an example of setting parameters.
So I believe you will a another version of your script include to use for the client script. See OOB examples in your instance. query the 'Client Script' table for "Script", "contains", "GlideAjax" and then review the script include it references to see required logic there. Again, I don't know that you can use the same code for both the client script and flow action.