- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2022 11:22 AM
I need to get the response from the body of "SUCCESS" or "FAILURE" so I can use the conditions to allow for split actions.
This is what we have so far. It gives a response if communication fails, but I want a response when the transaction fails.
Here is the output log:
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2022 08:53 AM
Here is the solution to my scenario:
I needed to use .includes to capture the "success" or "failure" response from the responseBody.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2022 10:22 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2022 11:10 PM
Hi,
you can use status code if is 200 set success else you can set as failure.
Like this if (status ==200){
activity.result= "Success";
}
else{
activity.result= "Failure";
}
OR Just understand below and make necessary changes to implement your requirement.
or share your full requirement I will help you on this.
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
this.status = '200';
var respObj = {}; //declare the response object
var payload = request.body.data; //retrieving the JSON body
var number = payload.number; //getting the number from JSON
var callerid = payload.callerid;
var state = payload.state;
var category = payload.category;
var impact = payload.impact;
var urgency = payload.urgency;
var short_description = payload.short_description;
var description = payload.description;
var grinc = new GlideRecord("incident");
grinc.addQuery("number", number);
grinc.query();
if (grinc.next()) { // To update data on the existing incidents
grinc.caller_id = callerid;
grinc.state = state;
grinc.category = category;
grinc.impact = impact;
grinc.urgency = urgency;
grinc.short_description = short_description;
grinc.description = description;
grinc.update();
this.status = '200';
respObj.body = {
"message": "Updation Success!",
"detail": "Incident " + grinc.number + " updated successfully"
};
} else if (callerid) { //create incident if when callerid is not empty
grinc.initialize();
grinc.caller_id = callerid;
grinc.state = state;
grinc.category = category;
grinc.impact = impact;
grinc.urgency = urgency;
grinc.short_description = short_description;
grinc.description = description;
grinc.insert();
this.status = '200';
respObj.body = {
"message": "Creation Success!",
"detail": "Incident " + grinc.number + " created successfully"
};
} else { //ignore the creation if there is no caller info
this.status = '400';
respObj.body = {
"message": "Incident Ignored",
"detail": "Caller email is not received from Request"
};
}
if (this.status == '200') {
response.setBody(respObj);
response.setStatus(this.status);
return response;
} else {
var setError= new sn_ws_err.ServiceError(); //this API used to set the custom error messages
setError.setStatus(this.status);
setError.setMessage(respObj.body.message);
setError.setDetail(respObj.body.detail);
return setError;
}
})(request, response);
Hope you got my point.
Please Mark ✅ Correct/Helpful if applicable, Thanks!!
Regards
Pavankumar
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2022 06:31 AM
Here is the code I adjusted, The request is successfully being submitted, but when it fails, I still get "Success".
can something like this be added to capture the failure? if so how? I tried the code but it got stuck when it ran.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2022 05:57 AM
Hi,
Please close the thread by marking correct so others will get benefit if it is resolved.
Mark ✅ Correct/Helpful if my response got resolved your issue, Thanks!!
Regards
Pavankumar
ServiceNow Community MVP 2024.
Thanks,
Pavankumar