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
SFDC Forum 007SFDC Forum 007 

Apex code alert message (List out of bound exception)

Hi,

Kindly help me out with my requirement,

I have a callreport (object) which i am attaching it to account via lookup for generating a pdf, which populates all the account details

 

I have a lookup called as "Call Plan" in call report, object which i am attaching to account, wen i am not entering any values in call plan lookup under callreport and wen i am attaching the call report to account

 

By clicking on the pdf button "PDF" , an error message "List index out of bound[0], displays but wen i attach the call plan its working fine Instead of that error message i want to display an meaningful message like "Please attach the callplan to call report",

 

Please help me out Thanks in Advance

bob_buzzardbob_buzzard

I think we'll need to see some code for this - it sounds like whatever is generating the PDF needs to be more defensive and check the size of the call plan list.

SFDC Forum 007SFDC Forum 007

public class CallReportExtn{
    
        public class PdfWrapper{
            public Call_Report__c cr{get;set;}
            public List<PADetails> pernAcctList{get;set;}
            public List<PADetails> pernAcctList2{get;set;}
            public Call_Item__c callitemList{get;set;}
           public List<Customer_Brand__c> customerbrndbbl {get;set;}
        }
            public class PADetails{
            public List<Contact> cclist{get;set;}
            public String paName{get;set;}
            public String paaccName{get;set;}
            public String paJobTitle{get;set;}
            
            public String paLastvisit{get;set;}
            
