Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Reassignment counter increased by 2 for single change in assignment group

Akshaykhare
Tera Contributor

In my ServiceNow instance Reassignment counter is getting increased by 2 for single change of assignment group. This is an intermittent issue. As per ServiceNow support this is occurring due to custom "Save" button on form. Following is the condition and script for the "Save" button. Can someone help me to understand what is in the script that is causing the issue

Condition : !(current.isNewRecord() && !current.canCreate()) && current.canWrite()
Script: 

action.setRedirectURL(current);
current.update();
if (! current.isActionAborted())
    action.setRedirectURL(current);


Akshaykhare_0-1761805420311.png

 






13 REPLIES 13

@Ankur Bawiskar 
I got the root cause for the issue, the following after BR runs on insert and update when assignment group field updated with particular assignment group on sc_task table. 

The update statement in this BR(current.update() ) triggers the BR for reassignment count increment again. Is there way to avoid this. 

(function executeRule(current, previous /*null when async*/ ) {
    var var1 = {};
    var sctask = new GlideRecord('sc_task');
    sctask.addQuery('number', current.number);
    sctask.query();
    if (sctask.next()) {
        var keys = [];
        var questionSet = new GlideappVariablePoolQuestionSet();
        questionSet.setRequestID(current.parent);
        questionSet.load();
        var question = questionSet.getFlatQuestions();
        for (var i = 0; i < question.size(); i++) {
            var1[question.get(i).getLabel()] = question.get(i).getDisplayValue();
        }
    }

    try {
        var lastWorknote = '';
        var lastComments = '';
        if (current.work_notes.changes()) {
            var worknotes = current.work_notes.getJournalEntry(1);
            lastWorknote = worknotes.toString();
        }
        if (current.comments.changes()) {
            var incComments = current.comments.getJournalEntry(1);
            lastComments = incComments.toString();
        }

        var r = new sn_ws.RESTMessageV2('Cloud Exponence Integration_ServiceReq', 'SR Create');
        var ob = {
            "ritm": {
                "variables": var1,
                correlation_display: current.getDisplayValue('parent'),
                correlation_id: current.parent.toString(),
                catalog_name: current.cat_item.getDisplayValue(),
                resolution_sla: current.parent.cat_item.u_sla.getDisplayValue(),
            },

            "sctask": {
                state: current.state.toString(),
                priority: current.priority.toString(),
                location: current.location.getDisplayValue(),
                requested_for: current.request_item.requested_for.getDisplayValue(),
                short_description: current.getDisplayValue('short_description'),
                assignment_group: current.assignment_group.getDisplayValue(),
                description: current.getDisplayValue('description'),
                submitted_details: current.getDisplayValue('u_submitted_details'),
                comments: lastComments,
                work_notes: lastWorknote,
                sctask_correlation_display: current.getDisplayValue('number'),
                sctask_correlation_id: current.sys_id.toString(),
            }
        };

        r.setRequestBody(JSON.stringify(ob));
        var req1 = r.getRequestBody(JSON.stringify(ob));
        var response = r.execute();
        var responseBody = response.getBody();
        var httpStatus = response.getStatusCode();
        gs.info('ServiceRequest CREATE:E-Bonding Integration: Request :' + req1);
        gs.info('ServiceReq CREATE:E-Bonding Integration: Body : ' + responseBody);
        var responseJSON = responseBody.substring(10, responseBody.length - 1);
        var parsedJSON = JSON.parse(responseJSON);
        var taskcorrelationId = parsedJSON['task_correlation_id'];
        var taskcorrelationDisplay = parsedJSON['task_correlation_display'];
        current.correlation_id = taskcorrelationId;
        current.correlation_display = taskcorrelationDisplay;
        current.update();
        var correlationId = parsedJSON['correlation_id'];
        var correlationDisplay = parsedJSON['correlation_display'];
        var num = current.parent.getDisplayValue();
        var gr = new GlideRecord('sc_req_item');
        gr.addQuery('number', num);
        gr.query();
        if (gr.next()) {
            {
                gr.correlation_id = correlationId;
                gr.correlation_display = correlationDisplay;
                gr.update();
            }
        }

        var getAttachment = new GlideRecord('sys_attachment');
        getAttachment.addEncodedQuery('table_name=sc_task^table_sys_id=' + current.sys_id + '^NQtable_name=sc_req_item^table_sys_id=' + current.parent);
        getAttachment.query();
        if (getAttachment.hasNext()) {
            while (getAttachment.next()) {
                var tableName = getAttachment.table_name;
                var corrID = '';
                if (tableName == 'sc_task') {
                    corrID = current.correlation_id;
                } else if (tableName == 'sc_req_item') {
                    corrID = correlationId;
                }
                var recordID = getAttachment.table_sys_id;
                var nameofFile = getAttachment.file_name;
                var contentType = getAttachment.content_type.getValue();
                //  gs.info('jkAttachment: ' + contentType + " , " + tableName + " , " + recordID + " , " + nameofFile + " , " + corrID);
                var r_attach = new sn_ws.RESTMessageV2('Cloud Exponence Integration_attachment', 'add_attachment');

                r_attach.setQueryParameter('table_name', tableName);
                r_attach.setQueryParameter('table_sys_id', corrID);
                r_attach.setQueryParameter('file_name', nameofFile);
                r_attach.setRequestHeader('content-Type', contentType);
                r_attach.setRequestHeader('Accept', 'application/json');
                r_attach.setRequestBodyFromAttachment(getAttachment.sys_id);
                var response_attach = r_attach.execute();
                var responseBody_attach = response_attach.getBody();
                var httpStatus_attach = response_attach.getStatusCode();
            }
        }

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

})(current, previous);

@Akshaykhare 

use setWorkflow(false)

current.correlation_display = taskcorrelationDisplay;
   current.setWorkflow(false); // avoids triggering any other BR
        current.update();

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Jeffrey Siegel
Mega Sage

is your UI action's "Action name" the same name as another UI action available to the table?  try renaming the custom UI Actions "Action name" and see if it still saves 2x.

@Jeffrey Siegel 
I did check but there is no other UI action present with same name on that table