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

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



Hi @Amit Gujarathi 

 

Thank you very much for prompt response.

Can I  run it on "before"?

Appreciate your help!

Amit Gujarathi
Giga Sage
Giga Sage

Yes


Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Thanks @Amit Gujarathi 

But It's working exactly opposite, seems strange.

Country field (reference) has 4 records, in which 2 records contains "India" and another 2 doesn't contain.

When contain "India" selected, user2 is being updated and India doesn't contain updating User1

Thanks again!