            public String paBrand1{get;set;}
            public String paBrand2{get;set;}
            public String paBrand3{get;set;}
            public String callObjective1{get;set;}
            public String callObjective2{get;set;}
             public String crdate{get;set;}
        }
         set<id> top3bbl =  new set<id>();
         public string bblval{get;set;}
        public String brand1{get;set;}
        public String brand2{get;set;}
        public String brand3{get;set;}
        public list<string> callnames{get;set;}
        public List<Call_Report__c> crList{get;set;}
        public List<Customer_Brand__c> cbList{get;set;}
        public List<PdfWrapper> pdfWrapperList{get;set;}
        public List<PADetails> PADetailsList{get;set;}    
        public String userid=UserInfo.getUserId();
        Map<Id,List<PADetails>> paMap=new Map<Id,List<PADetails>>();
        Map<Id,Call_Item__c> callitempaMap=new Map<Id,Call_Item__c>();
        Map<Id,Call_Report__c> callReppaMap=new Map<Id,Call_Report__c>();
        Map<String,Customer_Brand__c> cCBpaMap=new Map<String,Customer_Brand__c>();
        Map<String,Contact> conmap=new Map<String,Contact>();
        Map<Id,List<PADetails>> cpmap=new Map<Id,List<PADetails>>();
         List<Customer_Brand__c> customerbrnd = new List<Customer_Brand__c>();
        List<Call_Item__c>  calltemplst=new List<Call_Item__c>();
        
        
           public List<SelectOption> getBBLS()
  {
  List<SelectOption> options = new List<SelectOption>();
        
   Schema.DescribeFieldResult fieldResult =
 Customer_Brand__c.BBL_Override__c.getDescribe();
   List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
       options.add(new SelectOption('','--None--'));  
   for( Schema.PicklistEntry f : ple)
   {
      options.add(new SelectOption(f.getLabel(), f.getValue()));
   }       
   return options;
  }    
 
 
 
 
    public CallReportExtn(ApexPages.StandardController controller){
        pdfWrapperList=baAccInfo();       
    }
    public List<PdfWrapper> baAccInfo(){
        List<PdfWrapper> pdfWrapper=new List<PdfWrapper>();
        Set<Id> accIdSet=new Set<Id>();
        Set<Id> callplanIdSet=new Set<Id>();
        crList=[select id,Contact__c,Date__c,Call_plan__c,Account__r.Name,Account__c,Account__r.id,Account__r.BillingCity,Account__r.BillingStreet,Account__r.BillingState,Account__r.BillingCountry,Account__r.BillingPostalCode,Account__r.phone,Account__r.Type,Account__r.Segment_Status__c,Account__r.Total_Number_Of_Patients__c,Account__r.No_of_Seats__c,Objective__r.Name,Objective2__r.Name from Call_Report__c where CallDate__c=TODAY and OwnerId=:userid];
       System.debug('crList==> '+crList);   
        for(Call_Report__c callRep:crList){
             if(callRep.Account__c !=null)
                 accIdSet.add(callRep.Account__c);
                 
            if(callRep.Call_plan__c !=null)
                 callplanIdSet.add(callRep.Call_plan__c);  
                 
            callReppaMap.put(callRep.Contact__c,callRep);     
                        
        }  
        
       List<Account> paList=[select Name,Job_Title__pc,Primary_Customer__pc,id,PersonContactId,Last_Activity__pc  from Account where Primary_Customer__pc in:accIdSet];
       List<Contact> cclist=[select Last_Activity__c from contact where Last_Activity__c !=null];
        System.debug('cclist ==> '+cclist );   
        if(callplanIdSet.size()>0){
             calltemplst = [select id,Brand__c, Brand__r.name,Position__c,Brand_Call_Plan__c from Call_Item__c where Brand_Call_Plan__c in:callplanIdSet ORDER BY Position__c limit 3];
             System.debug('calltemplst ==> '+calltemplst );        
        }
            
            
      
        for(Id baId:accIdSet){
            PADetailsList=new List<PADetails>();           
            PADetails paDetHeader=new PADetails();
            paDetHeader.paName='Name';
            paDetHeader.paJobTitle='Job Title';
           // paDetHeader.paLastvisit='Lastvisit';
           paDetHeader.crdate='Time';
           
            if(crList!=null && crList.size()>0 && crList.get(0)!=null && crList.get(0).Date__c!=null){
                paDetHeader.crdate='Time';     
            }else{
                paDetHeader.crdate='';    
            }
            
           if(calltemplst!=null && calltemplst.size()>0 && calltemplst.get(0)!=null && calltemplst.get(0).Brand__r.name!=''){
                paDetHeader.paBrand1=calltemplst.get(0).Brand__r.name;
                top3bbl.add(calltemplst.get(0).Brand__c);               
                                
            }else{
                paDetHeader.paBrand1='';    
            }
            
            
       
            if(calltemplst!=null && calltemplst.size()>0 && calltemplst.get(0)!=null && calltemplst.get(0).Brand__r.name!=''){
                paDetHeader.paBrand1=calltemplst.get(0).Brand__r.name;
                top3bbl.add(calltemplst.get(0).Brand__c);               
                                
            }else{
                paDetHeader.paBrand1='';    
            }
            
            if(calltemplst!=null && calltemplst.size()>1 && calltemplst.get(1)!=null && calltemplst.get(1).Brand__r.name!=''){
                paDetHeader.paBrand2=calltemplst.get(1).Brand__r.name;  
               top3bbl.add(calltemplst.get(1).Brand__c);                
            }else{
                paDetHeader.paBrand2='';    
            }
            
            if(calltemplst!=null && calltemplst.size()>2 && calltemplst.get(2)!=null && calltemplst.get(2).Brand__r.name!=''){
                paDetHeader.paBrand3=calltemplst.get(2).Brand__r.name;
                top3bbl.add(calltemplst.get(2).Brand__c);               
            }else{
                paDetHeader.paBrand3='';    
            }
            
            
             if(crList!=null && crList.size()>0 && crList.get(0)!=null && crList.get(0).Objective__r.Name!=''){
                paDetHeader.callObjective1='Objective1';     
            }else{
                paDetHeader.callObjective1='';    
            }
            
            if(crList!=null && crList.size()>0 && crList.get(0)!=null && crList.get(0).Objective__r.Name!=''){
                paDetHeader.callObjective2='Objective2';     
            }else{
                paDetHeader.callObjective2='';    
            }
            
             if(cclist!=null){
                paDetHeader.paLastvisit='Lastvisit';     
            }else{
                paDetHeader.paLastvisit='';    
            }
            
            
       if(top3bbl.size()>0){
           cbList= [select BBL__c,HCP__c,Brand__r.Name from Customer_Brand__c where  Brand__c in:top3bbl and HCP__c in:callReppaMap.keyset()];
           System.debug('cbList==> '+cbList);
           for(Customer_Brand__c cb:cbList){
               cCBpaMap.put(cb.HCP__c+cb.Brand__r.Name,cb);    
           }
       }
       
      
            
            PADetailsList.add(paDetHeader);
                   
            for(Account ac:paList){
                    System.debug('ac.Name ==> '+ac.Name);
                    PADetails paDet=new PADetails();
                    paDet.paName=ac.Name;
                    if(ac.Primary_Customer__pc !=null)
                    //paDet.paaccName = ac.Primary_Customer__pc;
                    paDet.paJobTitle=ac.Job_Title__pc;
                    
                    if(callReppaMap.get(ac.personContactId)!=null){
                        paDet.crdate=string.valueof(callReppaMap.get(ac.personContactId).Date__c.time());
                    }else{
                        paDet.crdate='';

                    }
                   
                   
                       
                  
                    if(callReppaMap.get(ac.personContactId)!=null){
                        paDet.callObjective1=callReppaMap.get(ac.personContactId).Objective__r.Name;
                    }else{
                        paDet.callObjective1='';

                    }
                    if(callReppaMap.get(ac.personContactId)!=null){
                        paDet.callObjective2=callReppaMap.get(ac.personContactId).Objective2__r.Name;
                    }else{
                        paDet.callObjective2='';

                    }
                    if(cCBpaMap.get(ac.personContactId+calltemplst.get(0).Brand__r.name)!=null){
                        paDet.paBrand1=cCBpaMap.get(ac.personContactId+calltemplst.get(0).Brand__r.name).BBL__c;
                    }else{
                        paDet.paBrand1='';

                    }
                    if(cCBpaMap.get(ac.personContactId+calltemplst.get(1).Brand__r.name)!=null){
                        paDet.paBrand2=cCBpaMap.get(ac.personContactId+calltemplst.get(1).Brand__r.name).BBL__c;
                    }else{
                        paDet.paBrand2='';

                    }
                    if(cCBpaMap.get(ac.personContactId+calltemplst.get(2).Brand__r.name)!=null){
                        paDet.paBrand3=cCBpaMap.get(ac.personContactId+calltemplst.get(2).Brand__r.name).BBL__c;
                    }else{
                        paDet.paBrand3='';

                    }                    
                    if(ac.Last_Activity__pc!=null){
                        System.debug('ac.Last_Activity__pc ==> '+ac.Last_Activity__pc);
                        String str=String.valueOf(ac.Last_Activity__pc);
                        System.debug('str ==> '+str);
                        System.debug('str1 ==> '+str.substring(0,4));
                        System.debug('str2 ==> '+str.substring(5,7));
                        System.debug('str3 ==> '+str.substring(8,10));
                        Datetime dt=Datetime.newInstance(Integer.valueOf(str.substring(0,4)),Integer.valueOf(str.substring(5,7)),Integer.valueOf(str.substring(8,10)),00,00,00);
                        System.debug('dt==> '+dt);
                        paDet.paLastvisit=dt.format('dd/mm/yyyy');
                       
                        
                     }
                     
                     else
                     {
                         paDet.paLastvisit='';
                         
                     }
                     
                     
                 
                     
                     
                     
                    
                     
                    if(baId == ac.Primary_Customer__pc)
                    {
                    PADetailsList.add(paDet);
                    paMap.put(baId,PADetailsList);    
                    }
          }
          
         }
        
         
        
        for(Call_Report__c callRep:crList){
            PdfWrapper pw=new PdfWrapper();
            pw.cr=callRep;
            pw.pernAcctList=paMap.get(callRep.Account__c);
            //if(pw.pernAcctList.size()>0)
            //{
           // for(PADetails obj:pw.pernAcctList)
            //{
            //if(obj.paaccName !=null && callRep.Account__r.Name !=null)
           // {
           // if(callRep.Account__r.Name != obj.paaccName)
           // {
            // pw.pernAcctList2.add(obj);
            //}
            //}
            //}
            //}
            pw.callitemList=callitempaMap.get(callRep.Call_plan__c);
            pdfWrapper.add(pw);
                                
        }
        
        return pdfWrapper;
    }
    
    

}this is my apex controller for ur reference

