Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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

share|improve this question
    
Is Revision_Number__c an integer field, a formula, a look-up or is it a text field? –  crmprogdev Jun 14 at 21:28
add comment

2 Answers 2

Check for null first and treat the blanks as zeros.

Try this:

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

    }
share|improve this answer
    
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 Jun 14 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 Jun 15 at 4:45
    
pepefloyd @sfdcfox I solved my problem. THere seemed to be some problem in my code. Thanks for your help :) –  MnZ Jun 15 at 9:19
add comment

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.

share|improve this answer
    
thnaks @crmprogdev if(clonedQuteLink.Revision_Number__c != null) did the trick –  MnZ Jun 15 at 9:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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