if statement in business rule

chrisn_
Mega Guru

Hello everyone,

I am a bit rushed and having trouble modifying this script to behave the way I want.

I have a business rule that I need to query the cost plan table which works, and set the custom funding state field to a certain value. The issue is I only want this to change cost plans that have a state value of 1 or 4. Can someone please help me take this across the finish line with the if statement? Does this look correct?

var gr = new GlideRecord("cost_plan");
gr.addQuery("task", current.sys_id);
gr.query();
while(gr.next()){

if (gr.u_funding_approval_state == ('1' && '2');
//gs.addErrorMessage('BR is executing');
gr.u_funding_approval_state = current.u_funding_approval_state;
gr.update();
}

8 REPLIES 8

For the first BR I would recommend using separate conditions

if ((gr.u_funding_approval_state == '1') ||  (gr.u_funding_approval_state == '4'))

For the second script

As @Ravi T mentioned should work.

if (gr.u_funding_approval_state.toString() == '2')

Try this??

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


var gr = new GlideRecord("pm_project");
gr.addQuery("task", current.sys_id);
gr.query();
while(gr.next()){
if (gr.u_funding_approval_state == 2){
//gs.addErrorMessage('BR is executing');
gr.u_funding_approval_state = 4;
gr.update();

}
}


})(current, previous);

paulmorris
Giga Sage

As Pradeep has wisely suggested, it is much more efficient to do the IF statement check from your GlideRecord query as it will greatly reduce the number of records returned.

Example below:

var gr = new GlideRecord("cost_plan");
gr.addQuery("task", current.sys_id);
gr.addQuery("u_funding_approval_state","IN","1,2"); //is one of
gr.query();
while(gr.next()){
   gr.u_funding_approval_state = current.u_funding_approval_state;
   gr.update();
}

As others have mentioned, your syntax for if statements are not correct.

For future reference, 'vkachineni' has provided the correct syntax:

if ((gr.u_funding_approval_state == '1') ||  (gr.u_funding_approval_state == '4'))

See https://www.w3schools.com/jsref/jsref_if.asp for a good article on IF statements.

 

However, in this instance, the most correct solution is to include this in your query as shown above with the IN.

Totally agree with Paul and Pradeep. The filter should be applied on the gliderecord. Overlooked that as the focus was on fixing the if stmt. Excellent learning point.