I have written a script to stop the deletion of ci, it is not working

Sujata Lenka
Tera Contributor

 

I have created  a before delete business rule in cmdb_ci table and written the below script, but it is not stopping the deletion of ci if ci is linked to incident table, please help.

 

var gr = new GlideRecord('Task');
gr.addQuery('cmdb_ci', current.sys_id);
gr.query();
if (gr.hasNext()) {
current.setAbortAction(true);
}

2 REPLIES 2

Ramz
Mega Sage

Hi @Sujata Lenka ,

If its for only incident table you can gliderecord incident table alone.Also if you are using Task table convert the table name to lower case.

Try this:

 

var gr = new GlideRecord('incident');
gr.addQuery('cmdb_ci', current.sys_id);
gr.query();
if (gr.next()) {
current.setAbortAction(true);
}

 

Please mark my answer correct/helpful if it resolved your query.

Ahmmed Ali
Mega Sage

Hello @Sujata Lenka 

 

The table name is just task, you have capital T.

Below updated code:

var gr = new GlideRecord('task');
    gr.addQuery('cmdb_ci', current.sys_id);
    gr.setLimit(1);
    gr.query();
    if (gr.hasNext()) {
        gs.addErrorMessage("This CI is associated with one or more tasks!");
        current.setAbortAction(true);
    }
 
Thank you,
Ali
 
If I could help you with your Query then, please hit the Thumb Icon and mark my answer as Correct!!

Thank you,
Ali