The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Business Rule

yoli1
Tera Contributor

Hi,

I'm trying to write an "if" block to check for some values. I have got more values to verify in the if statement of a Business rule. The script is working fine. But I'm trying to find easy and best practice to achieve this. 

if(current.u_level_3.name=='IT Service Systems'||current.u_level_3.name=='Marketing'||current.u_level_3.name=='Sales'){
		//
	}
11 REPLIES 11

Syed30
Tera Expert

 

Hello @yoli1 ,

You can simplify by using the include keyword. This keyword allows you to check if a value exists in the array.

 

 

const validValues = ['IT Service Systems', 'Marketing', 'Sales'];

if (validValues.includes(current.u_level_3.name)) {
    // Your code here
}

 

 

 

Aylee Andersen
Kilo Sage

If you're just looking for a shorter way to write this if-statement, you could try the following:

if (["IT Service Systems", "Marketing", "Sales"].indexOf(current.u_level_3.name !== -1) {}

You just list all the options in the array and make sure that current.u_level_3.name is one of the items in that list.

 

Alternatively (and probably best practice), you could build that condition out in the "When to run" tab of the Business Rule. Of course that's assuming it's possible for you to do that in this scenario.

Hello @Aylee Andersen  , should in the "When to run" tab of the Business Rule in the condition choose the 'changes to' then put the values ? and why would you consider that to be best practice?

 

Thanks

I apologize, I didn't see @Syed30's comment earlier but it looks like we were thinking along the same lines! Feel free to mark their solution as correct if that's what you end up going with.

 

For the "When to run" tab's "Filter conditions", I would use "is" instead of "changes to" since the BR isn't going to capture a dot walked field changing. Using "is" would also match what your script is doing currently. The reason I consider this best practice is because it utilizes the no/low code model that ServiceNow tries to push for. But if it makes more sense to script the condition for your use case, go for it!