0

I'm triggering an email with OnAfterInsert. Inside the method, I have access to all the necessary data to send the actual e-mail:

  • emailtemplateId
  • whoId
  • whatId
  • internalEmailAddrs
  • externalEmailAddrs
  • orgwideEmailId

Now, this trigger is activated after I insert a new Account. but while trying to send the e-mail, I notice that I have the in the title mentioned error. While digging some deeper, I notice that the ID of the Account does not exists anymore. I guess this entry has been rolled back, due to this error.

I've been digging in the permissions, and every permission (that I know of) is correct.

My last guess is that the guest user (anonymous profile) does not have access to the email template I'm trying to use. But I have no idea where I could double-check this.

Here is the code of the trigger:

public void OnAfterInsert(Account[] newAccounts){
    Id orgwideEmailId = null;
    List<OrgWideEmailAddress> orgWideEmailAddresses = [select id from OrgWideEmailAddress LIMIT 1];
    if(orgWideEmailAddresses.size() > 0) {
        orgwideEmailId = orgWideEmailAddresses[0].Id;
    }
    String selectedTemplateId = GlobalUtility.emailTemplates.get(
            GlobalUtilityConstants.EMAILTEMPLATE_PARTNERSELECTED_APINAME
    );

    String whoId = null;
    List<String> internalEmailAddrs = new List<String>();

    Set<Id> parentAccounts = new Set<Id>();
    for(Account acc : newAccounts){
        parentAccounts.add(acc.ParentId);
    }

    Map<Id, Account> parentAccountMap = new Map<Id, Account>([
            SELECT
                    Id,
                    Contact_Email__c,
                    Email__c
            FROM
                    Account
            WHERE
                    Id in :parentAccounts
    ]);
    Map<Id, String> parentEmailMap = new Map<Id,String>();

    for(Account parent : parentAccountMap.values()){
        if(parentEmailMap.containsKey(parent.Id)){
            // already processed this parent.
            continue;
        }

        if(parent.Contact_Email__c != null){
            parentEmailMap.put( parent.Id, parent.Contact_Email__c);
            continue;
        }

        if(parent.Email__c != null){
            parentEmailMap.put( parent.Id, parent.Email__c);
            continue;
        }
    }

    for( Account newAccount : newAccounts ) {

        String whatId = newAccount.Id;
        String selectedParentId = newAccount.ParentId;

        // Parent changed
        if( selectedParentId != null ) {
            List<String> externalEmailAddrs = new List<String>{parentEmailMap.get(selectedParentId)};


            ContactForm_Controller.doSentMail(
                    selectedTemplateId,
                    whoId,
                    whatId,
                    internalEmailAddrs,
                    externalEmailAddrs,
                    orgwideEmailId
            );
        }
    }
}

Did someone experience the same issue? Help is greatly appreciated!

  • you have to collect accountid right. parentAccounts.add(acc.id); why you are collecting parent accountid – Annappa PH Sep 10 '18 at 14:20
  • There are different types of accounts. One is a customer, which needs a partner. That's why I collect eventual parent accounts. – Jeroen Bellemans Sep 10 '18 at 14:22
  • On which line of code you are getting the error? – sanket kumar Sep 10 '18 at 14:28
  • ContactForm_Controller.doSentMail() --> messaging.renderStoredEmailTemplate() that's the stacktrace. – Jeroen Bellemans Sep 10 '18 at 14:31
  • can you please put system.debug log before ContactForm_Controller.doSentMail. Please print all the values. where you declared internalEmailAddrs,externalEmailAddrs???? – Annappa PH Sep 10 '18 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.