3

I have a requirement where I need to check if Revision_Number__c number field is blank. If its blank than assign 1 else increment by 1. Following is what I tried.

    if(quoteLink.Revision_Number__c.isBlank)
    {
        quoteLink.Revision_Number__c=1;        
    }  
    else
    {
        clonedQuteLink.Revision_Number__c+=1;
    }

It works if the Revision_Number__c already has some values. If its blank then an error about derefencing null values appears. Please guide. Thanks

1
  • Is Revision_Number__c an integer field, a formula, a look-up or is it a text field?
    – crmprogdev
    Commented Jun 14, 2014 at 21:28

2 Answers 2

5

Check for null first and treat the blanks as zeros.

Try this:

if(quoteLink.Revision_Number__c!=null)
    {
        if(quoteLink.Revision_Number__c > 0)
           clonedQuteLink.Revision_Number__c+=1;
    }  
    else
    {
         quoteLink.Revision_Number__c=1;       

    }
3
  • Two things here: (1) null <op> var is difficult to read compared to var <op> null, unless your native language is a right-to-left language. Consider revising. We don't write 1 = x to assign 1 to x, we write x = 1. Nor do we say, in English, "if null is Revision_Number__c", but instead say "if Revision_Number__c is null". It keeps the "language" consistent. (2) See my deleted answer on not using if-else to reduce code complexity and providing easier code coverage.
    – sfdcfox
    Commented Jun 14, 2014 at 23:55
  • I tried with if(quoteLink.Revision_Number__c!=null) and also as if(null!=quoteLink.Revision_Number__c). Both didn't work if field has no values. Else its working if field has already a value which it increments. Thanks
    – MnZ
    Commented Jun 15, 2014 at 4:45
  • pepefloyd @sfdcfox I solved my problem. THere seemed to be some problem in my code. Thanks for your help :)
    – MnZ
    Commented Jun 15, 2014 at 9:19
1

Try something like:

if(quoteLink.Revision_Number__c.isEmpty() == false )

{
    clonedQuteLink.Revision_Number__c+=1;

}  

else

{
     quoteLink.Revision_Number__c=1;        
}  

If this doesn't work, you may need to add a try-catch block to trap a null-pointer exception whenever Revision_Number__c isEmpty or null.

1
  • thnaks @crmprogdev if(clonedQuteLink.Revision_Number__c != null) did the trick
    – MnZ
    Commented Jun 15, 2014 at 9:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .