deleting a record

Balakrishna Ko1
Tera Contributor

I have to delete record that is selected in catalog form. so i have written code below in runscript activity in workflow. the record is not deleting from table only few times it is deleting. can anyone help me on this. thanks

find_real_file.png

script

var gr = new GlideRecord('u_training_courses');

//gr.addQuery('sys_id',current.variables.Requested_for);

gr.addQuery('active',true);

gr.query();

gr.next();

gr.deleteRecord();

1 ACCEPTED SOLUTION

Hi Bala,



1. Field name - 'Requested_for' ('R' in caps). ServiceNow converts the field names to be smaller case by default. Need to change it as 'requested_for' or copy/paste the correct field name.


2. Field name to be queried with is 'group.name' and not 'group'. Make sure you are giving the correct group name (with correct case too).



Please try the below modified script.



var gr = new GlideRecord('sys_user_grmember');


gr.addQuery('group.name','itil_group');   //   group to group.name and make sure the group name is correct with right case


gr.addQuery('user',current.variables.requested_for); // change from Requested_for to requested_for


gr.query();


if(gr.next())


{


gr.deleteRecord();


}




gs.addInfoMessage("Testing " + current.variables.requested_for.getDisplayValue() + " removed");




Please try and share the message if it is not getting deleted.



Hope this helps. Mark the answer as correct/helpful based on impact.



Thanks


Antin


View solution in original post

16 REPLIES 16

Shishir Srivast
Mega Sage

Please check if this helps.



var gr = new GlideRecord('u_training_courses');


//gr.addQuery('sys_id',current.variables.Requested_for);


gr.addQuery('active',true);


gr.query();


while(gr.next()){


gr.deleteRecord();


}



Note: Please use this carefully since you are trying to run the delete on all the record which are active true, you have to put some more query to get the specific records.


it has deleted other irrelavant records also


Your script does a query that returns all active records from "u_training_courses" table. Using a "while" loop to delete means you're going to delete all records returned.


What is the exact condition to match the records from "u_training_courses" table for deletion?


i want to delete only role allocated and requested _for user only in the above form