0

I get error when LargestRevision list has null revision numbers. Here revision number is a number field and Quote_Link__c is a lookup.

  if(quoteLink.Id!=null)
       {
        LargestRevision =[SELECT Revision_Number__c,Quote_Link__c FROM OPP_QuoteLink__c WHERE Quote_Link__c=:quoteLink.Id ORDER BY Revision_Number__c DESC LIMIT 1];      
            if(LargestRevision.size()==0&&LargestRevision[0].Revision_Number__c==null)
            {
                //System.debug('--LargestRevisionsize---'+LargestRevision.size());
                //System.debug('--LargestRevision[0]---'+LargestRevision[0].Revision_Number__c); 
                clonedQuteLink.Quote_Link__c = quoteLink.Id;
                clonedQuteLink.Revision_Number__c = 1;
                System.debug('--clonedQuteLink.Quote_Link__c ---'+clonedQuteLink.Quote_Link__c);
            }
       }
        else{
                System.debug('---quoteLink.Quote_Link__c---'+quoteLink.Quote_Link__c);                
                clonedQuteLink.Quote_Link__c = quoteLink.Quote_Link__c;
                clonedQuteLink.Revision_Number__c=(LargestRevision[0].Revision_Number__c)+1;    //THIS line throws error if list has revision null
        }
  • 1
    How do you init the LargestRevision variable? You can not use the index when a list is empty. – Jair Jun 18 '14 at 1:26
  • Its a list of objects – MnZ Jun 18 '14 at 1:51
5

If largestrevision has a size of zero, it is empty. You can only access indices less than size. You probably meant to use || instead of &&. The shortcut operator will only evaluate the right-hand side if the left side is true. This will prevent the index exception.

  • Turned out to a silly issue. I was looking into an index even if there was no value. I guess i have to improve knowledge on condional statements. Thanks for helping @sdfcfox – MnZ Jun 19 '14 at 10:39

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.