Before Business Rule

B_pppppp
Tera Contributor

I have 3 fields (reference to the cmdb_ci_service table). Field 1 is the parent of field 2. The second is the parent of the third.  (field 2 parent of the field 3)

I need a business rule that gives an error if:

- all fields are empty

- if only the first field is not empty, but the other two are empty

- if the first two are not empty but the third is empty even though there are values ​​that could be chosen 

If the first two fields are not empty, and the third is empty because there is no value that can be chosen, "That's OK" should be displayed. 

Could you help me with this?

3 REPLIES 3

Shivani M S
Kilo Guru

Hi @B_pppppp .

 

Please find the before insert/update business rule along with condition when to run (test1 is empty or test2 is empty or test3 is empty)

Script:

(function executeRule(current, previous /*null when async*/ ) {
    if ((current.u_test1 == '' && current.u_test_2 == '' && current.u_test_3 == '') ||
        (current.u_test1 != '' && current.u_test_2 == '' && current.u_test_3 == '') ||
        (current.u_test1 != '' && current.u_test_2 != '' && current.u_test_3 == '' && hasValue() == 'Yes')) {
        gs.addErrorMessage("You cannot submit the form as test fields are empty");
        current.setAbortAction(true);
    }

    function hasValue() {
        var cl = new GlideRecord('sys_choice');
        cl.addEncodedQuery('name=cmdb_ci_service^element=u_test_3');
        cl.addQuery('dependent_value', current.u_test_2);
        cl.query();
        while (cl.next()) {
            return 'Yes';
        }
    }
})(current, previous);
 
Regards,
Shivani

Maddysunil
Kilo Sage

@B_pppppp 

Specify the conditions under which the Business Rule should execute. In this case, you want the rule to trigger when any of the three fields are modified.

 

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

    var field1Value = current.field1;
    var field2Value = current.field2;
    var field3Value = current.field3;

    if (!field1Value && !field2Value && !field3Value) {
        gs.addErrorMessage("All fields are empty. Please fill in at least one field.");
    } else if (field1Value && !field2Value && !field3Value) {
        gs.addErrorMessage("Field 1 is filled, but fields 2 and 3 are empty. Please fill in the dependent fields.");
    } else if (field1Value && field2Value && !field3Value) {
        // You need to implement logic to check if there are values available for field3
        // If there are no values, display "That's OK"
        gs.addErrorMessage("Field 1 and Field 2 are filled, but Field 3 is empty. Please select a value for Field 3.");
    } else {
        gs.addInfoMessage("That's OK"); // No error condition met
    }

})(current, previous);

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

Deepak Shaerma
Kilo Sage

Hi @B_pppppp 
You need to use before business rule and tick on both insert and update. Leave condition as blank just go for script in advanced.

 var field1 = current.field1.toString(); //replace field1 after current with the actual name of field
    var field2 = current.field2.toString(); //replace field2 after current with the actual name of field
    var field3 = current.field3.toString(); //replace field3 after current with the actual name of field

    // Check for all fields empty
    if (!field1 && !field2 && !field3) {
        gs.addErrorMessage('All fields cannot be empty.');
        // prevent record from being saved
        current.setAbortAction(true);
    }
    // Check if only the first field is not empty
    else if (field1 && !field2 && !field3) {
        gs.addErrorMessage('Field 2 and Field 3 cannot be empty when Field 1 is selected.');
        current.setAbortAction(true);
    }
    // check if first two are not empty but the third is
    else if (field1 && field2 && !field3) {
        // Query to check if field 3 has possible selections based on field 2
        var childOptions = new GlideRecord('cmdb_ci_service');
        childOptions.addQuery('parent', field2);  // Assuming ‘parent’ is the field you use for hierarchy
        childOptions.query();
        if (childOptions.hasNext()) {
            // there are valid options for field 3 but none is selected
            gs.addErrorMessage('Please select a value for Field 3.');
            current.setAbortAction(true);
        } else {
            // There are no valid options; therefore, this setup is acceptable
            gs.addInfoMessage("That’s OK");
        }
    }



Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards 
Deepak Sharma