function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
aaryan sriaaryan sri 

How to write IF condition on String value

Hi All,

Based on condition, value should store on sring. Present formula writtens below way

totalBusinessDistance = String.valueOf((Decimal)ar.get('totalBusinessDistance')-(2*(Decimal)ar.get('totalCommutingMiles')));
 Presently  totalBusinessDistnece storing value of totalBusinessDistance- 2*totalCommutingMiles.  Now want to be if (totalBusinessDistance- 2*totalCommutingMiles) value comes below 0 then totalBusinessDistance store only 0 other wise it  should store (totalBusinessDistance- 2*totalCommutingMiles).

Amit Chaudhary 8Amit Chaudhary 8
Please try below code that will help you
totalBusinessDistance = String.valueOf((Decimal)ar.get('totalBusinessDistance')-(2*(Decimal)ar.get('totalCommutingMiles')));
if(totalBusinessDistance < 0 )
 totalBusinessDistance = 0 ;

Please let us know if this will help you
aaryan sriaaryan sri
Hi Amit,
 As per your code checking totalBusinessDistance < 0 if it's yes making totalBusinessDistance=0 what about if condition false. What is the final value stores into totalBusinessDistance field .
Deepak GulianDeepak Gulian
According to current formula totalBusinessDistance storing String value, so in IF condition we can't do comparison with integer
so the formula should be this

Store the output in Integer

Integer ttlBusHr = (Decimal)ar.get('totalBusinessDistance')-(2*(Decimal)ar.get('totalCommutingMiles'));
if( ttlBusHr < 0 ) {
totalBusinessDistance = '0' ;
totalBusinessDistance = String.valueOf(ttlBusHr);
aaryan sriaaryan sri
Hi Deepak,

Planning to write this condition in below way, Please correct me if wrong

totalBusinessDistance = String.valueOf(((Decimal)ar.get('totalBusinessDistance')-(2*(Decimal)ar.get('totalCommutingMiles')))<0?