if condition to check if currency value greater than or lesser than shows incorrect result

farci
Mega Expert

Hi,

I have written a business rule to compare two currency field values and which ever is lesser among them, should be used to set a value of another field.

Total Credit Amount is compared against 

Below is the script that I am using in an on before, business rule.

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

var maxcap = current.eli_cre_amt.getCurrencyValue();
var totalcredit = current.tc_a.getCurrencyValue();
if (totalcredit < maxcap){
current.app_credit.setDisplayValue(totalcredit);
gs.addInfoMessage(totalcredit);
}
else
{
current.app_credit.setDisplayValue(maxcap);
gs.addInfoMessage(maxcap);
}

The issue is, it is setting the Maximum credit amount instead of Total Credit amount. (here total credit amount is lesser than maximum credit amount) see the screenshot attached.

Regards,
Narmi


})(current, previous);

 

1 ACCEPTED SOLUTION

Dennis R
Tera Guru

The issue is that getCurrencyValue() returns a string, so you are doing a lexical comparison. In other words:

 

"4.63" < "7.24" because four lexically comes before seven. However,

"14.63" < "7.24" because one lexically comes before seven. It never gets to the second digit.

 

To fix this, you need to convert the value string into a number. You can do it like this:

 

(function executeRule(current, previous /*null when async*/) {
    var maxcap = parseFloat(current.eli_cre_amt.getCurrencyValue());
    var totalcredit = parseFloat(current.tc_a.getCurrencyValue());
    
    if (totalcredit < maxcap) {
        current.app_credit.setDisplayValue(totalcredit);
        gs.addInfoMessage(totalcredit);
    }
    else {
        current.app_credit.setDisplayValue(maxcap);
        gs.addInfoMessage(maxcap);
    }
})();

 

Hope this helps,

--Dennis R

View solution in original post

2 REPLIES 2

Dennis R
Tera Guru

The issue is that getCurrencyValue() returns a string, so you are doing a lexical comparison. In other words:

 

"4.63" < "7.24" because four lexically comes before seven. However,

"14.63" < "7.24" because one lexically comes before seven. It never gets to the second digit.

 

To fix this, you need to convert the value string into a number. You can do it like this:

 

(function executeRule(current, previous /*null when async*/) {
    var maxcap = parseFloat(current.eli_cre_amt.getCurrencyValue());
    var totalcredit = parseFloat(current.tc_a.getCurrencyValue());
    
    if (totalcredit < maxcap) {
        current.app_credit.setDisplayValue(totalcredit);
        gs.addInfoMessage(totalcredit);
    }
    else {
        current.app_credit.setDisplayValue(maxcap);
        gs.addInfoMessage(maxcap);
    }
})();

 

Hope this helps,

--Dennis R

Thank You, Dennis.

 

Works brilliantly!

 

Regards,
Narmi