How to retry if transaction get cancelled for inbound integration

Roshnee Dash
Tera Guru

In an inbound integration scenario, if a transaction is interrupted mid-execution—due to instance slowness or other issues—the script may not complete successfully. As a result, although the record is created in ServiceNow, the response is not sent back to the third-party system, preventing the corresponding record from being created/updated there.And 3rd party cant try 2nd time as its having unique value. How can we implement a retry mechanism to re-execute the script and ensure the response is captured and stored in a custom table for future reference or reprocessing?
Here is my code sample of Scripted rest api resource

(function process(request, response) {
    var body = request.body.data;
    var workNotes = JSON.parse(body.u_work_notes || '{}');
    var splitDetails = workNotes.SplitDetails;
    var wipAccruals = workNotes.WIPAccruals;
    var accessToken = request.getHeader('Authorization');
    var responseObj = {};

    try {
        var generatePayload = new genratePayloadfor().generatePayload(body);
        var updatePayload;
        var fin = new GlideRecord('table1');
        if (fin.get('unique_number', body.unique_number)) {
            var updateFin = new CreateFinImport().createFinImportRecord(body);
            responseObj = {
                "Finance request": fin.number.toString(),
                "sys_id": fin.sys_id.toString(),
                "message": "Record already exist!"
            };
            updatePayload = new genratePayloadfor().updatePayload(generatePayload, updateFin, fin.sys_id, 'update',responseObj,200);
            return responseObj;
        }

        if (!body.u_assignment_group || !body.u_description) {
            var error = new sn_ws_err.ServiceError();
            error.setStatus(442);
            error.setMessage('Assignment group and Description are mandatory values to be sent');
            error.setDetail('Assignment group and Description are mandatory values to be sent');
            return error;
        }

        /**If the BDR number is not exist than create a new Schedule and Bill Record through the Record Producer "Finance Request Split Details" */

        var requestfin = new sn_ws.RESTMessageV2();
        requestfin.setEndpoint(gs.getProperty("sys_proper") + 'api/sn_sc/servicecatalog/items/sys_id/submit_producer');
        requestfin.setHttpMethod('POST');
        requestfin.setRequestHeader("Accept", "application/json");
        requestfin.setRequestHeader("Content-Type", "application/json");
        requestfin.setRequestHeader("Authorization", accessToken);

        requestfin.setRequestBody(JSON.stringify({
            variables: {
                bdr_number: body.uniquenumber,
                u_type_of_request: 'type',
                split_details: JSON.stringify(splitDetails),
                u_description: body.u_description,
                wip_accruals: JSON.stringify(wipAccruals),
                final_fee_note: body.u_ztot_value
            }
        }));

        var res = requestfin.execute();
        var resBody = JSON.parse(res.getBody());
        var result = resBody.result;
        var statusCode = res.getStatusCode();
        var resHeader = JSON.stringify(res.getAllHeaders());
//from here the trasaction got cancelled
        if (res.getStatusCode() == 200) {
           
            var insertFin = new CreateFinImport().createFinImportRecord(body);
            responseObj = {
                "Finance request": result.number1.toString(),
                "sys_id": result.sys_id.toString(),
                "message": "Record has been created!"
            };
            updatePayload = new genratePayloadfor().updatePayload(generatePayload, insertFin, result.sys_id, 'create',responseObj,200);
            return responseObj;

        } else {
            var error = new Error("Insert failed. No sys_id returned.");
            error.detail = "Current values: " + JSON.stringify(gr);
            error.statusCode = 400; // Bad Request or any other code
            throw error;
        }

    } catch (ex) {
        gs.error("Caught error: " + ex.message);
        var httpCode = ex.statusCode || 500;
        response.setStatus(httpCode);
        responseObj = {
            status: "failure",
            error: {
                message: ex.message,
                detail: ex.detail
            }
        };
       
        response.setBody(responseObj);
        updatePayload = new genratePayloadfor().updatePayload(generatePayload, '', '', '',responseObj,httpCode);

    }

})(request, response);


 

Your feedback makes the community stronger! If you found this helpful, marking it as the correct answer helps others.
Stay awesome,
Roshnee Dash
3 REPLIES 3

Roshnee Dash
Tera Guru

Hi@Ankur Bawiskar can you please help with this post?

Your feedback makes the community stronger! If you found this helpful, marking it as the correct answer helps others.
Stay awesome,
Roshnee Dash

Ankur Bawiskar
Tera Patron
Tera Patron

@Roshnee Dash 

since the API is consumed by 3rd party why should ServiceNow have retry mechanism

3rd party should handle this and if they send same info again, within ServiceNow you can check and if already record present then don't create it and send response to 3rd party that earlier request was success

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Okay. But I want to store the response in a custom table for future tracking purposes, but due to a transaction issue, the data isn't being inserted into the table. How can I resolve this?

Your feedback makes the community stronger! If you found this helpful, marking it as the correct answer helps others.
Stay awesome,
Roshnee Dash