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
gdkgdk 

Getting Error while Uplaoding more than 100 contacts and Accounts

Hi All,

User-added image

I need to uplaod more than 100 contacts and Accounts like the above CSV file format,
here 2 accounts and 13 contacts iam uploading and also duplicate not allowed for Contacts as well as Account
this is working fine when iam uploading 30 records,
But i am getting following error while uploading more than 30 records

System.LimitException: Too many SOQL queries: 101

below the code i used:
-------------------------------
public PageReference UploadAction() {
          Count=0;
    contactInsert.clear();
    dcontact.clear();
 
 
  if(csvFile!=null)
        {
           nameFile=csvFile.toString();          
               //Read the CSV Rows using split method
            filelines = nameFile.split('\n');        
         }
    for (Integer i=1;i<filelines.size();i++)
            {
                String[] inputvalues = new String[]{};
                  inputvalues = filelines[i].split(',');   
                 Contact cnt= new Contact();
                 Account Ac=new Account();
                  List<Account> al= new List<Account>();
                 cnt.FirstName= inputvalues[0];
                 cnt.LastName= inputvalues[1];
                 if(String.isNotBlank(inputvalues[2]))
                 {
                 Ac.Name=inputvalues[2];
                al=[Select id,Name from Account Where name=:Ac.Name];                 
                 if(al.size()==0)
                 {
                 insert Ac;
                 }                 
                 List<Account> Acl= new List<Account>();
                 Acl=[Select id, name from Account Where name=:Ac.Name];
                 cnt.AccountId=Acl[0].id;     
                 
                }
                
                cnt.Email= inputvalues[3];
                
                cnt.Phone= inputvalues[4];    
                List<contact> cl= new List<contact>();
                cl=[select id, email from contact where email=:inputvalues[3] and account.name=:inputvalues[2]];
               
                if(cl.size()==0)
                {
                count++;
                 contactInsert.add(cnt);
        
                 }
                 else{
              
                 cnt.AccountName__C=  inputvalues[2];
          
       
                 dcontact.add(cnt);
                 
                 
       
                 }
            }    
            
            if(count!=0)
            {
            System.Debug('-------------count value----'+ Count);
    insert contactInsert;
    count=0;
    
    }else{
 
    }
     
        return null;
    }


Thanks...!
Best Answer chosen by gdk
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi ,

Put this query outside of your for loop . Use map instead .
List<contact> cl= new List<contact>();
                cl=[select id, email from contact where email=:inputvalues[3] and account.name=:inputvalues[2]];


Use belo code.
public PageReference UploadAction() {
          Count=0;
    contactInsert.clear();
    dcontact.clear();
 

  if(csvFile!=null)
        {
           nameFile=csvFile.toString();          
               //Read the CSV Rows using split method
            filelines = nameFile.split('\n');        
         }
	 for (Integer i=1;i<filelines.size();i++)
            {
				String[] inputvalues = new String[]{};
                inputvalues = filelines[i].split(','); 
				
			}			
	Map<Id,Contact> contactMap = new Map<Id,Contact>([select id, email from contact where email=:inputvalues[3] and account.name=:inputvalues[2]]);
    for (Integer i=1;i<filelines.size();i++)
            {
                String[] inputvalues = new String[]{};
                  inputvalues = filelines[i].split(',');   
                 Contact cnt= new Contact();
                 Account Ac=new Account();
                  List<Account> al= new List<Account>();
                 cnt.FirstName= inputvalues[0];
                 cnt.LastName= inputvalues[1];
                 if(String.isNotBlank(inputvalues[2]))
                 {
                 Ac.Name=inputvalues[2];
                al=[Select id,Name from Account Where name=:Ac.Name];                 
                 if(al.size()==0)
                 {
                 insert Ac;
                 }                 
                 List<Account> Acl= new List<Account>();
                 Acl=[Select id, name from Account Where name=:Ac.Name];
                 cnt.AccountId=Acl[0].id;     
                 
                }
                
                cnt.Email= inputvalues[3];
                
                cnt.Phone= inputvalues[4];    
                //List<contact> cl= new List<contact>();
                //cl=[select id, email from contact where email=:inputvalues[3] and account.name=:inputvalues[2]];
               
                if(contactMap.values().size()==0)
                {
                count++;
                 contactInsert.add(cnt);
        
                 }
                 else{
              
                 cnt.AccountName__C=  inputvalues[2];
          
       
                 dcontact.add(cnt);
                 
                 
       
                 }
            }    
            
            if(count!=0)
            {
            System.Debug('-------------count value----'+ Count);
    insert contactInsert;
    count=0;
    
    }else{
 
    }
     
        return null;
    }

