Server script help for outbound call

Are Kaveri
Tera Contributor

Hi,

 

I have below requirement.

 

we have a custom table and we are integrating with third party system.

 

When state is Pending for Int then we are triggering the below assync BR.

  try {

        var access_Token = new global.myIntegrationUtils().getAccessToken('sys_ of oauth', 'name of Application registry');

        var r = new sn_ws.RESTMessageV2('restmessage_name', 'RESTMESSAGE');
        r.setRequestHeader('Authorization', 'Bearer ' + access_Token);

        r.setStringParameterNoEscape('emp_id', current.getValue("employee_number"));
        r.setStringParameterNoEscape('my_id_number', current.getValue("id_number"));

        var response = r.execute();
        var responseBody = response.getBody();
        var httpStatus = response.getStatusCode();

        responseBody = typeof responseBody == "string" ? responseBody : JSON.stringify(responseBody);
        var SAPresposneToUpdate = httpStatus == 201 ? gs.getMessage('Successfully updated in SAP \n {0}', [responseBody]) : responseBody;


        current.work_notes = SAPresposneToUpdate;
        if (httpStatus == 201 || httpStatus == 200) {
            current.state = 3; // if Success = closing request
            gs.eventQueue('notigy_requestor', current, '');
        } else {
// if there is any error we need to retrigger 
state= 5; // this will be moving to retrigger state
current.id_number = ''; //setting blank to this field
      gs.eventQueue('notigy_grmember', current, '');
        }

        current.update();

    } catch (ex) {
        var message = ex.message;
    }

 

 

I am not understanding how to retrigger the Business rule and handle the errors.

errors can be as below:

employee number locked = we will get status code = 400

if we give same id number for 2 request = we will status code= 400

we need to correct this and retrigger.

 

Please help me if anyone have any idea how to handle error codes and retrigger my Outbound integration.

 

14 REPLIES 14

You can modify your else block of the code as below :

 

else {
// if there is any error we need to retrigger 
current.state= 5; // I assume that 5 represents your retrigger state
current.id_number = ''; //setting blank to this field
      gs.eventQueue('notigy_grmember', current, '');
        }

 

Once the record goes back to retrigger state, someone will update the correct employee id and need to mark the state to pending for int. This will satisfy the trigger condition of the business rule and it should run.

 

Also, just a suggestion, you could have made use of a flow which could have simplified this. Whenever the REST API call fails, you can create a task for the records which failed and once the issues are fixed and task is closed, the flow could resume and make the REST API call again.


Please mark this response as correct and helpful if it assisted you with your question.

@Amit Verma 

Where do I need to mark pending for int 

 

that’s where I got stuck 

@Are Kaveri 

 

The one who would be making the correction to your record's Employee ID need to change the state as well to Pending for int manually so that it could be picked up by the Business rule.

 

I assume that the state change to pending for int is being done from your third party tool as of now or it's being controlled by something else ?


Please mark this response as correct and helpful if it assisted you with your question.

@Amit Verma 

 

In BR when i receive error we are setting state = retrigger

after manual update we need to change to  state= pending for Int (once update happens the state need to update automatically.)

 

Hi @Are Kaveri 

 

Try with the below approach :

 

1, Make the Business rule work on the conditions like State is Pending for Int OR State Changes To retrigger.

2. In the advanced section of your Business rule, make use of the following logic. Please amend the code as per your requirement :

 

(function executeRule(current, previous /*null when async*/ ) {
    // check if the state is retrigger then execute code to change the state to Pending for Int
    if (current.state == 5) {
        // Make the state change to Pending for Int
        current.state = 1; //Assuming 1 is the state value for Pending for Int
        current.update();
    }
    // else if state is Pending for Int, execute REST API code
    else if (current.state == 1) {
        try {

            var access_Token = new global.myIntegrationUtils().getAccessToken('sys_ of oauth', 'name of Application registry');

            var r = new sn_ws.RESTMessageV2('restmessage_name', 'RESTMESSAGE');
            r.setRequestHeader('Authorization', 'Bearer ' + access_Token);

            r.setStringParameterNoEscape('emp_id', current.getValue("employee_number"));
            r.setStringParameterNoEscape('my_id_number', current.getValue("id_number"));

            var response = r.execute();
            var responseBody = response.getBody();
            var httpStatus = response.getStatusCode();

            responseBody = typeof responseBody == "string" ? responseBody : JSON.stringify(responseBody);
            var SAPresposneToUpdate = httpStatus == 201 ? gs.getMessage('Successfully updated in SAP \n {0}', [responseBody]) : responseBody;


            current.work_notes = SAPresposneToUpdate;
            if (httpStatus == 201 || httpStatus == 200) {
                current.state = 3; // if Success = closing request
                gs.eventQueue('notigy_requestor', current, '');
            } else {
                // if there is any error we need to retrigger 
                current.state = 5; // this will be moving to retrigger state
                current.id_number = ''; //setting blank to this field
                gs.eventQueue('notigy_grmember', current, '');
            }

            current.update();

        } catch (ex) {
            var message = ex.message;
        }
    }

})(current, previous);

 

You can take reference from this approach and try.


Please mark this response as correct and helpful if it assisted you with your question.