deactivate user

BanuMahalakshmi
Tera Contributor

Hi,

 

Assigned to field is list collector data type. when active value changes into false into sys_user table, the value should deactivate into assigned_to field also(cmdb_Ci table). I have tried the below script but it removed all assigned_to field value. pls let me know the changes in the script. Thanks.

 

BanuMahalakshmi_0-1721809357691.png

 

 

  var assignee = new GlideRecord('cmdb_ci');
    assignee .addQuery("assigned_to", '=', current.sys_id.toString());
    assignee .query();
    while (assignee .next()) {
        assignee .assigned_to = "";
        assignee .update();
    }
1 ACCEPTED SOLUTION

Mark Manders
Mega Patron

A list field contains the comma separated sys_id's of the users and you are setting the entire field to 'empty', so that's just doing exactly what you explain it does.

 

Try something like this:

(function executeRule(current, previous /*null when async*/) {

        var userSysId = current.sys_id.toString();
        var cmdbCiGr = new GlideRecord('cmdb_ci');
        cmdbCiGr.addQuery('assigned_to', 'CONTAINS', userSysId);
        cmdbCiGr.query();
        
        while (cmdbCiGr.next()) {
            var assignedTo = cmdbCiGr.getValue('assigned_to');
            var assignedToArray = assignedTo.split(',');

            // Remove the inactive user's sys_id from the assigned_to list
            var index = assignedToArray.indexOf(userSysId);
            if (index > -1) {
                assignedToArray.splice(index, 1);
                cmdbCiGr.setValue('assigned_to', assignedToArray.join(','));
                cmdbCiGr.update();
            }
        }
    
})(current, previous);

 


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

View solution in original post

10 REPLIES 10

SN_Learn
Kilo Patron
Kilo Patron

Hi @BanuMahalakshmi ,

 

In cmdb_ci, 'assigned_to' field is a reference field not a list type.
Is the glide list a custom field?

----------------------------------------------------------------
Mark this as Helpful / Accept the Solution if this helps.

RAMANA MURTHY G
Mega Sage
Mega Sage

Hello @BanuMahalakshmi ,

 

I have tried your requirements in my PDI, here are some changes, these are working fine for me

 

RAMANAMURTHYG_0-1721810962495.png

 

RAMANAMURTHYG_1-1721810988397.png

 

(function executeRule(current, previous /*null when async*/) {
	var gr = new GlideRecord('cmdb_ci');
	gr.addEncodedQuery('assigned_to='+current.getUniqueValue()); 
	gr.query();
	while(gr.next()){
		gr.assigned_to = '';
		gr.setWorkflow(false);    // if you want to stop other background work flows then you have to write this, otherwise no need
		gr.update();
	}
})(current, previous);
Please mark my answer helpful  & correct if it helps you
Thank you

G Ramana Murthy
ServiceNow Developer

The field is of list type as is in the question. This will do exactly the same as the BR already in the question.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

AMan1997
Tera Guru

Hi @BanuMahalakshmi ,

The script is correct. However, if you run the business rule as a Before. Keeping same script.
Then you will achieve your requirement.

AMan1997_0-1721811410148.png

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var assignee = new GlideRecord('cmdb_ci');
    assignee.addQuery("assigned_to", '=', current.sys_id.toString());
    assignee.query();
    while (assignee.next()) {
        assignee.assigned_to = "";
        assignee.update();
    }

})(current, previous);

 

If my response helped, Please mark the answer helpful/correct based on impact!

Thanks,