Let us know if it helps you.

All Answers

Pankaj_GanwaniPankaj_Ganwani
Hi,

You have written the soql in for loop since we are permitted to execute 100 SOQLs in one transaction, please remove it from there or bulkify your code.
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi ,

Put this query outside of your for loop . Use map instead .
List<contact> cl= new List<contact>();
                cl=[select id, email from contact where email=:inputvalues[3] and account.name=:inputvalues[2]];


Use belo code.
public PageReference UploadAction() {
          Count=0;
    contactInsert.clear();
    dcontact.clear();
 

  if(csvFile!=null)
        {
           nameFile=csvFile.toString();          
               //Read the CSV Rows using split method
            filelines = nameFile.split('\n');        
         }
	 for (Integer i=1;i<filelines.size();i++)
            {
				String[] inputvalues = new String[]{};
                inputvalues = filelines[i].split(','); 
				
			}			
	Map<Id,Contact> contactMap = new Map<Id,Contact>([select id, email from contact where email=:inputvalues[3] and account.name=:inputvalues[2]]);
    for (Integer i=1;i<filelines.size();i++)
            {
                String[] inputvalues = new String[]{};
                  inputvalues = filelines[i].split(',');   
                 Contact cnt= new Contact();
                 Account Ac=new Account();
                  List<Account> al= new List<Account>();
                 cnt.FirstName= inputvalues[0];
                 cnt.LastName= inputvalues[1];
                 if(String.isNotBlank(inputvalues[2]))
                 {
                 Ac.Name=inputvalues[2];
                al=[Select id,Name from Account Where name=:Ac.Name];                 
                 if(al.size()==0)
                 {
                 insert Ac;
                 }                 
                 List<Account> Acl= new List<Account>();
                 Acl=[Select id, name from Account Where name=:Ac.Name];
                 cnt.AccountId=Acl[0].id;     
                 
                }
                
                cnt.Email= inputvalues[3];
                
                cnt.Phone= inputvalues[4];    
                //List<contact> cl= new List<contact>();
                //cl=[select id, email from contact where email=:inputvalues[3] and account.name=:inputvalues[2]];
               
                if(contactMap.values().size()==0)
                {
                count++;
                 contactInsert.add(cnt);
        
                 }
                 else{
              
                 cnt.AccountName__C=  inputvalues[2];
          
       
                 dcontact.add(cnt);
                 
                 
       
                 }
            }    
            
            if(count!=0)
            {
            System.Debug('-------------count value----'+ Count);
    insert contactInsert;
    count=0;
    
    }else{
 
    }
     
        return null;
    }

Let us know if it helps you.
This was selected as the best answer
gdkgdk
Hi Ashish thanks for your quick respond,
            making the above changes we can upload upto 40 Records(Contacts and Accounts), but here i need to upload more than 100 records


Thanks and regards,
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi ,

What issue you are facing now ?
gdkgdk
Hi Ashish Thanks for you reply...,


Thanks..!

 
Gujjula404Gujjula404
Hi Ashish,
 below code can you check once


 if(csvFile!=null)

     {          nameFile=csvFile.toString();         
           //Read the CSV Rows using split method
         filelines = nameFile.split('\n');       

     }
    for (Integer i=1;i<filelines.size();i++)

         {
             String[] inputvalues = new String[]{};

            inputvalues = filelines[i].split(',');
             

}          

   Map<Id,Contact> contactMap = new Map<Id,Contact>([select id, email from contact where email=:inputvalues[3] and account.name=:inputvalues[2]]);


-------------------------------------------

it will compare only last record

with out loop how can we compare?