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
Ramesh VaratharajRamesh Varatharaj 

Too many SOQL error -

I am writting a code to copy the company name and title from lead or contacts to the task/activity object - i am doing this so that i can include them in the activity tab for users quick reference. I think i almost completed - but stuck with too many SOQL queries:101 error while testing for records inserts more than 50. Can any please suggest how to fix this:
Here is my code:


trigger copynametitle on Task (before insert) {
// trigger to copy company and title of the contact or lead related to the task

public set<Id> LeadIDs = new Set<Id>();
public set<Id> ContactIDs = new Set<Id>();




    for(task t: trigger.new){
        if(t.WhoId<>null){
            if(string.valueOf(t.WhoId).startsWith('00Q'))
            LeadIDs.add(t.WhoId);
            if(string.valueOf(t.WhoId).startsWith('003'))
            ContactIDs.add(t.WhoId);
        }
    List<Lead>leaddetail = [Select Id, Name, Company, Title from Lead where ID in :LeadIDs];
    List<Contact>Contactdetail = [Select Id, Name, Title, AccountName_Text__c from Contact where ID in :ContactIDs];
    
    Map<Id, Lead> LeadMap = new Map<Id,Lead>(); 
    Map<Id, Contact> ContactMap = new Map<Id,Contact>(); 
    
    for(Lead l:leaddetail){
    LeadMap.put(l.Id, l);    
    }
    
    for(Contact c:Contactdetail){
    ContactMap.put(c.Id, c);
    }
    
    for(task t1:trigger.new){
    String wld = t.WhoId;
    
    if(wld!=null && wld.startswith('00Q'))
    {
    Lead thisLead = LeadMap.get(t.WhoID);
    if(thisLead!=null)
    {
    t1.Company__c = thisLead.Company;
    t1.Title__c = thisLead.Title;
    }  } 
     
    if(wld!=null && wld.startswith('003'))
    {
    Contact thisContact = ContactMap.get(t.WhoID);
    if(thisContact!=null)
    {
    t1.Company__c = thisContact.AccountName_Text__c;
    t1.Title__c = thisContact.Title;
    }  }  

    }

}}
Amit Chaudhary 8Amit Chaudhary 8
Issue was  { } only due to that your query was comming inside the for loop.

Please try below code. I hope that will help  you.
trigger copynametitle on Task (before insert) 
{
	// trigger to copy company and title of the contact or lead related to the task

	public set<Id> LeadIDs = new Set<Id>();
	public set<Id> ContactIDs = new Set<Id>();

    for(task t: trigger.new)
	{
        if(t.WhoId<>null)
		{
            if(string.valueOf(t.WhoId).startsWith('00Q'))
            LeadIDs.add(t.WhoId);
            if(string.valueOf(t.WhoId).startsWith('003'))
            ContactIDs.add(t.WhoId);
        }
	}	
	
    List<Lead> leaddetail = [Select Id, Name, Company, Title from Lead where ID in :LeadIDs];
    List<Contact> Contactdetail = [Select Id, Name, Title, AccountName_Text__c from Contact where ID in :ContactIDs];
    
    Map<Id, Lead> LeadMap = new Map<Id,Lead>(); 
    Map<Id, Contact> ContactMap = new Map<Id,Contact>(); 
    
    for(Lead l:leaddetail)
	{
		LeadMap.put(l.Id, l);    
    }
    
    for(Contact c: Contactdetail)
	{
		ContactMap.put(c.Id, c);
    }
    
    for(task t1: trigger.new)
	{
		String wld = t.WhoId;
    
		if(wld!=null && wld.startswith('00Q'))
		{
			Lead thisLead = LeadMap.get(t.WhoID);
			if(thisLead!=null)
			{
			t1.Company__c = thisLead.Company;
			t1.Title__c = thisLead.Title;
			}  
		} 
		if(wld!=null && wld.startswith('003'))
		{
			Contact thisContact = ContactMap.get(t.WhoID);
			if(thisContact!=null)
			{
				t1.Company__c = thisContact.AccountName_Text__c;
				t1.Title__c = thisContact.Title;
			}  
		}  
    }
}
Please check below post for trigger best pratice i hope that will help you

http://amitsalesforce.blogspot.com/2015/06/trigger-best-practices-sample-trigger.html

Please let us know if this will help  you

Thanks
AMit Chaudhary