Can we trigger outbound REST using catalog client script ?

String
Kilo Sage

we need to trigger an out Bound rest API while catalog item field change (asset field )

 

String_0-1677394560050.png

 

outbound Json:

“Catalog”: {

        “Asset”:”XYZ”,

        “Type:”outbound”,

        }

 

when user changes the asset field than we have capture the asset values and  trigger the out Bound .

So Can please suggest the best practices to achieve 

Please guide me

 

1 ACCEPTED SOLUTION

Try the following steps:

 

1. Navigate to System Web services -> Outbound -> Rest Messages, open the Http methods for the api which created and scroll down to related links to click "Preview Script usage" and copy the script.

 

2. Go to script include -->New
Name: integrationClientUtilities
Client callable: true
Accessible from: All application scopes
Script:

var integrationClientUtilities = Class.create();
integrationClientUtilities.prototype = {
initialize: function() {},

apicall: function() {
var asset = this.getParameter('sysparm_asset'); //value from client
var asset_name; //get the asset display name
var get_asset = new GlideRecord('alm_asset');
if (get_asset.get(asset)) {
asset_name = get_asset.display_name.toString();
}
//Paste the script which copied in Step 2
try {
var r = new sn_ws.RESTMessageV2('Integration', 'method post');
r.setStringParameterNoEscape('Asset', asset_name);
r.setStringParameterNoEscape('Type', 'outbound');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.info("header " + httpStatus);
gs.info("body " + responseBody);
if (httpStatus == 200)
return 'success'; //In case if you want store the output in the form, you should parse response body

} catch (ex) {
var message = ex.message;
}
},

type: 'integrationClientUtilities'
});

 

3. Open the catalog item to create new catalog client script

 

Name: onChange Asset call API
Type: onChange
UI Type: All
Variable Name: Asset
Script:

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var gaAJAX = new GlideAjax('integrationClientUtilities'); //Script include name
gaAJAX.addParam('sysparm_name', 'apicall'); //Function name
gaAJAX.addParam('sysparm_asset', g_form.getValue('asset')); //Pass the asset value to server side, make sure the variable name is correct
gaAJAX.getXMLAnswer(function(answer) {
if (answer == 'success')
g_form.addInfoMessage('API call success');
else
g_form.addErrorMessage('API call got failed')
';
}
}

 

Thanks,
Faizeal.

View solution in original post

5 REPLIES 5

Aavi
Mega Guru

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 executable code.  

 

Please mark as correct answer if this solves your issue.

 

hi @Aavi 

Thanks for your quick reply, can you share sample code if possible ?

Try the following steps:

 

1. Navigate to System Web services -> Outbound -> Rest Messages, open the Http methods for the api which created and scroll down to related links to click "Preview Script usage" and copy the script.

 

2. Go to script include -->New
Name: integrationClientUtilities
Client callable: true
Accessible from: All application scopes
Script:

var integrationClientUtilities = Class.create();
integrationClientUtilities.prototype = {
initialize: function() {},

apicall: function() {
var asset = this.getParameter('sysparm_asset'); //value from client
var asset_name; //get the asset display name
var get_asset = new GlideRecord('alm_asset');
if (get_asset.get(asset)) {
asset_name = get_asset.display_name.toString();
}
//Paste the script which copied in Step 2
try {
var r = new sn_ws.RESTMessageV2('Integration', 'method post');
r.setStringParameterNoEscape('Asset', asset_name);
r.setStringParameterNoEscape('Type', 'outbound');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.info("header " + httpStatus);
gs.info("body " + responseBody);
if (httpStatus == 200)
return 'success'; //In case if you want store the output in the form, you should parse response body

} catch (ex) {
var message = ex.message;
}
},

type: 'integrationClientUtilities'
});

 

3. Open the catalog item to create new catalog client script

 

Name: onChange Asset call API
Type: onChange
UI Type: All
Variable Name: Asset
Script:

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var gaAJAX = new GlideAjax('integrationClientUtilities'); //Script include name
gaAJAX.addParam('sysparm_name', 'apicall'); //Function name
gaAJAX.addParam('sysparm_asset', g_form.getValue('asset')); //Pass the asset value to server side, make sure the variable name is correct
gaAJAX.getXMLAnswer(function(answer) {
if (answer == 'success')
g_form.addInfoMessage('API call success');
else
g_form.addErrorMessage('API call got failed')
';
}
}

 

Thanks,
Faizeal.

Hi @Faizeal Mohamed 

Perfect ,thanks for the details explanation .