The CreatorCon Call for Content is officially open! Get started here.

Request Business Rule Pre Insert Script

awibb
Tera Contributor

Hello so we have an email that is being sent to the user(s) in the requested_for and opened_by field on a request when its inserted into the sc_request table. The default behavior appears to be, when a user submits a catalog item the value for both variables is the submitting users sys_id. In our use case on the catalog item the user can pick others users I.E. if I(user A) open the catalog item I can select user B as the user. So I want to have my business rule override the value in requested_for with the user selected in the catalog item before its inserted into the sc_request table. I was able to do so with the following script but ran into a problem when trying to do the same for the RITM associated with the request. TLDR the value for requested_for in the request is now correct but the value for requested_for in the RITM is not correct. I checked in the debugger and both are getting set correctly so not sure what the issue is here.

 

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

    // Add your code here
    var ritm = new GlideRecord('sc_req_item');
    ritm.addQuery('request', current.sys_id);
    ritm.query();
    if(ritm.next()){
        // gs.info(ritm.variables.user);
        current.setValue('requested_for', ritm.variables.user);
        ritm.setValue('requested_for', ritm.variables.user);
       
    }

})(current, previous);
1 ACCEPTED SOLUTION

Ethan Davies
Mega Sage
Mega Sage

You are setting the value of a record in another table, you will need to use .update() for your changes to be reflected.

 

ritm.setValue('requested_for', ritm.variables.user);
ritm.update();

 

I assume it works for the sc_request currently because your Business Rule is running onBefore, which means you don't need to use .update for current.setValue().

View solution in original post

1 REPLY 1

Ethan Davies
Mega Sage
Mega Sage

You are setting the value of a record in another table, you will need to use .update() for your changes to be reflected.

 

ritm.setValue('requested_for', ritm.variables.user);
ritm.update();

 

I assume it works for the sc_request currently because your Business Rule is running onBefore, which means you don't need to use .update for current.setValue().