Background script (it should not create new records)

ss123
Tera Contributor

Hi! I'm having a hard time fixing this background script below. The script is working however, it creates new records in the vtb_task table. Am I missing something? Thank you in advance 🙂

 

var current = new GlideRecord('vtb_card');
current.query();
while (current.next()){
var vtbtask = new GlideRecord('vtb_task');
vtbtask.addQuery('sys_id', current.task);
vtbtask.query();
if (vtbtask.next()) {
vtbtask.setValue('u_choice_2', 'no');

} else {
vtbtask.setValue('u_choice_2', 'yes');
}
vtbtask.update();
}

19 REPLIES 19

Just remove the else section for it, it will just update the record as No for this field u_choice_2

Best Regards
Aman Kumar

Sagar Pagar
Tera Patron

Hi, 

Try to add

vtbtask.setWorkflow (false);

 

Before vtbtask.update (); line.

 

Thanks,

Sagar Pagar 

The world works with ServiceNow

Hi @Sagar Pagar , added the this vtbtask.setWorkflow (false); before vtbtask.update (); line, but still creates new records.

 

Thanks,

Sab

Hi @ss123,

it should be w/o space in between setWorkflow and (false); 

 

vtbtask.setWorkflow(false); 

 

Thanks,

Sagar Pagar

The world works with ServiceNow

Hi @Sagar Pagar 

This is the updated script, result was, it just inserts records with u_choice_2 field = Yes 

var current = new GlideRecord('vtb_card');
current.query();
while (current.next()){
var vtbtask = new GlideRecord('vtb_task');
vtbtask.addQuery('sys_id', current.task);
vtbtask.query();
if (vtbtask.next()) {
vtbtask.setValue('u_choice_2', 'no');

} else {
vtbtask.setValue('u_choice_2', 'yes');
}
vtbtask.setWorkflow(false);
vtbtask.update();
}

 

Thanks,

Sab