How to pass values to a list collector from another list collector and remove when it removed.

Kumar147
Tera Contributor

Hello,

 

I have two List collectors on different tables.

List collector 1 - Change Table - Referenced to Incident

List collector 2 - Incident Table - Referenced to Change

When List Collector 2 on Incident Table is updated with any Change Request, The Incident number should be added to the List collector 1 on Change Table. similarly When Change request is Removed from List Collector 2 on Incident Table, The Incident number should be removed from List collector 1 on Change Table. List Collector 2 can have multiple changes linked. So when any change request is mapped to multiple Incidents and unmapped from any one of the Incident, the current Incident should be unmapped from the that change request and the remaining incidents should remains added.

Can anyone help me out this thing. Thanks in advance.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Kumar147 

something like this

1) After update BR on incident table, Condition List collector changes

(function executeRule(current, previous /*null when async*/) {
    // Avoid recursion
    if (gs.getSession().getProperty('skip_update') == 'true') {
        gs.getSession().setProperty('skip_update', 'false');
        return;
    }

    // Get the list of changes from the List Collector 2
    var currentChanges = current.u_list_collector_2.split(',');
    var previousChanges = previous.u_list_collector_2.split(',');

    // Find added and removed changes
    var addedChanges = currentChanges.filter(function(change) {
        return previousChanges.indexOf(change) === -1;
    });

    var removedChanges = previousChanges.filter(function(change) {
        return currentChanges.indexOf(change) === -1;
    });

    // Add the Incident number to the List Collector 1 on the Change table
    addedChanges.forEach(function(changeSysId) {
        var changeGR = new GlideRecord('change_request');
        if (changeGR.get(changeSysId)) {
            var incidentList = changeGR.u_list_collector_1.split(',');
            if (incidentList.indexOf(current.sys_id.toString()) === -1) {
                incidentList.push(current.sys_id.toString());
                changeGR.u_list_collector_1 = incidentList.join(',');
                gs.getSession().setProperty('skip_update', 'true');
                changeGR.update();
            }
        }
    });

    // Remove the Incident number from the List Collector 1 on the Change table
    removedChanges.forEach(function(changeSysId) {
        var changeGR = new GlideRecord('change_request');
        if (changeGR.get(changeSysId)) {
            var incidentList = changeGR.u_list_collector_1.split(',');
            var index = incidentList.indexOf(current.sys_id.toString());
            if (index !== -1) {
                incidentList.splice(index, 1);
                changeGR.u_list_collector_1 = incidentList.join(',');
                gs.getSession().setProperty('skip_update', 'true');
                changeGR.update();
            }
        }
    });

})(current, previous);

2) after update on CHG, condition List collector changes

(function executeRule(current, previous /*null when async*/) {
    // Avoid recursion
    if (gs.getSession().getProperty('skip_update') == 'true') {
        gs.getSession().setProperty('skip_update', 'false');
        return;
    }

    // Get the list of incidents from the List Collector 1
    var currentIncidents = current.u_list_collector_1.split(',');
    var previousIncidents = previous.u_list_collector_1.split(',');

    // Find added and removed incidents
    var addedIncidents = currentIncidents.filter(function(incident) {
        return previousIncidents.indexOf(incident) === -1;
    });

    var removedIncidents = previousIncidents.filter(function(incident) {
        return currentIncidents.indexOf(incident) === -1;
    });

    // Add the Change Request number to the List Collector 2 on the Incident table
    addedIncidents.forEach(function(incidentSysId) {
        var incidentGR = new GlideRecord('incident');
        if (incidentGR.get(incidentSysId)) {
            var changeList = incidentGR.u_list_collector_2.split(',');
            if (changeList.indexOf(current.sys_id.toString()) === -1) {
                changeList.push(current.sys_id.toString());
                incidentGR.u_list_collector_2 = changeList.join(',');
                gs.getSession().setProperty('skip_update', 'true');
                incidentGR.update();
            }
        }
    });

    // Remove the Change Request number from the List Collector 2 on the Incident table
    removedIncidents.forEach(function(incidentSysId) {
        var incidentGR = new GlideRecord('incident');
        if (incidentGR.get(incidentSysId)) {
            var changeList = incidentGR.u_list_collector_2.split(',');
            var index = changeList.indexOf(current.sys_id.toString());
            if (index !== -1) {
                changeList.splice(index, 1);
                incidentGR.u_list_collector_2 = changeList.join(',');
                gs.getSession().setProperty('skip_update', 'true');
                incidentGR.update();
            }
        }
    });

})(current, previous);

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

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Kumar147 

