How to consider "Contains" in the Business rule to update a field value based on multiple conditions

Ksnow
Tera Contributor

Hi all,

 

I was looking a script in business rule to update a field on the form based on the multiple conditions like below:

-> Field "Country" contains  value as "India" then update a field "User" with multiple field values (/FieldA/FieldB/FieldC/)

-> Here combinational query If "Country" does not contains value "India" and field "City" has a value "Pune", then update "User2" field with (/FieldD/FieldE/FieldF/)

2 more conditions are included as above (combination).

Could you please help me on it? How to consider contains in BR in if condition.

 

Thanks,

1 ACCEPTED SOLUTION

Amit Gujarathi
Giga Sage
Giga Sage

Hi @Ksnow ,
I trust you are doing great.
please find the below reference code

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

    // Check if Country contains 'India'
    if (current.country.indexOf('India') > -1) {
        // Update User field with concatenated values
        current.user = current.fieldA + '/' + current.fieldB + '/' + current.fieldC;
    }
    // Check if Country does not contain 'India' and City is 'Pune'
    else if (current.country.indexOf('India') == -1 && current.city == 'Pune') {
        // Update User2 field with concatenated values
        current.user2 = current.fieldD + '/' + current.fieldE + '/' + current.fieldF;
    }

    // Add additional conditions here following the same pattern

    // Commit the changes
    current.update();

})(current, previous);

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



View solution in original post

8 REPLIES 8

Ksnow
Tera Contributor

Never mind. it's working fine now.

THanks 

Ankur Bawiskar
Tera Patron
Tera Patron

@Ksnow 

did you try to use filter conditions instead?

You can avoid scripting

Any challenge you faced using filter conditions in BR?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar 

 

How to check multiple conditions using the filter and update the field accordingly?

 

Thanks

@Ksnow 

simply dot walk to fields you require and use the operator

did you start on this? please share screenshots.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader