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

Me Being Mustaq
Tera Guru

Hi @Akshaykhare ,

 

The issue where the Reassignment counter increments by 2 instead of 1 for a single assignment group change is likely caused by how the custom "Save" button's script manages the record update and redirects.

 

let me explain the script how it works

 

for condition:- !(current.isNewRecord() && !current.canCreate()) && current.canWrite()

  • This checks that the record is not a new record that the user can't create, and the user has write access.

  • This condition ensures the save logic only runs on editable existing or new records the user can update.

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

 

  • action.setRedirectURL(current); sets the redirect to the current record before the update.

  • current.update(); performs an update on the record, which triggers Business Rules, including any Reassignment counter increments.

  • After update, the script again sets redirect URL if the action wasn't aborted.

causes:

  • On the first current.update() call, the assignment group change triggers, incrementing the counter once.

  • The subsequent form submit/save caused by clicking the custom Save button also commits the same change again, incrementing the counter again.

  • Hence, the counter increments by 2 for what is logically one change.

fix:-

  • Remove the explicit current.update(); inside the save button script and let the form’s normal save process handle it.

  • Or, add a gs.eventQueue('some_custom_event') and have an asynchronous process handle the update if needed, avoiding multiple synchronous updates.

  • Alternatively, prevent the default form submit from firing twice — ensure that the Save button does not cause the form to submit after the explicit update.

If your button is a UI action running on the server side for form submit, a minimal script would be:

                                  action.setRedirectURL(current);

 

If you want to do additional validation or processing before save, do it without calling current.update() explicitly

 

If it is helpful, please hit the thumbs button and accept the correct solution by referring to this solution in the future it will be helpful to them.

 

Thanks & Regards,

Mohammed Mustaq Shaik

Hi @Me Being Mustaq 
If I remove current.update() from the script then how the data will be updated in database?

Note: User is only using this "Save" button (UI action) to update the task.

Hi @Akshaykhare ,

 

Oh, if the user is using the Save button, I need to check that. Let me do some analysis and I'll get back to you.

 

Thanks,

Mohammed Mustaq Shaik

Hi @Me Being Mustaq 


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);