Script to trigger REST message
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-14-2024 06:48 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-14-2024 07:28 AM
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()).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-14-2024 10:44 AM
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