if statement in business rule
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-11-2019 11:10 AM
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();
}
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-11-2019 12:01 PM
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')
Vinod Kumar Kachineni
Community Rising Star 2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-11-2019 02:59 PM
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);
Please mark my response as correct and helpful if it helped solved your question.
-Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-11-2019 05:52 PM
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.
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-11-2019 08:12 PM
Vinod Kumar Kachineni
Community Rising Star 2022