0

I have one text field name plannumber.

i want to compare Integer.valueof(plannumber__c) > Integer.valueof(plannumber__c). But when its giving wrong results when the textfield value plannumber__c is greater then 9.

For example it will make 9>10 and giving true.

Converting field type is not option at salesforce level what other alternative can I have at runtime to let it behave at number so that I can compare values.

    List<plan__c> planlist = acc.getSObjects('plans');
                if(planlist != null && planlist .size()>0){
                  recentplan = new plan__c();
                  recentplan.plannumber__c = '0';
                  for(plan__c tempplan : planlist){
                      if(tempplan.plannumber__c!= null && Integer.valueof(tempplan.plannumber__c)>Integer.valueof(tempplan.plannumber__c)){
                          recentplan = tempplan;
                      }
                  }

//process on the basic of tempplan
                  acc.value= recentplan .amount__c;

              }
  • how are comparing same field value Integer.valueof(plannumber__c)> Integer.valueof(plannumber__c) ? Is that Trigger.old and Trigger.new – Santanu Boral Mar 28 '17 at 20:53
  • Actually we have one logic to figure out record with highest value for plannumber__c field value, so we do like we initialize temp with zero and start comparing with next and so on. There is Trigger on Plan which run and calcuate on Trigger.new for all records and filter with highest value for plannumber__c – David Mycka Mar 28 '17 at 20:55
  • Can you post your code here – Santanu Boral Mar 28 '17 at 20:56
  • 1
    @DavidMycka This makes no sense as you are comparing the same value and it will never be great as it will always be equal. You are essentially doing '9'>'9' – Eric Mar 28 '17 at 21:16
5

You are wrongly comparing the values.

Currently it is: Integer.valueof(tempplan.plannumber__c)>Integer.valueof(tempplan.plannumber__c)

You should compare with tempOpp.plannumber__c and tempplan.plannumber__c. So put the conditions properly.

Example:

for(plan__c tempplan : planlist)
{
    if(tempplan.plannumber__c!= null 
        && Integer.valueof(tempOpp.plannumber__c)>Integer.valueof(tempplan.plannumber__c)){
                recentplan = tempplan;
    }
}

Now, verify how the conditions to be satisfied to make recentplan = tempplan;

  • Wouldnot still this compartion will fail when 9>10 will be consider true by salesforce. As for text it is true. plannumber__c is text value at salesforce level – David Mycka Mar 28 '17 at 21:08
  • 2
    It will not. @DavidMycka, your problem is that your code is comparing the value to ITSELF. Obviously, a number can never be greater than itself. – Sebastian Kessel Mar 28 '17 at 21:09
  • I just checked, it is working fine only again for 0 to 9. For double digits it is failing. You can check this developer console for any text value in salesforce – David Mycka Mar 28 '17 at 21:12
2

While your posted code does not make sense as you are comparing the same value the following should help in the case that your posted code is not actually what you are using

String a = '9';
string b = '10';

System.debug(('0' + a).right(2)>('0' + b).right(2)); //WORKS
System.debug(integer.valueOf(a) > integer.valueOf(b)); //WORKS

Essentially we are adding a 0 and then taking the right 2 characters to ensure we are always comparing a string containing 2 digits.

BUT, you do not need to do that as the second debug outputs correctly as well

This goes back to the initial problem, you are comparing the same values

Integer.valueof(tempplan.plannumber__c)>Integer.valueof(tempplan.plannumber__c)

will always evaluate to false as the are equal

  • Thanks Eric for help. Just one more thing, when we take any value text in salesforce it will come as string when we query in apex right?. – David Mycka Mar 28 '17 at 21:24
  • @DavidMycka - If it is a TEXT field then yes. When querying a field in SF it will be returned as the type the field is defined as – Eric Mar 28 '17 at 21:25
  • Thanks ERIC. You are awesome. I would give you a best answers but since Santanu gave first, i would give him. But you are awesome :) – David Mycka Mar 28 '17 at 21:27
  • @DavidMycka - Well what was the issue then? You said the other answer did not work? – Eric Mar 28 '17 at 21:31
  • i have look into my code now, since i am not getting right results when i running that. I have updated the code as per suggestion here. But still results are not comming right. But i am satisfied with answers here. I will update group here once i figure out root cause. – David Mycka Mar 28 '17 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.