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.

How to delete multiple User Relationships record in workflow runscript ?

ads
Tera Expert

Hi All,

I want to delete multiple record in the User Relationship table by getting the ci from the variable in the cat item and users from a list collector type variable. I used the below run script, Its not deleting the multiple record. Can anyone help me on this ?

var dataCenter = current.variables.data_centre;

if (type == 'Remove access') {
    var removeRel = current.variables.please_select_person_to_remove.toString().split(',');//list collector type variable
    for (var i = 0; i < removeRel.length; i++) {
 
        var userGR = new GlideRecord('sys_user');
        userGR.addQuery('sys_id', removeRel[i]);
        userGR.query();
        if (userGR.next()) {
 
            var relDelete = new GlideRecord("cmdb_rel_person");
            relDelete.addQuery('ci', dataCenter);
            relDelete.addQuery('user', userGR.sys_id);
            relDelete.query();
            while (relDelete.next()) {
                relDelete.deleteRecord();
            }
        }
    }
}
 
13 REPLIES 13

Siddhesh Gawade
Mega Sage
Mega Sage

Hello @ads ,

Can you please check the values of variable 'dataCenter' and removeRel  . It should be sys_ids not the displayvalue. 

For this line of code    'userGR.addQuery('sys_id', removeRel[i]);'   and for the ' relDelete.addQuery('ci', dataCenter); '  you need to give the sys_ids. So please confirm that. 


 

Kindly mark my answer as Correct and helpful based on the Impact.

Regards,

Siddhesh

 

Hi, I have not used the displayValue(). So It will get the sys_id, do you have any idea how to get the sys_ids from this line - 'userGR.addQuery('sys_id', removeRel[i]);' 

@ads can you show me the values of varibles removeRel and dataCenter. I can write a code for you. 

var dataCenter = current.variables.data_centre;

current.variables.please_select_person_to_remove.toString().split(',');//list collector type variable