- 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 10:34 AM
Yes, you can trigger an outbound REST API call using a Catalog Client Script in ServiceNow.
A Catalog Client Script is a script that runs on the client-side (i.e. in the user's web browser)
Here is another example:.
/*To trigger an outbound REST API call using a Catalog Client Script, you can use the XMLHttpRequest object or the fetch API to send an HTTP request to the desired API endpoint. Here's an example of how to use fetch to send an outbound REST API call from a Catalog Client Script:*/
javascript
Copy code
function onSubmit() {
var myData = {
// Data to send to the API endpoint
// ...
};
fetch('https://example.com/api/endpoint', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(myData)
})
.then(response => response.json())
.then(data => {
// Handle the API response data
// ...
})
.catch(error => {
// Handle the API error
// ...
});
}