Business Rule Inserting 2 Records - Only want to Insert 1 Record

Carter1
Giga Guru

Hi all,

I am writing an after insert/update business rule on a table (u_example) that is creating a record on another table (u_budget_key). This business rule is running only when my field, Budget Key is empty.

find_real_file.png

 

(function executeRule(current, previous /*null when async*/) {

var key = new GlideRecord('u_budget_key');
	key.initialize();
	key.u_gl = current.u_gl;
	key.u_vendor = current.u_vendor;
	key.u_cc = current.u_cc;
	key.insert();


})(current, previous);

The script is working, but instead of only inserting one record, there are 2 duplicate records added to the other table. Can anyone help me diagnose why?

1 ACCEPTED SOLUTION

Great to know it worked.

Also, what I meant was condition not necessarily has to be the same check if there is something that runs on update & when it runs for the record which it runs for has Budget Key empty or not.

In addition you could also check below once that runs after insert/update

(function executeRule(current, previous /*null when async*/) {

var key = new GlideRecord('u_budget_key');
key.addQuery('u_gl',current.u_gl);
key.addQuery('u_vendor',current.u_vendor);
key.addQuery('u_cc',current.u_cc);
key.query();
if(!key.next()) //if not then insert
{
	key.initialize();
	key.u_gl = current.u_gl;
	key.u_vendor = current.u_vendor;
	key.u_cc = current.u_cc;
	key.insert();
}

})(current, previous);

View solution in original post

21 REPLIES 21

Sounds Good 🙂 

Regards,
Muhammad

I think you are still looking for a solution so as I mentioned earlier, you can add additional AND condition that will not effect your actual logic but may be helps you with stopping the loop. 

Adding Budget Key [Changes] in addition to Budget Key [is empty] can help you. 

Regards,
Muhammad

Jaspal Singh
Mega Patron
Mega Patron

Hi Carter,

 

Try changing the business rule to run before insert/update rather than after. As I guess there may be some other business rule that runs on update when Budget key | is empty

Hi Jaspal,

I changed it to run before insert/update and there were still 2 records inserted into my other table.

In that case can you check if there exists some other Business rule that runs on the same table when Budget Key | is empty