- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2023 11:02 PM
we need to trigger an out Bound rest API while catalog item field change (asset field )
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2023 06:10 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2023 12:52 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2023 03:10 AM
hi @Aavi
Thanks for your quick reply, can you share sample code if possible ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2023 06:10 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2023 10:01 AM
Perfect ,thanks for the details explanation .