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

Hi @Aman Kumar S yes it should be current.task , because the "task" is a field in the vtb_card , referencing the current vtb_task. 

Davedd90
Tera Contributor

No "task" is a field of card GlideRecord variable. There is no current in background script. If you are using this in business rule, then don't use gliderecord on vtb_card. Directly use

 

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();
}

ss123
Tera Contributor

There's still error in the current

SabrinaSalazar_0-1669706500142.png

vtb_card is a related list of vtb_task. and the reason is that I want to validate if the vtb_task has a value in the vtb_card related list

 

Thanks,

Sab

Davedd90
Tera Contributor

In background script, there is no current. So you have to go with card.task only. You have to keep using setWorkflow.

ss123
Tera Contributor

Well, it should be current.task , because the "task" is a field in the vtb_card , referencing the current vtb_task. I hope I explained it well. 🙂