- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2020 12:27 PM
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.
(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?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2020 12:59 PM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2020 12:40 PM
Hi Michael,
I do want this business rule to run on both insert and update. I disabled the "on update" and still saw 2 records inserted. I also added in the setWorkflow(false) line and still saw 2 inserts.
Not sure whats going on but thanks for the response.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2020 12:37 PM
The rule runs when Budget Key is empty and creates the new Budget Key record, but it never sets the Budget Key field to no longer be empty. So back-to-back updates will cause multiple inserts.
Assuming the Budget Key field is a reference to the u_budget_key table, try this:
1. Change the type to be BEFORE insert/update instead of after
2. change the line of code from
key.insert();
to
if(key.insert()){
current.u_budget_key = key.getUniqueValue();
}
This will verify the key actually got inserted, and set the Budget Key field so that it is no longer empty.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2020 12:39 PM
You can also do like this:
current.u_budget_key = key.insert();
because GlideRecord.insert() returns the sys_id of the newly created record, or false if it fails.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-14-2020 12:51 PM
So actually I may have spoke too soon.. I didn't realize that the sys_id would populate the budget key field. I actually want the budget key field to remain empty. Do you know of a work around for this?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2020 07:43 AM
I suppose I'm not understanding the larger business case at this point. My assumption was you wanted to create one, and exactly one Budget Key for every u_example record, and then associate that Budget Key with the u_example record that created it.
It sounds like you want to keep creating new Budget Keys at every update to a u_example, until someone or something else selects the Budget Key as a value on the Budget Key field?
If the latter is your desire, your code as-is should work, but something else is causing a double-update upon insert resulting in two budget keys. You could modify the Business Rule to only run on Update and see if that fixes it.
If you only want one Business Key created per u_example record, but still want to manually select it in the Business Key field on u_example, you could:
1. Add a field to the u_budget_key table that is a reference to the u_example table (you could call it "source")
2. Add code to your business rule that queries the u_budget_key table for one where the "source" u_example is the current.sys_id, and only create/insert a new one if the query returns nothing.