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
BigTobyDogBigTobyDog 

URGENT: caused by: System.LimitException: Too many SOQL queries: 101

System was working fine until this morning, now all of a sudden is starting to generate this email error notification.  Can anyone help steer me in the right direction?

  

 

 

ERROR MESSGE:  The following errors were encountered while processing an incoming email:

 

CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY : emailAttachmentReassigner: execution of BeforeInsert

 

caused by: System.LimitException: Too many SOQL queries: 101

 

Trigger.emailAttachmentReassigner: line 13, column 1 CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY : emailAttachmentReassigner: execution of BeforeInsert

 

caused by: System.LimitException: Too many SOQL queries: 101

 

Trigger.emailAttachmentReassigner: line 13, column 1 CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY : emailAttachmentReassigner: execution of BeforeInsert

 

caused by: System.LimitException: Too many SOQL queries: 101

 

Sean TanSean Tan

Please review the code related to this trigger:

 

emailAttachmentReassigner

 

Ensure it's not doing any SOQL statements in loops. If you need help with this, please post the code.

BigTobyDogBigTobyDog

Hi Sean... Thanks for your response.  The trigger is below.  I greatly appreciate your help as I am new to triggers and did not write this original one.  Had been working for years to my knowledge an now real change to it.  

 

 

1// Jonathan Hersh - jhersh@salesforce.com

2// November 13, 2008

3

4trigger emailAttachmentReassigner on Attachment (before insert) {

5  for( Attachment a : trigger.new ) {

6  // Check the parent ID - if it's 02s, this is for an email message

7  if( a.parentid == null )

8      continue;

10    String s = string.valueof( a.parentid );

11   

12    if( s.substring( 0, 3 ) == '02s' )

13      a.parentid = [select parentID from EmailMessage where id = :a.parentid].parentID;

14     

15  }

16}

Avidev9Avidev9

Well you have to bulkify your trigger.

Your trigger seems to have a SOQL inside a for loop which is not considered as a good practice.

 

I made some changes to your code to bulkify the same.

 

trigger emailAttachmentReassigner on Attachment(before insert) {
    Set < String > parentIds = new Set < String > ();
    for (Attachment a: trigger.new) {
        // Check the parent ID - if it's 02s, this is for an email message
        if (a.parentid == null)
            continue;

        String s = string.valueof(a.parentid);

        if (s.substring(0, 3) == '02s')
            parentIds.add(a.parentid);

    }

    Map < Id, EmailMessage > EmailMessageMap = new Map < Id, EmailMessage > ([select parentID,Id from EmailMessage where id IN =:parentIds]);
    for (Attachment a: trigger.new) {
        EmailMessage emsg = EmailMessageMap.get(a.Id);
        if (emsg != null)
            a.parentid = emsg.parentid
    }
}

 PS : May contain syntax errors

Sean TanSean Tan

Ok so based off the logic I see in this trigger, what you're doing is checking if the Attachment's parent is an EmailMessage. If so you're re-parenting it to the Parent of the Email Message.

 

The problem line would be line 13, where the it's looping through each attachment and querying the email message object. If I'm assuming the business case correctly try the below code... it should do what you need but be bulk safe:

 

// Jonathan Hersh - jhersh@salesforce.com
// November 13, 2008
trigger emailAttachmentReassigner on Attachment (before insert)
{
    Map<Id, EmailMessage> emailMessageMap = new Map<Id, EmailMessage>{};
    
    for( Attachment a : trigger.new )
    {
        // Check the parent ID - if it's 02s, this is for an email message
        if( a.parentid == null )
            continue;
        String s = string.valueof( a.parentid );
        
        if( s.substring( 0, 3 ) == '02s' )
        {
            emailMessageMap.put(a.ParentId, null);            
        }
    }
    
    emailMessageMap.putAll([SELECT Id, ParentID from EmailMessage where Id IN :emailMessageMap.keySet()]);
    
    for ( Attachmment a : Trigger.new )
    {
        EmailMessage em = emailMessageMap.get(a.ParentId);
        
        if (em != null)
        {
            a.ParentId = em.ParentId;
        }
    }
}

 

BigTobyDogBigTobyDog

Thank you!!! So you think the reason this error is throwing is due to an attachment in the email they are attempting to send to case?

 

Also, do I first need to write the trigger and test class within the sandbox then deploy as a new trigger and delete the original problem trigger?  Sorry for the naivety, but would you be able to help with the test class? 

Sean TanSean Tan

If the trigger is already in production, then I assume there is already a test class that covers the trigger. If so you don't really need to change anything with the test class.

 

You'll want to simply overwrite the code in the Sandbox org and deploy it to production. (You can run your test class in sandbox to make sure the test class still works).

BigTobyDogBigTobyDog

Yes the test class still passes.  There seems to be an error the new trigger though.  

 

 

Error: Compile Error: Invalid type: Attachmment at line 22 column 11

Sean TanSean Tan

Spelling mistake ... Sorry about that... try this:

 

// Jonathan Hersh - jhersh@salesforce.com
// November 13, 2008
trigger emailAttachmentReassigner on Attachment (before insert) 
{
    Map<Id, EmailMessage> emailMessageMap = new Map<Id, EmailMessage>{};
    
    for( Attachment a : trigger.new ) 
    {
        // Check the parent ID - if it's 02s, this is for an email message
        if( a.parentid == null )
            continue;
        String s = string.valueof( a.parentid );
        
        if( s.substring( 0, 3 ) == '02s' )
        {
            emailMessageMap.put(a.ParentId, null);            
        }
    }
    
    emailMessageMap.putAll([SELECT Id, ParentID from EmailMessage where Id IN :emailMessageMap.keySet()]);
    
    for ( Attachment a : Trigger.new )
    {
        EmailMessage em = emailMessageMap.get(a.ParentId);
        
        if (em != null)
        {
            a.ParentId = em.ParentId;
        }
    }
}

 

BigTobyDogBigTobyDog

Excellent.  It's not throwing the error anymore.  Question,  would I be updating the class or the trigger and which do I deploy to production?  Both a class and a trigger exist with the same code.

 

Thanks!!!!

Sean TanSean Tan

I'm not sure why both a class and a trigger exist with the same code. So I can't say for sure on that.  However, in terms of getting it resolved in Prod ASAP, I would update the trigger in sandbox and just deploy the trigger.