Business Rule Scripting Help - Query other table and change current field

JWayner
Tera Contributor

I'm trying to set up a business rule to check if the data in a field called control_number in another table matches the current record number. If yes, then in the current record, I want to change the internal_validation_performed field to true. 

I've tried it as a before and after rule. I've selected update and insert. I can't seem to get it to work. I've added an info message throughout and no matter what, the info message is returned successfully (regardless if the query requirements are met), meaning the query is not working properly. Can anyone help?

 

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


var currentNumber = current.getValue('number'); 
var controlTests = new GlideRecord('x_hotm_orm_risk_registry');
controlTests.addQuery('control_number', currentNumber);
controlTests.query();

 

if (controlTests.next()) {
current.setValue('internal_validation_performed', true);
}

})(current, previous);

1 ACCEPTED SOLUTION

One other thing to consider...

What type of field is Number and what type of field is Control Number?  Are they both reference, is one a string and one a reference, etc?

 

This may be the reason the query isn't working...maybe you are getting a sys_id from a reference, but one of the fields is a string field which is text.  


Please mark this response as correct and/or helpful if it assisted you with your question.
Steven

View solution in original post

12 REPLIES 12

That was it! The control_number reference field references sys IDs. As soon as I changed number to sys_ID, it started working properly. Thank you so much!

Riya Verma
Kilo Sage
Kilo Sage

Hi @JWayner ,

 

Hope you are doing great . 

 

Use After Business rule , and try to use current.setWorkflow (false)  to avoid retriggering the BR.

 

(function executeRule(current, previous /*null when async*/) {
    var currentNumber = current.getValue('number');
    var controlTests = new GlideRecord('x_hotm_orm_risk_registry');
    controlTests.addQuery('control_number', currentNumber);
    controlTests.query();
    
    if (controlTests.next()) {
        current.setValue('internal_validation_performed', true);
        current.setWorkflow(false); // To prevent retriggering the business rule
        current.update(); // Save the changes to the current record
    }
})(current, previous);

 Please try using this updated script,

Please mark the appropriate response as correct answer and helpful, This may help other community users to follow correct solution.
Regards,
Riya Verma

Thanks for your quick response, Riya. Unfortunately, it's still not working. The only way I seem to be able to get anything to happen is by adding the source table as a variable and referencing it in the currentNumber variable. If I do this, the internal_validation_performed field becomes true in every record. 

 

(function executeRule(current, previous /*null when async*/) {
    var controlTable = new GlideRecord ('x_hotm_orm_control_registry');
    var currentNumber = controlTable.getValue('number');
    var controlTests = new GlideRecord('x_hotm_orm_risk_registry');
    controlTests.addQuery('control_number', currentNumber);
    controlTests.query();
    
    if (controlTests.next()) {
        current.setValue('internal_validation_performed', true);
        current.setWorkflow(false); // To prevent retriggering the business rule
        current.update(); // Save the changes to the current record
    }
})(current, previous);