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

jMarshal
Mega Sage
Mega Sage

I think you need to add "current.update();" in your if statement (after setting the value)...if that doesn't work maybe also try assigning the value directly "current.internal_validation_performed = true;" instead of using setValue.

JWayner
Tera Contributor

One step closer. I applied this and nothing happened, but when I tried to create a new variable denoting the original table, it started making every record's current.internal_validation_performed field true. So the value change is working now, but I think the query is still off. This is what I tried:

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


var controlRecords = new GlideRecord ('x_hotm_orm_control_registry');
var currentNumber = controlRecords.getValue('number'); //i thought maybe I needed to denote that the number field it's searching for is on the original table and not the control test table because that also has a number field generated by the system.
var controlTests = new GlideRecord('x_hotm_orm_risk_registry');
controlTests.addQuery('control_number', currentNumber);
controlTests.query();

if (controlTests.next()) {//if a record is returned
current.internal_validation_performed = true;
}

})(current, previous);

OK, so the query is picking up all records in the custom table, not just those which match the controlNum...

try setting the variable in 2 steps.

var currentNumber;

currentNumber = controlRecords.getValue('number');

...and you're sure the field on the risk registry table is called "control_number" ? (just checking, you have a few "controls" and "numbers")

JWayner
Tera Contributor

I tried this and it is doing the same thing by picking up all the records. Also I verified that the field name and risk registry table name are correct. 

 

(function executeRule(current, previous /*null when async*/) {
   var controlRecords = new GlideRecord ('x_hotm_orm_control_registry');
   var currentNumber;
   currentNumber = controlRecords.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);