Before business rule not updating field value based on referenced table

BijoyDeb
Tera Contributor

Hi all,

 

Our customer needs exact field named requestor department on catalog task and want department of requested for in that field.

 

I created one custom field referring to department table and was trying to update its value based on requested for.

As we know, that catalog task don't have requested for and that needs to be taken from request table, I have written same logic in my before business rule, but department is not getting populated though. 

Info logs are showing undefined 

 

Var gr = new GlideRecord('sc_request');

if(gr.get(current.request))

var depart = gr.requested_for.department;

 

current.u_requestor_department = depart

 

This code I have written in before insert update br on catalog task table.

 

Do I need to write after br for this?

1 ACCEPTED SOLUTION

Rajesh Chopade1
Mega Sage

hi @BijoyDeb 

Since you are trying to update the field based on values from another table, you should use an after Business Rule to ensure the record is saved and available for reading.

(function executeRule(current, previous /*null when async*/) {
    // Check if the current record is valid and has a request reference
    if (current.request) {
        var gr = new GlideRecord('sc_request');
        
        // Retrieve the sc_request record associated with the current task
        if (gr.get(current.request)) {
            // Check if the requested_for field is populated
            if (gr.requested_for) {
                var requestedFor = new GlideRecord('sys_user');
                
                // Retrieve the user record associated with requested_for
                if (requestedFor.get(gr.requested_for)) {
                    // Update the custom field with the department of the requested_for user
                    current.u_requestor_department = requestedFor.department;
                } else {
                    gs.info('User record not found for requested_for: ' + gr.requested_for);
                }
            } else {
                gs.info('Requested_for field is empty in request: ' + current.request);
            }
        } else {
            gs.info('Request record not found: ' + current.request);
        }
    } else {
        gs.info('Request reference is empty on catalog task: ' + current.sys_id);
    }
})(current, previous);

 

I hope my answer helps you to resolve the issue if yes please mark my answer helpful and correct.

thank you

rajesh

View solution in original post

3 REPLIES 3

Gangadhar Ravi
Giga Sage
Giga Sage

@BijoyDeb 

 

Have you tried with set values section of business rule ? you may not need script for this.

Yeah, did that!

But that as well didn't work

Rajesh Chopade1
Mega Sage

hi @BijoyDeb 

Since you are trying to update the field based on values from another table, you should use an after Business Rule to ensure the record is saved and available for reading.

(function executeRule(current, previous /*null when async*/) {
    // Check if the current record is valid and has a request reference
    if (current.request) {
        var gr = new GlideRecord('sc_request');
        
        // Retrieve the sc_request record associated with the current task
        if (gr.get(current.request)) {
            // Check if the requested_for field is populated
            if (gr.requested_for) {
                var requestedFor = new GlideRecord('sys_user');
                
                // Retrieve the user record associated with requested_for
                if (requestedFor.get(gr.requested_for)) {
                    // Update the custom field with the department of the requested_for user
                    current.u_requestor_department = requestedFor.department;
                } else {
                    gs.info('User record not found for requested_for: ' + gr.requested_for);
                }
            } else {
                gs.info('Requested_for field is empty in request: ' + current.request);
            }
        } else {
            gs.info('Request record not found: ' + current.request);
        }
    } else {
        gs.info('Request reference is empty on catalog task: ' + current.sys_id);
    }
})(current, previous);

 

I hope my answer helps you to resolve the issue if yes please mark my answer helpful and correct.

thank you

rajesh