Calling REST POST message in Business Rule

Steven Watts2
Tera Guru

Hi,

I'm trying to call a REST message via a business rule but it's not being run. I've confirmed that the business rule is getting called when the conditions are meet. I've also confirmed the REST message works by posting successfully to Slack directly from the REST message, I.e., not calling it from a business rule.

 

This is the business rule. I've used the script usage preview from the REST message to generate the script. The only changes i've made are to add in some logging statements and create an object which i'm trying to use to set the body.

 

(function executeRule(current, previous /*null when async*/ ) {

    var obj = {
        "text": "test"
    };

    try {
        var r = new sn_ws.RESTMessageV2('Slack POST', 'Post Incident Slack');

        //override authentication profile 
        //authentication type ='basic'/ 'oauth2'
        //r.setAuthenticationProfile(authentication type, profile name);

        //set a MID server name if one wants to run the message on MID
        //r.setMIDServer('MY_MID_SERVER');

        //if the message is configured to communicate through ECC queue, either
        //by setting a MID server or calling executeAsync, one needs to set skip_sensor
        //to true. Otherwise, one may get an intermittent error that the response body is null
        //r.setEccParameter('skip_sensor', true);
		r.setRequestBody(obj);
        var response = r.execute();
        var responseBody = response.getBody();
        var httpStatus = response.getStatusCode();
    } catch (ex) {
        var message = ex.message;
		gs.log("Slack responseBody" + responseBody);
		gs.log("Slack httpStatus" + httpStatus);
		gs.log("Slack message" + message);
    }
})(current, previous);

 Can anyone see what i'm doing wrong? I get nothing in the logs for the log statements in catch.

 

Steven

1 ACCEPTED SOLUTION

AnveshKumar M
Tera Sage
Tera Sage

Hi @Steven Watts2 ,

Have you tried adding log statements in try block like these so that you can check if there are any error from end point.

gs.log('Slack response:' + response);
gs.log('Slack response body:' + responseBody);
gs.log('Slack HTTP Status:' + httpStatus);

 

Thanks,
Anvesh

View solution in original post

4 REPLIES 4

AnveshKumar M
Tera Sage
Tera Sage

Hi @Steven Watts2 ,

Have you tried adding log statements in try block like these so that you can check if there are any error from end point.

gs.log('Slack response:' + response);
gs.log('Slack response body:' + responseBody);
gs.log('Slack HTTP Status:' + httpStatus);

 

Thanks,
Anvesh

Hey @AnveshKumar M 

Thanks for the suggestion, I can see that i'm getting a 400 response back now with invalid_payload in the body. I'll update the object i'm sending and then try again. 

Steven

@Steven Watts2  That's Great!

 

Please mark my answer helpful and accept as solution if it helped you.

Thanks,
Anvesh

@AnveshKumar M Thanks again, this worked:

 

(function executeRule(current, previous /*null when async*/ ) {




    try {
        var r = new sn_ws.RESTMessageV2('Slack POST', 'Post Incident Slack');

        //override authentication profile 
        //authentication type ='basic'/ 'oauth2'
        //r.setAuthenticationProfile(authentication type, profile name);

        //set a MID server name if one wants to run the message on MID
        //r.setMIDServer('MY_MID_SERVER');

        //if the message is configured to communicate through ECC queue, either
        //by setting a MID server or calling executeAsync, one needs to set skip_sensor
        //to true. Otherwise, one may get an intermittent error that the response body is null
        //r.setEccParameter('skip_sensor', true);

        var obj = {
            "text": "test"
        };
        r.setRequestBody(JSON.stringify(obj));
        var response = r.execute();
        var responseBody = response.getBody();
        var httpStatus = response.getStatusCode();

        gs.log('Slack response:' + response);
        gs.log('Slack response body:' + responseBody);
        gs.log('Slack HTTP Status:' + httpStatus);

    } catch (ex) {
        var message = ex.message;
        gs.log("Slack responseBody" + responseBody);
        gs.log("Slack httpStatus" + httpStatus);
        gs.log("Slack message" + message);
    }
})(current, previous);