business rule abort action and gr.insert() returns another sys_id
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2016 01:32 AM
Hi,
In a business rule, I need to cancel an insert on some condition. If the condition is reached, it updates the values of another record. That's ok.
But I need the business rule to return the sys_id of the updated record, is anyone have a solution for this (you can't make a return on BR) ?
Because when the insert is made on other scripts, they get back the sys_id to make some operations on it.
For exemple, this is what I need :
- When an insert of new hardware is done from anywhere in the system : ID = new_hw.insert()
- If the insert matches the business rules, ID contains the sys_id of the updated record.
- If the insert doesn't match the conditions, ID contains the sys_id of the newly created asset.
Regards.
For info, the BR :
var mac_utils = new u_MacUtils();
var result_mac, result;
result_mac = mac_utils.validateMac(current.u_mac_address);
result = mac_utils.already_in_park_even_retired(current.company, current.u_mac_address);
if(result_mac[0] && result[0] == true) {
var old_hw = new GlideRecord('alm_hardware');
odl_hw.setWorkflow(false); // in case of the old HW is from another company
old_hw.get(result[1]);
old_hw.model = current.model;
old_hw.comments = current.comments;
old_hw.model_category = current.model_category;
old_hw.u_related_item = current.u_related_item;
old_hw.assigned_to = current.assigned_to;
old_hw.install_status = current.install_status;
old_hw.location = current.location;
old_hw.company = current.company;
old_hw.u_reseller = current.u_reseller;
old_hw.update();
gs.log("+++ Re using an hardware instead of creating a new one for : " + old_hw.assigned_to + " with mac : " + current.u_mac_address);
current.setAbortAction(true);
//return old_hw.sys_id;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2016 03:33 AM
What is not clear to me is what you mean by ID = new_hw.insert(). This specific item feels to be more of a script include function than a business rule.
A business rule is basically a database trigger which allows you to muck with the record on the path to the database. You have access to 'current' and 'previous' versions of the row that triggered the business rule. Though if you really want to keep some state around, there is g_scratchpad (see global variables in Scripting in Business Rules - ServiceNow Wiki ) .
I would probably recommend not using g_scratchpad. For example, what happens if discovery is running when your code is running and discovery triggers the business rule at the same time a user is making a change?
If you don't want current to change, the abort() will prevent the change from happening.
If you need the sys_id in the context of ID = new_hw.insert(), I'd say move your BR code into a script include and have the script include return the value. Modify the business rule above to call the script include and don't do anything with the return value from the script include. This way, when you are actively calling the validation code, you get sys_id back.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2016 05:33 AM
Hello, thanks for your reply.
Let me use an exemple :
var new_hw = new GlideRecord('alm_hardware');
var ID;
new_hw.initialize();
// here operations on values of the new record
ID = new_hw.insert()
var already_asset = new GlideRecord('u_alm_asset_price'); |
already_asset.addEncodedQuery('u_asset.sys_idSTARTSWITH'+ ID);
already_asset.query();
So I need the Business Rule intercept the insert (that's ok) , you understood with this example that insert() is standard GlideRecord insert , but I want the insert() to return the sys_id of the updated record instead of a new one.