+ Start a Discussion
Roy SouravRoy Sourav 

guest user sends emails with visualforce email template - invalid cross reference ID -

Hi All,
I am badly stuck at a point where I need help from the Community.

I have visualforce email template with custom vf component. The email is send using apex code by a guest user. I get below error while whenever guest user tries to send email using a site page -

INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

public static void sendSurveyNotification(list<Feedback_Survey__c> feedbacks) {
        //try {           
        	if(feedbacks!=null && feedbacks.size()> 0) {               
                //Fetching the email template
                Id emt_notify;
                Id emt_completed;
                list<EmailTemplate> emts = [Select Id, Name, DeveloperName From EmailTemplate where DeveloperName 
                                            IN ('C_SAT_Survey_Request_Notification','C_SAT_Survey_Completed_Notification') LIMIT 2];
                if(emts[0].DeveloperName == 'C_SAT_Survey_Request_Notification') {
                    emt_notify = emts[0].id;
                    emt_completed = emts[1].id;
                else {
                    emt_notify = emts[1].id;
                    emt_completed = emts[0].id;
                list<Messaging.SingleEmailMessage> messages = new list<Messaging.SingleEmailMessage>();
                for(Feedback_Survey__c fbk: feedbacks) {
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                   	if(fbk.C_SAT_Status__c == 'Initiated')
                    System.debug('Taget Object Id==>'+UserInfo.getUserId());
                    System.debug('fbk id==>'+fbk.id);
                    list<String> ccaddresses = new list<String>{(String)fbk.Transaction_Manager_Email__c, (String)fbk.Survey_Requester_Email__c};
                    mail.setToAddresses(new list<String>{(String)fbk.Real_Estate_Manager_Email__c});
                    System.debug('Org Wide Address==>'+system.label.Org_Wide_TM_Support);
        Catch(Exception e) {
            System.debug('Error ==>' +e.getMessage());
            System.debug('Line Number ==>' +e.getLineNumber());
            System.debug('Cause ==>'+e.getCause());
            System.debug('Cause ==>'+e.getStackTraceString());

Thanks in Advance...!!
Suraj MakandarSuraj Makandar
Hi Sourav Roy 63,

We get this error whenever we have assigned a wrong Id to a field. 

For ex: In contact object we have a lookup to account, If we try to create a new contact and give an invalid id for the AccountID field then we will get the error.

Please make sure write Ids are assigned.

Roy SouravRoy Sourav

Hi Suraj,

Thanks for your reply...!!

I have checked thoroughly, I am passing the correct ID for all email methods. As you can see in the above code, I have used debug logs to check whatever I am passing.

Sushil Kumar VermaSushil Kumar Verma
Hi @Roy Sourav

Please add the renderUsingSystemContextWithoutSharing="True" attribute to every Visualforce email template.

Jaap ScheperJaap Scheper
Thanks, Sushil, that did the trick for me!
Juan David Uribe RuizJuan David Uribe Ruiz
Thanks @Sushil Kumar Verma, its works for me
Juan David Uribe RuizJuan David Uribe Ruiz
For more reference please visit: https://help.salesforce.com/articleView?id=email_templates_vf_guest_users.htm&type=5