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
Adil_SFDCAdil_SFDC 

List Index out of bounds 0

public void sendMailAndCreateTask(List<Document> dList)
    {
        List<Task> taskList = new List<Task>();
        
        String endPoint;
        HTTPResponse res;
 
        HttpRequest req = new HttpRequest();
        
  
        Http http = new Http();
        String customizedEmailText = '';
          String customizedEmailSub = '';
  
       Contact_Email__c[]  ce = [select Subject__c,description__c from Contact_Email__c where user__c = :UserInfo.getUserId()];
        if(ce.size() >0 && ce[0].description__c != null)
        {
          customizedEmailText = EncodingUtil.UrlEncode(ce[0].description__c,'UTF-8');
           customizedEmailSub = EncodingUtil.UrlEncode(ce[0].Subject__c,'UTF-8');
          
        }
        
       
      Integer ra=dList.size() - 1;
              //  docIdString=dList[0].title;
        if(dList!=null &&dList.size()>0) {
    selecttitle = dList[0].title+' '+'and'+' '+ra+' '+'others';
     selecttitle = EncodingUtil.UrlEncode(selecttitle,'UTF-8'); 
     }
     
             if(dList!=null &&dList.size()==1) 
     selecttitle = EncodingUtil.UrlEncode(dList[0].title,'UTF-8'); 
           
           String docIdsSelected ;
       if(dList!=null &&dList.size()>0) 
        docIdsSelected = dList[0].id;
 
       if(dList!=null && dList.size()>1)
        for(Integer i=1;i<dList.size();i++){
        docIdsSelected = docIdsSelected + ','+dList[i].id;
        system.debug('Docdocdoc'+ docIdsSelected );
        }
    
        if(isDynamic == 'true'&&ce[0].Subject__c==null  ) ( ERROR)
         
     else  if(isDynamic == 'true'&&ce[0].Subject__c!=null)
 
           
            else if(isDynamic == 'false'&&ce[0].Subject__c!=null)
               
               
               else  
               
               
               
                   system.debug('XXXX'+endpoint);
                   
        /*if(testVar == '')
            {
                res = http.send(req);
                JSONParser parser = JSON.createParser(res.getBody());
                while (parser.nextToken() != null) 
                {
                    if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'd')) 
                    {
                        parser.nextToken();
                        if(parser.getText() == 'true')
                        {
                            taskList.add(t);
                            
                            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Email sent successfully for the selected documents.'));// \''+d.title+'\'.'));
                        }
                        else
                        {
                            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Email could not be sent.'));                        
                        }
                    }
                }        
                    
            }*/
            
            boolean dListcountertrack=false;
        for(Document d: dList)
        {
      
         if(d==dList[0])
         dListcountertrack=true;
         else
         dListcountertrack=false;
          
        // docIdsSelected = d.Id;
     //  if(isDynamic == 'true')
         
               
          //  else
  //   system.debug('eeeee'+dList);
            req.setMethod('GET');
            req.setTimeout(60000);
    
            req.setEndpoint(endPoint);
   
            Task t = new Task();
            t.WhoId = c.id
            t.Subject = 'iCentera Dynamic Email sent for '+d.title;
            t.status = 'Completed';
            t.type = 'Email';
            String comments = d.title+'\n\n';
            comments += 'Please download and save the following linked file:\n';
            comments += '\nTitle:'+d.title;
            comments += '\nFormat:'+d.filetype;
            comments += '\nDescription:'+d.description;
    
            t.description = comments;
            taskList.add(t);
            
           if(testVar == '' && dListcountertrack)
            {
                res = http.send(req);
                JSONParser parser = JSON.createParser(res.getBody());
                while (parser.nextToken() != null) 
                {
                    if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) && (parser.getText() == 'd')) 
                    {
                        parser.nextToken();
                        if(parser.getText() == 'true')
                        {
                           // taskList.add(t);
                            
                            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Email sent successfully for the selected documents.'));// \''+d.title+'\'.'));
                        }
                        else
                        {
                            ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Email could not be sent.'));                        
                        }
                    }
                }        
                    
            }
                
        }
 
        insert taskList;
    }
Rahul SharmaRahul Sharma

Adil_SFDC,

 

Before refering to the 0th index of list you must perform a check on whether the list is empty or not.

Adil_SFDCAdil_SFDC

Hi Rahul 

 

i was waiting for the reply . Here there are no records my list is totally empty. 

