The CreatorCon Call for Content is officially open! Get started here.

Duplicate user's associate tasks to merge

Swati k1
Tera Contributor

Hi All,

 

We have observed that there are duplicate user records (sometimes more than three) with the same name in the system. However, only one of these users has a valid User ID, while the others do not.

When opening a user's profile, we can see related records under the related lists — such as Incidents, RITMs, and Requests. To address this, we are currently using a background script to migrate all task-related records from the duplicate (invalid) user profiles to the valid user (with the correct User ID).

Now, the client has requested to perform this de-duplication process via a Record Producer. I have created two reference variables:

  • Primary Username – the user to retain (valid user with User ID)

  • Secondary Username – the duplicate user to merge (missing User ID)

Kindly provide a Client Script and Script Include to implement this functionality through the Record Producer.

 

 

 

var primarySysId = '021bc84247598a105419f422516d43e3';
   var secondarySysId = '8be12086c3dd02d093fa12deb0013145';   var grTask = new GlideRecord('task');
   grTask.addEncodedQuery('u_requester=' + secondarySysId);
   grTask.query();
   while (grTask.next()) {       var table = grTask.sys_class_name.toString();       var gr = new GlideRecord(table);
       gr.addQuery('number', grTask.number);
       gr.query();
       if (gr.next()) {
           if (table == 'sc_req_item') {
               gr.u_requester = primarySysId;
           }           if (table == 'sc_task') {
               gr.u_requester = primarySysId;
           }
           if (table == 'incident') {
               gr.caller_id = primarySysId;
           }
           if (table == 'sc_request') {
               gr.requested_for = primarySysId;
           }
           gr.setWorkFlow(false);
           gr.autoSysFields(false);
           gr.update();
       }
   }

1 REPLY 1

M Iftikhar
Tera Sage

Hi Swati,

Here is a solution to perform user de-duplication via a Record Producer, including the Client Script and Script Include to implement the functionality:

Create a Hidden Checkbox Variable - deduplication_flag

Create a hidden checkbox variable to track if the de-duplication process has been completed:

MIftikhar_0-1759241438004.png

Client Script - "User De-Duplication" (onSubmit)

Create an onSubmit Client Script to trigger the de-duplication process when the user submits the form:

function onSubmit() {
    var deduplicationFlag = g_form.getValue('deduplication_flag');

    if (deduplicationFlag == 'false') {
        var primarySysId = g_form.getValue('primary_username');
        var secondarySysId = g_form.getValue('secondary_username');

        if (primarySysId && secondarySysId) {
            try {
                var ga = new GlideAjax('global.UserDeDuplication');
ga.addParam('sysparm_name', 'deDuplicateUserTasks');
                ga.addParam('sysparm_primary_sysid', primarySysId);
                ga.addParam('sysparm_secondary_sysid', secondarySysId);
                ga.getXMLAnswer(function(response) {
                    var answer = response.responseXML.documentElement.getAttribute('answer');
                    if (answer == 'success') {
                        // mark deduplication flag true
                        g_form.setValue('deduplication_flag', true);
                        g_form.addInfoMessage('User de-duplication completed successfully.');
                    } else {
                        g_form.addErrorMessage('Error occurred while de-duplicating users.');
                    }
                });
            } catch (e) {
                g_form.addErrorMessage('An error occurred during user de-duplication: ' + e.message);
            }
        } else {
            g_form.addErrorMessage('Please provide both Primary and Secondary Users.');
        }
// stop submission because the AJAX response is async
return false;
    } 
}

MIftikhar_1-1759241527016.png

Client Script - "Form Submission on Success" (onChange)

Create an onChange Client Script for the deduplication_flag variable to trigger form submission upon successful de-duplication:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    if (newValue == 'true') {
        g_form.submit();
    }
}
MIftikhar_2-1759241902842.png

Script Include - "UserDeDuplication"

Create a Script Include to handle the de-duplication logic:

var UserDeDuplication = Class.create();
UserDeDuplication.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    // To perform de-duplication logic
    deDuplicateUserTasks: function() {
        var primarySysId = this.getParameter('sysparm_primary_sysid');
        var secondarySysId = this.getParameter('sysparm_secondary_sysid');

        if (!primarySysId || !secondarySysId) {
            return 'error';
        }

        var grTask = new GlideRecord('task');
        grTask.addEncodedQuery('u_requester=' + secondarySysId);
        grTask.query();

        while (grTask.next()) {
            var table = grTask.sys_class_name.toString();
            var gr = new GlideRecord(table);
            gr.addQuery('number', grTask.number);
            gr.query();

            if (gr.next()) {
                // Update the related records based on the table type
                if (table == 'sc_req_item') {
                    gr.u_requester = primarySysId;
                }
                if (table == 'sc_task') {
                    gr.u_requester = primarySysId;
                }
                if (table == 'incident') {
                    gr.caller_id = primarySysId;
                }
                if (table == 'sc_request') {
                    gr.requested_for = primarySysId;
                }

                // Update the record
                gr.setWorkFlow(false);  // Disable workflows
                gr.autoSysFields(false); // Disable automatic system fields
                gr.update();
            }
        }

        return 'success'; // Return success after completing the task updates
    },

    type: 'UserDeDuplication'
});
MIftikhar_3-1759242131699.png

I hope this solution addresses your requirement for de-duplicating users via the Record Producer.

 

Thanks & Regards,
Muhammad Iftikhar
If my response helped, please mark it as the accepted solution so others can benefit as well.

Thanks & Regards,
Muhammad Iftikhar

If my response helped, please mark it as the accepted solution so others can benefit as well.