so basically you want to sync, remember you will have to ensure the update doesn't go in recursion

check this link with response from Sravani and enhance

Sync'ing RITM and SCTASK work notes \ additional comments 

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Kumar147 

something like this

1) After update BR on incident table, Condition List collector changes

(function executeRule(current, previous /*null when async*/) {
    // Avoid recursion
    if (gs.getSession().getProperty('skip_update') == 'true') {
        gs.getSession().setProperty('skip_update', 'false');
        return;
    }

    // Get the list of changes from the List Collector 2
    var currentChanges = current.u_list_collector_2.split(',');
    var previousChanges = previous.u_list_collector_2.split(',');

    // Find added and removed changes
    var addedChanges = currentChanges.filter(function(change) {
        return previousChanges.indexOf(change) === -1;
    });

    var removedChanges = previousChanges.filter(function(change) {
        return currentChanges.indexOf(change) === -1;
    });

    // Add the Incident number to the List Collector 1 on the Change table
    addedChanges.forEach(function(changeSysId) {
        var changeGR = new GlideRecord('change_request');
        if (changeGR.get(changeSysId)) {
            var incidentList = changeGR.u_list_collector_1.split(',');
            if (incidentList.indexOf(current.sys_id.toString()) === -1) {
                incidentList.push(current.sys_id.toString());
                changeGR.u_list_collector_1 = incidentList.join(',');
                gs.getSession().setProperty('skip_update', 'true');
                changeGR.update();
            }
        }
    });

    // Remove the Incident number from the List Collector 1 on the Change table
    removedChanges.forEach(function(changeSysId) {
        var changeGR = new GlideRecord('change_request');
        if (changeGR.get(changeSysId)) {
            var incidentList = changeGR.u_list_collector_1.split(',');
            var index = incidentList.indexOf(current.sys_id.toString());
            if (index !== -1) {
                incidentList.splice(index, 1);
                changeGR.u_list_collector_1 = incidentList.join(',');
                gs.getSession().setProperty('skip_update', 'true');
                changeGR.update();
            }
        }
    });

})(current, previous);

2) after update on CHG, condition List collector changes

(function executeRule(current, previous /*null when async*/) {
    // Avoid recursion
    if (gs.getSession().getProperty('skip_update') == 'true') {
        gs.getSession().setProperty('skip_update', 'false');
        return;
    }

    // Get the list of incidents from the List Collector 1
    var currentIncidents = current.u_list_collector_1.split(',');
    var previousIncidents = previous.u_list_collector_1.split(',');

    // Find added and removed incidents
    var addedIncidents = currentIncidents.filter(function(incident) {
        return previousIncidents.indexOf(incident) === -1;
    });

    var removedIncidents = previousIncidents.filter(function(incident) {
        return currentIncidents.indexOf(incident) === -1;
    });

    // Add the Change Request number to the List Collector 2 on the Incident table
    addedIncidents.forEach(function(incidentSysId) {
        var incidentGR = new GlideRecord('incident');
        if (incidentGR.get(incidentSysId)) {
            var changeList = incidentGR.u_list_collector_2.split(',');
            if (changeList.indexOf(current.sys_id.toString()) === -1) {
                changeList.push(current.sys_id.toString());
                incidentGR.u_list_collector_2 = changeList.join(',');
                gs.getSession().setProperty('skip_update', 'true');
                incidentGR.update();
            }
        }
    });

    // Remove the Change Request number from the List Collector 2 on the Incident table
    removedIncidents.forEach(function(incidentSysId) {
        var incidentGR = new GlideRecord('incident');
        if (incidentGR.get(incidentSysId)) {
            var changeList = incidentGR.u_list_collector_2.split(',');
            var index = changeList.indexOf(current.sys_id.toString());
            if (index !== -1) {
                changeList.splice(index, 1);
                incidentGR.u_list_collector_2 = changeList.join(',');
                gs.getSession().setProperty('skip_update', 'true');
                incidentGR.update();
            }
        }
    });

})(current, previous);

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