Script to trigger REST message

servicenow begi
Tera Contributor

Requesting your help to trouble shoot my code.

1. here is the json sample request body I got from the client

{
"entityType": "ASSET",
"update": [
{
"entityId": "123456",
"fields": {
"item": [
{
"name": "Asset Status",
"value": "Received"
},
{
"name": "Total Cost",
"value": "100.50"
}]}}]}

2. expected Response:

{"code":"Success","data":[{"update":{"entityId":"123456","fields":{"item":[{"name":"Asset Status","value":"Received"},{"name":"Total Cost","value":"100.50"}]}},"status":{"type":"SUCCESS","statusCode":200}}]}

 

3. below is my scheduled job to run execute the rest message and based on my requirement to get all the assets updated in last 24hours

var request = new sn_ws.RESTMessageV2('TEST REST', 'PUT METHOD');
// Get all assets updated in the last 24 hours
var assetAttributes = [{
    'sName': 'u_status',
    'nameAPI': 'Asset Status'
}, {
    'sName': 'u_total_cost',
    'nameAPI': 'Total Cost'
}];
var myRestBody = {};
myRestBody.entityType = "ASSET";
myRestBody.update = [];
var gr = new GlideRecord('sn_ent_asset');
gr.addEncodedQuery('u_active=true^sys_updated_onRELATIVEGT@hour@ago@24');
gr.query();

while (gr.next()) {
    var updateObj = {};
    updateObj.entityId = gr.getValue('u_enterprise_asset_tag');
    updateObj.fields = {};
    updateObj.fields.item = [];
    for (i in assetAttributes) {
        {
            var itemObj = {};
            itemObj.name = assetAttributes[i].nameAPI;
            itemObj.value = gr.getDisplayValue([assetAttributes[i].sName]);
            updateObj.fields.item.push(itemObj);
        }
    }
    myRestBody.update.push(updateObj);
}
gs.log(JSON.stringify(myRestBody));
// 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 + httpStatus);
 
4. below is the response I am getting, from this script in the system logs
First response in system logs :
{"entityType":"ASSET","update":[{"entityId":"767676","fields":{"item":[{"name":"Asset Status,"value":"Received"},{"name":"Total Cost","value":"150"}]}}]}
 
Second response in system logs:
Asset ID: fa7b80fd97d52150cffc3717f053afd0, Response: TypeError: Cannot convert undefined or null to object500
 
Requesting your help to troubleshoot this error "Cannot convert undefined or null to object500"
2 REPLIES 2

kps sumanth
Mega Guru

Hello @servicenow begi ,

 

Can you share the Rest message and the function you are using, in the script you are not setting the requested body, you need to set the request body. Moreover you can check the request body by logging the below line.

 

gs.info(request.getRequestBody()).

Amit Gujarathi
Giga Sage
Giga Sage

HI @servicenow begi ,
I trust you are doing great.
Implement error handling for the REST call to gracefully handle cases where the call fails or returns an unexpected response.

try {
    var response = request.execute();
    if (response) {
        var responseBody = response.getBody();
        var httpStatus = response.getStatusCode();
        gs.log('Asset ID: ' + gr.getValue('sys_id') + ', Response: ' + responseBody + httpStatus);
    } else {
        gs.log('Error: The response from the REST call was null or undefined.');
    }
} catch (e) {
    gs.log('REST call failed with error: ' + e.toString());
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi