1

I have custom LWC page as Public Site, In my Page i will be collecting the Name and Email address, On clicking send will insert the data in Custom Object and will send an Email to an given Email address.

Email Template is configured as Visualforce Email Template Email Name is Enroll

When I click send email button from my Page I'm getting System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id following error.

try {
            emitcs1__Enrollment__c newEnrollment = new Enrollment__c (
                emitcs1__Family_Name__c = familyName,
                emitcs1__Family_Email__c = familyEmail,
                emitcs1__User__c = serviceCenterId
            );
            insert newEnrollment;

            Id enrollId = newEnrollment.Id;


            // get an email template
            EmailTemplate tpl = (EmailTemplate)[select Id   FROM EmailTemplate WHERE DeveloperName ='Enroll' limit 1];
          //  EmailTemplate tpl = (EmailTemplate)[select Id   FROM EmailTemplate WHERE DeveloperName ='Enrollment_Link' limit 1];

            List<Messaging.SingleEmailMessage> emailList = new List<Messaging.SingleEmailMessage>();
            String[] toAddresses = new String[] {familyEmail};
                String[] ccAddresses = new String[] {'myemailaddress@gmail.com'};
                    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();


            email.setTemplateId(tpl.Id);     
            email.setReplyTo('support@test.com');
            email.setSenderDisplayName('testing');        

            Contact con=[SELECT Id FROM Contact WHERE AccountId =:serviceCenterId limit 1];  
            email.setTargetObjectId(con.Id);
            email.setWhatId(enrollId); 
            email.setSaveAsActivity(false);
            email.setCcAddresses(ccAddresses);
            email.setTreatTargetObjectAsRecipient(false);
            email.setToAddresses(toAddresses);
            emailList.add(email);  

            List<Messaging.SendEmailResult> results = Messaging.sendEmail(emailList);
            if (results[0].success) {            
                newEnrollment.Status__c= 'Email Sent';
                Update newEnrollment;
                System.debug('The email was sent successfully.');
            }else {
                System.debug('The email failed to send:  ' +  results[0].errors[0].message);
            } 
            return true;
        }catch (Exception e) {
            system.debug('exception -->'  + e);
            return false;
        }  

I have HTML Email template Name Enrollment_Link by Changing this Template in my code It's working. Both has referered to same fields and Objects.

Debug Log

14:49:43.0 (196648255)|VARIABLE_SCOPE_BEGIN|[331]|e|Exception|true|false
14:49:43.0 (196811201)|VARIABLE_ASSIGNMENT|[331]|e|"common.apex.methods.MessagingStaticMethods$EmailExecutionException: SendEmail failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []"|0x206203be
14:49:43.0 (196831506)|STATEMENT_EXECUTE|[331]
14:49:43.0 (196838244)|STATEMENT_EXECUTE|[332]
14:49:43.0 (196848350)|HEAP_ALLOCATE|[332]|Bytes:21
14:49:43.0 (196956008)|HEAP_ALLOCATE|[332]|Bytes:139
14:49:43.0 (196983450)|HEAP_ALLOCATE|[332]|Bytes:160
14:49:43.0 (197012130)|USER_DEBUG|[332]|DEBUG|exception -->System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []
14:49:43.0 (197032576)|STATEMENT_EXECUTE|[333]
14:49:43.0 (197044203)|HEAP_ALLOCATE|[333]|Bytes:5
14:49:43.0 (197062023)|SYSTEM_MODE_EXIT|true

I tested From Classic Email Templates page by Clicking Send Test and Verify Merge Fields. It's working.

1
  • Have you checked if the user needs access through sharing rules or something for any of the objects mentioned in the Enroll template? Also, have you checked if the email templates are available to the public site? Apr 17 at 10:24
0

check if the user has access right to Enrollment__c and contact object.
I think you forgot to set the template Id also

email.setTemplateId(tpl.Id);
1
  • Yes the user has access right to Enrollment__c and contact object. Also I missed to add email.setTemplateId(tpl.Id); here. It's already exist in my code May 9 '20 at 7:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.