bob_buzzardbob_buzzard

Quite a lot of code there - care to give me a hint where the exception is coming from?

SFDC Forum 007SFDC Forum 007

k i have a button called as pdf buttonon account list view layout, like said wen i dont attach any call plan to a call report and click on the pdf button it gives an listindex out of bound, but wen i enter values for call plan and attach it to a call report which inturn attached to account, its working fine

 

 

i want a alert message i.e : "please attach the call plan "to be displayed rather than displaying list index out of bound error

 

 

should i need to write a java script on button click "pdf button", please help me out with some code

 

bob_buzzardbob_buzzard

I understand the problem that you are having - what  I'm asking is where in the code this error is occuring.  When you get the exception, do you get a line number or stack trace?

SFDC Forum 007SFDC Forum 007

the problem doesnt occurs in code, its not throwing any line no exception , its happening in the sf screen just giving list index out of bound[0]on click of pdf button , its not giving any line no error in the code

 

 

Thanks in Advance

bob_buzzardbob_buzzard

The error is coming from your controller, you just aren't catching it.

 

If you turn on debug logging and click the button, you should see the line number there.

Shiv ShankarShiv Shankar

Where is your PDF genrate button ..it is either on call report or  on account ?

Shiv ShankarShiv Shankar

as far as i understood 

 

crList=[select id,Contact__c,Date__c,Call_plan__c,Account__r.Name,Account__c,Account__r.id,Account__r.BillingCity,Account__r.BillingStreet,Account__r.BillingState,Account__r.BillingCountry,Account__r.BillingPostalCode,Account__r.phone,Account__r.Type,Account__r.Segment_Status__c,Account__r.Total_Number_Of_Patients__c,Account__r.No_of_Seats__c,Objective__r.Name,Objective2__r.Name from Call_Report__c where CallDate__c=TODAY and OwnerId=:userid];

 

after this code you can add

 

if(crList == 0){

      ApexPages.addMessage(new ApexPages.message(ApexPages.Severity.ERROR,'Your error message'));

}

else{

       'Your code to genrate pdf'

}

 

if it works please mark as a solution

SFDC Forum 007SFDC Forum 007

pdf is on account list page after clic on "go " button the pdf button will populate

SFDC Forum 007SFDC Forum 007

Comparison arguments must be compatible types: LIST<Call_Report__c>, Integer at line 92 column 15

 

 

its throwing me this error atter adding the condtion