Update Request (sc_request) using Before Business Rule on Request Item (sc_req_item)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-25-2022 06:41 AM
Hi Everyone,
I have a business rule that runs on the sc_req_item table before insert. It copies some variables from the request item to the parent request (sc_request) "requested for" field. I need to do this before insert so that this change is already in effect for notifications, etc.
I currently have only been able to get it to work by running asynchronously, because I need to reference the parent Request and update that on submission of the Request Item. Unfortunately, it's not working 100% of the time and I think this is because it's asynchronous. Can anyone else recommend an alternate approach?
(function executeRule(current, previous /*null when async*/) {
var pr = new GlideRecord('sc_request');
pr.get(current.request);
if(current.variables.ri2_requested_for){
if(current.variables.ri2_requested_for != current.variables.ri2_contact) {
pr.requested_for = current.variables.ri2_requested_for;
}
else if(current.variables.ri2_contact != current.opened_by) {
pr.requested_for = current.variables.ri2_contact;
}
pr.update();
}
else if(current.variables.requested_for){
pr.requested_for = current.variables.requested_for;
pr.update();
}
else if(current.variables.who_is_this_request_for) {
pr.requested_for = current.variables.who_is_this_request_for;
pr.update();
}
else if(current.variables.u_who_is_this_request_for) {
pr.requested_for = current.variables.u_who_is_this_request_for;
pr.update();
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-25-2022 06:49 AM
Any business rule that updates a foreign record should run as an after business rule (or an async one). So that part is good.
However in this case the problem is that when the Requested Item is inserted, the Request record does not yet exist. At least if it is a "normal" Catalog Item submission scenario and not one where an already existing Requested Item creates another sibling one, using API calls. So it would be better to move the Business Rule to the Request table, make it before one as it will update the current record.
But I do have an issue with this setup: Requests are so designed that they can contain any number of Requested Items. That raises the problem: no matter whether you copy from Request to Requested Item or the other way, from Requested Item to Request, which variables (belonging to which Requested Item) will you copy. Sure, in most cases your Requests will contain one Requested Items. But that is not by rule, but by coincidence. One day you will start using Order Guides and things will fall on their face.
I would reconsider this requirement and solve the original problem some other proper way.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-25-2022 06:54 AM
Also for the requested for copying problem, you might just as well use the Requested for variable type which will do the trick of copying the requested for user automatically.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-25-2022 07:02 AM
Hi Janos,
Thank you for your response. I had written a long response about why these requirements have forced me into this approach, but I think you have given me something to go off of with the OOTB 'Requested For' variable. I thought it was only available as an interim step before checkout, but I'm wondering if I can populate it using a onSubmit client script or something similar.
Thank you for your help and I will push us towards another solution that gets us closer to OOTB functionality.
Regards,
Matt
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-25-2022 08:11 AM
You're most welcome 🙂
Also going for OOB is always a good strategy, I always fight for that to happen when I'm asked for a solution.