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
An_jaAn_ja 

Too many SOQL queries : 101

Here is the code:

 

trigger ContactSumTrigger on Contact (after insert, after update) 

{
  User blocked = new User();
    try
    {
      blocked = [Select Id From User Where User.Id = '00550000001qLmn'];
    }
    
    catch(Exception e)
    {
}
  
  Map<Id,Account> updateAccounts = new Map<Id,Account>();
  Set<Id> updateAccountIds = new Set<Id>();
  
  // If we are inserting, updating, or undeleting, use the new ID values
  if(Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete&& blocked.Id!=null && Trigger.new[0].LastModifiedById != blocked.Id)
    for(Contact contact:Trigger.new)
      updateAccountIds.add(contact.AccountId);
  
  // If we are updating, some contacts might change, so include that as well as deletes
  if(Trigger.isUpdate || Trigger.isDelete && blocked.Id!=null && Trigger.old[0].LastModifiedById != blocked.Id )
    for(Contact contact:Trigger.old)
      updateAccountIds.add(contact.AccountId);
  
  // Do not create a record for null field
  updateAccountIds.remove(null);
  
  // Create in-memory copies for all accounts that will be affected
  if(blocked.Id!=null && Trigger.new[0].LastModifiedById != blocked.Id)
  for(Id accountId:updateAccountIds)
    updateAccounts.put(accountId,new Account(id=accountId,Number_of_Physicians__c=0));
    
  
  // Run an optimized query that looks for all contacts that meet the if/then criteria
  if(blocked.Id!=null && Trigger.new[0].LastModifiedById != blocked.Id)
  for(Contact contact:[select id, AccountId from contact where AccountId in :updateAccountIds and RecordTypeID = '01250000000DXGT'Limit 100])
    updateAccounts.get(contact.AccountId).Number_of_Physicians__c++;
  
  

    
  try
   {
    // Update all the accounts with new values.
    Database.update(updateAccounts.values());
   }
  
  catch(Exception e)
    {
    System.debug(e);
    trigger.new[0].addError('There was an error on a record related to this contact: Please contact to your system administrator by sending an email to support');
       
    }


}

 

Vinit_KumarVinit_Kumar

Hi,

 

Your code looks good.Can you check debug logs as what is going on there,I am sure some other transaction is running in same context of this tranaction.