The CreatorCon Call for Content is officially open! Get started here.

Compare Drop down and String Value

Pankaj15
Tera Contributor

Hi Team,

 

Create a 2 fields in the Custom table:

Risk Level: choice[Critical, High, Medium and Low]

Barr Level: String Only set Value[Critical, High, Medium and Low]

 

Requirement: compare the Barr Level  is Higher then the Risk Level.

 

 

 

1 ACCEPTED SOLUTION

Maddysunil
Kilo Sage

@Pankaj15 

you can use a Business Rule or a Script Include to compare the values of the "Risk Level" and "Barr Level" fields whenever a record is inserted or updated in the custom table.

 

 

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

    // Get the values of Risk Level and Barr Level
    var riskLevel = current.getValue('risk_level');
    var barrLevel = current.getValue('barr_level');

    // Map the text values to their corresponding numerical values
    var riskLevelValue = getNumericValue(riskLevel);
    var barrLevelValue = getNumericValue(barrLevel);

    // Compare the values
    if (barrLevelValue < riskLevelValue) {
        // Barr Level is lower than Risk Level
        gs.addErrorMessage("Barr Level cannot be lower than Risk Level.");
        current.setAbortAction(true); // Abort the transaction
    }

    // Function to map text values to numerical values
    function getNumericValue(level) {
        switch (level) {
            case 'Critical':
                return 4;
            case 'High':
                return 3;
            case 'Medium':
                return 2;
            case 'Low':
                return 1;
            default:
                return 0; // Default value or error handling
        }
    }

})(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

View solution in original post

6 REPLIES 6

RikV
Tera Expert

- Create a new field to display the result (higher, lower, same).

- Open the dictionary for the created field. Press 'Advanced view' from the Related Links if not already in advanced view. 

- Go to the calculated value tab and check the 'Calculated Value' checkbox. Put in the following script:

 

 

(function calculatedFieldValue(current) {
    var ranking = {
        'Low': 1,
        'Medium': 2,
        'High': 3,
        'Critical': 4
    };

    var field1Rank = ranking[current.getDisplayValue('field1')];
    var field2Rank = ranking[current.getDisplayValue('field2')];

    if (field2Rank > field1Rank) {
        return 'Field 2 is higher than Field 1';
    } else if (field2Rank < field1Rank) {
        return 'Field 2 is lower than Field 1';
    } else {
        return 'Field 2 is the same as Field 1';
    }
})(current);

 

 

Your newly created field will now hold a string value saying if field2 is higher, lower or the same as field1.

 

You can also just make it a true/false field of course and call it Higher: true/false. 

Maddysunil
Kilo Sage

@Pankaj15 

you can use a Business Rule or a Script Include to compare the values of the "Risk Level" and "Barr Level" fields whenever a record is inserted or updated in the custom table.

 

 

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

    // Get the values of Risk Level and Barr Level
    var riskLevel = current.getValue('risk_level');
    var barrLevel = current.getValue('barr_level');

    // Map the text values to their corresponding numerical values
    var riskLevelValue = getNumericValue(riskLevel);
    var barrLevelValue = getNumericValue(barrLevel);

    // Compare the values
    if (barrLevelValue < riskLevelValue) {
        // Barr Level is lower than Risk Level
        gs.addErrorMessage("Barr Level cannot be lower than Risk Level.");
        current.setAbortAction(true); // Abort the transaction
    }

    // Function to map text values to numerical values
    function getNumericValue(level) {
        switch (level) {
            case 'Critical':
                return 4;
            case 'High':
                return 3;
            case 'Medium':
                return 2;
            case 'Low':
                return 1;
            default:
                return 0; // Default value or error handling
        }
    }

})(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