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 11:30 AM
Hi Chrisn,
You can use addOrCondition() in a script to filter the records. Please refer below link for reference with example.
https://developer.servicenow.com/app.do#!/api_doc?v=madrid&id=GQC-addOrCondition_S_S_O
-Pradeep Sharma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-11-2019 11:40 AM
var gr = new GlideRecord("cost_plan");
gr.addQuery("task", current.sys_id);
gr.query();
while(gr.next()){
if ((gr.u_funding_approval_state == '1') || (gr.u_funding_approval_state == '4'))
{
gr.u_funding_approval_state = current.u_funding_approval_state;
gr.update();
}
}
Vinod Kumar Kachineni
Community Rising Star 2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-11-2019 11:52 AM
Hello vkachineni,
That is very close to what I managed to get working before I saw your update. This is working for me.
(function executeRule(current, previous /*null when async*/) {
var gr = new GlideRecord("cost_plan");
gr.addQuery("task", current.sys_id);
gr.query();
while(gr.next()){
if (gr.u_funding_approval_state == ('1' || '4'))
//gs.addErrorMessage('BR is executing');
gr.u_funding_approval_state = current.u_funding_approval_state;
gr.update();
}
})(current, previous);
I was hoping if you have one more moment, if you could take a look at another script.
This business rule is trying to go reverse of what the above did. The above executed from a project and affected all of it's down stream cost plans. I am trying to make a business rule that will change the funding state of the project when the cost plan's state is changed to a 4.
(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);
Currently it doesn't appear to be working. I believe it might be the addquery portion, any thoughts?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-11-2019 11:58 AM
Hi,
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();
}
try the above one
Mark it correct/helpful if it helps you
Regards
Ravi