If my list is empty how do i check this. 

Rahul SharmaRahul Sharma

Whenever you try to access records from list first check if its empty or not like it is done below:

 

List<Account> lstAccount = new List<Account>();

// Fill the list...

// Before processing the list check if its empty or not
// It will go inside the loop only if the List is having values in it. if(lstAccount.size() > 0){ // Do something with lstAccount[0].Name }

 

Adil_SFDCAdil_SFDC

when i try to fill the list i get the error again . I m trying this

 

List< Contact_Email__c> lstcon = new List<Contact_Email__c>();
lstcon[0].subject__c= ' ';
insert lstcon;
if(lstcon.size()>0){
if(isDynamic == 'true'&&lstcon[0].subject__c==null)

 

I get error again

Rahul SharmaRahul Sharma

In your code just add a condition and check if it helps:

if(isDynamic == 'true' && ce.size() > 0 && ce[0].Subject__c==null )

 In previous post it was just an example to explain when to add the check.

Adil_SFDCAdil_SFDC

i Added this  and it works

 

List< Contact_Email__c> lstcon = new List<Contact_Email__c>();
Contact_Email__c cont = new Contact_Email__c();

cont.subject__c= ' ';
lstcon.add(cont);
if(lstcon.size()>0){
if(isDynamic == 'true'&&lstcon[0].subject__c==null)

Rahul SharmaRahul Sharma

Yup that would work too..

Adil_SFDCAdil_SFDC

Hi Rahul that works but my select title fails 

I dont get any error message but the select title doesnt pass in the endpoint. 

Any suggestion

 




public void sendMailAndCreateTask(List<Document> dList) { List<Task> taskList = new List<Task>(); String endPoint; HTTPResponse res; HttpRequest req = new HttpRequest(); Http http = new Http(); String customizedEmailText = ''; String customizedEmailSub = ''; Contact_Email__c[] ce = [select Subject__c,description__c from Contact_Email__c where user__c = :UserInfo.getUserId()]; if( ce.size() >0 &&ce[0].description__c != null) { customizedEmailText = EncodingUtil.UrlEncode(ce[0].description__c,'UTF-8'); customizedEmailSub = EncodingUtil.UrlEncode(ce[0].Subject__c,'UTF-8'); } Integer ra=dList.size() - 1; // docIdString=dList[0].title; if(dList!=null &&dList.size()>0) { selecttitle = dList[0].title+' '+'and'+' '+ra+' '+'other(s)'; selecttitle = EncodingUtil.UrlEncode(selecttitle,'UTF-8'); system.debug('yyy'+selecttitle); } if(dList!=null &&dList.size()==1) selecttitle = EncodingUtil.UrlEncode(dList[0].title,'UTF-8'); system.debug('rrrr'+selecttitle); String docIdsSelected ; if(dList!=null &&dList.size()>0) docIdsSelected = dList[0].id; if(dList!=null && dList.size()>1) for(Integer i=1;i<dList.size();i++){ docIdsSelected = docIdsSelected + ','+dList[i].id; system.debug('Docdocdoc'+ docIdsSelected ); } List< Contact_Email__c> lstcon = new List<Contact_Email__c>(); Contact_Email__c cont = new Contact_Email__c(); cont.subject__c= selecttitle; lstcon.add(cont); if(lstcon.size()>0){ if(isDynamic == 'true'&&lstcon[0].subject__c==null) endPoint = 'http://api.icentera.com/v1sandbox/TestService.svc/json/EmailDocuments?token='+tokenVal+'&docids='+docIdsSelected+'&recipient='+c.Email+'&subject='+selecttitle+'&sfdccontactid='+c.id+'&isDynamic=true'+'&body='; if(isDynamic == 'true'&&lstcon[0].subject__c!=null) endPoint = 'http://api.icentera.com/v1sandbox/TestService.svc/json/EmailDocuments?token='+tokenVal+'&docids='+docIdsSelected+'&recipient='+c.Email+'&subject='+customizedEmailSub+'&sfdccontactid='+c.id+'&isDynamic=true'+'&body='+customizedEmailText; if(isDynamic == 'false'&&lstcon[0].subject__c!=null)

 

Rahul SharmaRahul Sharma

Add some debug statements for knowing the cause.

Adil_SFDCAdil_SFDC

for the solution i tried for inserting contactemail 

 

I get error 

 

Arguement 1 cannot be null in test method. 

 

any solution for this