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

Matt B4
Tera Contributor

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? 

 find_real_file.png

(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);

 

5 REPLIES 5

aa24
Kilo Contributor

I used business rule before insert table sc_req_item , then I used GlideRecord to query table sc_req with sys_id is current.request, I can not found any record . May be record of table sc_req was not created before sc_req_item. After that I create business rule before insert table

sc_req and it works

 

(function executeRule(current, previous /*null when async*/ ) {
    var req_id = current.sys_id;
    var gr_request_item = new GlideRecord('sc_req_item');
    var user_name = ''; // user_name table sys_user
    var request_for_id = '';
    gr_request_item.addQuery('request', req_id);
    gr_request_item.query();
    while (gr_request_item.next()) {
        caller_id = gr_request_item.variables.user_name ;
        if (!gs.nil(caller_id)) {
            var gr_user = new GlideRecord('sys_user');
            gr_user.addQuery('user_name', user_name );
            gr_user.query();
            while (gr_user.next()) {
                request_for_id = gr_user.sys_id.toString();
            }
            if (!gs.nil(request_for_id)) {
                gr_request_item.requested_for = request_for_id;
                gr_request_item.update();
            }
        }
    }
    current.requested_for = request_for_id;
})(current, previous);