Need to Capture API Response when Message has "Successful" or "Failed" in body.

Allen12
Tera Guru

I need to get the response from the body of "SUCCESS" or "FAILURE" so I can use the conditions to allow for split actions.

find_real_file.png

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:

find_real_file.png

 

1 ACCEPTED SOLUTION

Allen12
Tera Guru

Here is the solution to my scenario:

I needed to use .includes to capture the "success" or "failure" response from the responseBody.

find_real_file.png

View solution in original post

5 REPLIES 5

Abhay Kumar1
Giga Sage
Hi Allen, at line 32 you can insert a new line and capture response in log what you receive after execute, or in catch block you can log same response in log as you just now captures error message. Hope this will help.

Pavankumar_1
Mega Patron

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

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Here is the code I adjusted,  The request is successfully being submitted, but when it fails, I still get "Success".

find_real_file.png

 

find_real_file.png

find_real_file.png

can something like this be added to capture the failure?  if so how?  I tried the code but it got stuck when it ran.

find_real_file.png

Pavankumar_1
Mega Patron

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

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar