- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2023 11:32 AM
Hi, requesting the members of this forum to please help fixing the issue
Requirement: whenever an asset is updated, send the updates to a third party tool using REST API.
STEP1: created connection - DONE
STEP2: Create rest api and PUT method with the end point and sample json request body and test - DONE
STEP 3: use a scheduled job to trigger this REST API, everyday at 5pm to send all the assets that are updated in last 24hours.
Issue: I am not sure how to pass asset details dynamically in the scheduled job
SAMPLE JSON REQUEST BODY:
{
"entityType": "ASSET",
"update": [
{
"entityId": "0001639", ------ (THIS ENTITY ID IS NOTHING BUT THE ASSET ID AND I AM NOT SURE HOW TO PASS THIS ASSET ID FOR ALL THE ASSETS UPDATED IN LAST 24HOURS)
"fields": {
"item": [
{
"name": "Commission....................",
"value": "${value}"
},
{
"name": "Net Proceeds..................",
"value": "${value}"
}]}}]}
My script below:
// getting all the assets updated in last 24hours.
var gr = new GlideRecord('sn_ent_asset');
gr.addEncodedQuery('sys_updated_onRELATIVEGT@hour@ago@24');
gr.query();
// rest message
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2023 01:26 PM
Hi,
Can you try below updated code
// Define the REST message
var request = new sn_ws.RESTMessageV2('Asset Integration', 'Asset Put');
// Get all assets updated in the last 24 hours
var gr = new GlideRecord('sn_ent_asset');
gr.addEncodedQuery('sys_updated_onRELATIVEGT@hour@ago@24');
gr.query();
while (gr.next()) {
// Create a JSON object for each asset
var assetData = {
entityType: 'ASSET',
update: [
{
entityId: gr.getValue('sys_id'), // Use the asset's sys_id as entityId
fields: {
item: [
{
name: 'Commission....................',
value: gr.getValue('u_commission')
},
{
name: 'Net Proceeds..................',
value: gr.getValue('u_net_proceeds')
}
]
}
}
]
};
// Set the JSON object as the request body
request.setRequestBody(JSON.stringify(assetData));
// Execute the REST message for this asset
var response = request.execute();
// Process the response if needed
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log('Asset ID: ' + gr.getValue('sys_id') + ', Response: ' + responseBody);
}
// Note: You can customize the field names and values as per your asset table schema.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2023 12:56 PM
Hi,
After doing the GlideRecord you need to process your results and build request body.
Below is a sample (very rough) code on how to build up this object (ofc you will have to amend it to you scenario - I've used cmdb_ci_win_server table as my source of data but the main idea should be exactly the same. You can run this code in background script on your pdi for review:
var assetAttributes = ["name","ram","os","cpu_type"];
var myRestBody = {};
myRestBody.entityType = "ASSET";
myRestBody.update = [];
var grData = new GlideRecord('cmdb_ci_win_server');
grData.setLimit(3);
grData.query();
while (grData.next()) {
var updateObj = {};
updateObj.entityId = grData.asset_tag.toString();
updateObj.fields = {};
updateObj.fields.item = [];
for (i in assetAttributes) {{
var itemObj = {};
itemObj.name = assetAttributes[i];
itemObj.value = grData[assetAttributes[i]].toString();
updateObj.fields.item.push(itemObj);
}}
myRestBody.update.push(updateObj);
}
gs.info(JSON.stringify(myRestBody));
After necessary corrections you need to pass JSON.stringify(myRestBody) as you requestbody.
Should you find this useful please be so kind to mark my answer helpful and as solution to your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2023 01:26 PM
Hi,
Can you try below updated code
// Define the REST message
var request = new sn_ws.RESTMessageV2('Asset Integration', 'Asset Put');
// Get all assets updated in the last 24 hours
var gr = new GlideRecord('sn_ent_asset');
gr.addEncodedQuery('sys_updated_onRELATIVEGT@hour@ago@24');
gr.query();
while (gr.next()) {
// Create a JSON object for each asset
var assetData = {
entityType: 'ASSET',
update: [
{
entityId: gr.getValue('sys_id'), // Use the asset's sys_id as entityId
fields: {
item: [
{
name: 'Commission....................',
value: gr.getValue('u_commission')
},
{
name: 'Net Proceeds..................',
value: gr.getValue('u_net_proceeds')
}
]
}
}
]
};
// Set the JSON object as the request body
request.setRequestBody(JSON.stringify(assetData));
// Execute the REST message for this asset
var response = request.execute();
// Process the response if needed
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.log('Asset ID: ' + gr.getValue('sys_id') + ', Response: ' + responseBody);
}
// Note: You can customize the field names and values as per your asset table schema.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2024 07:29 AM
thankyou so much, the script worked and I am getting the expected response.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2023 01:35 PM
Hi, your requirements will be 100% dependent on the target platform's ability to understand/decode your payload.
Do you have a required\defined payload format?
If the requirement is based on the formatting you added in your post above? Then it looks like individual rest messages per source record? so you would run your glidequery on your source table and within your glide loop trigger a rest message per record
RESTMessageV2 | ServiceNow Developers
Setting your body values from your glidequery in each loop.
If the requirement is 1 payload, then you would run your glidequery populating the required data into an object and\or array (depending on requirements) and then add this to the formatted message body before setting as payload body.
Are you able to confirm the required format?