change field values

hadron_collider
Tera Contributor

Hello,

 

 

I was wondering if this is correct to use a business rule to change the field value of records if a record from another table changes to "approved"

 

 

So the "Expense App" record (contains data like requestor, status, year, month) can be approved by a manager. When this happens UI Policy is triggers that changes the status to "approved" . Below you can see records that come from another table, they as well have fields like requestor, status, year and month. I want the status field to change for all records that have the same requestor, month and year.

kata90_0-1704211476342.png

 

 

here is my business rule however it does not work:

 

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

    // Check if the status of the record is changed to 'approved'
    if (current.status == 'approved') {

        gs.info("Business Rule Triggered for record: " + current.sys_id);

        // Retrieve the month, year, and requestor from the current record
        var month = current.month;
        var year = current.year;
        var requestor = current.requestor;

        // Query the 'x_aspas_expense_ap_expenses_records' table for matching records
        var gr = new GlideRecord('x_aspas_expense_ap_expenses_records');
        gr.addQuery('month', month);
        gr.addQuery('year', year);
        gr.addQuery('requestor', requestor);
        gr.addQuery('status', 'IN', 'pending,requested');
        gr.query();

        // Iterate over the matching records and update their status to 'approved'
        while (gr.next()) {
            gs.info("Updating record: " + gr.sys_id);
            gr.status = 'approved';
            gr.update();
        }
    }

})(current, previous);
 
 
 
kata90_1-1704211787284.png

 

 

 

any tips on how to make it work??

 
4 REPLIES 4

Danish Bhairag2
Tera Sage
Tera Sage

Hi @hadron_collider ,

 

Are you getting this info message?

gs.info("Updating record: " + gr.sys_id);

Once the BR is executed or is the BR getting executed?

 

Thanks,

Danish

 

Stefan Georgiev
Tera Guru

Hello @hadron_collider ,

your script logic looks fine, but you might have problems with the cross-scope problem, is the table that you are triggering the business rule in the same scope as the table that you want to update the records?

 

I hope that this helps you!

If the provided information answers your question, please consider marking it as Helpful and Accepting the Solution so other community users can find it faster.

All the Best,
Stefan

Hello,

 

 

Thanks for looking into this. 

Indeed, both tables are in the same scope

kata90_0-1704271637035.png

any further suggestion what can be done to make it work?

Hi @hadron_collider ,


Are you using an admin user, event then there might be an ACL preventing you from doing that, or a data policy.
You can test the script in a background script to check if the updates are executed. If they are done there might be another business rule or something else updating them back.

Hope that this helps you!

If the provided information answers your question, please consider marking it as Helpful and Accepting the Solution so other community users can find it faster.

All the Best,
Stefan