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
grigri9grigri9 

Sites user not sending out email correctly

I have some code that sends out an email. If I run that code from inside salesforce it works fine. However, if I run that code as the public sites user I get the following SendEmailResult error.

 

SendEmail failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

 

However, all the mailing objects ID's (who, what, template and orgwide) are exactly the same when I send it from inside salesforce and from the website so I don't understand how it could be giving an error in one case but not the other.

 

 Here's the code that sends the email:

 

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 

mail.setTemplateId(template);

mail.setwhatId(what);

mail.setTargetObjectId(who);

mail.setSaveAsActivity(true);

mail.setOrgWideEmailAddressId(orgwide);

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail});

 

for(Messaging.SendEmailResult res : r){

if(res.isSuccess() == false)

System.debug(res.geterrors().get(0).getMessage());

}

 

 Am I misunderstanding the error or is there some other ID field that could be screwing up the email?

 

 

Message Edited by grigri9 on 11-17-2009 09:08 PM
WesNolte__cWesNolte__c

Hey

 

It's tough to tell without seeing the rest of the code, but here's a few things to try:

 

 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 

 

System.debug(template); 

mail.setTemplateId(template);

System.debug(what); 

mail.setwhatId(what);

System.debug(who); 

mail.setTargetObjectId(who);

// try set the next line to false as well as true 

mail.setSaveAsActivity(true);

mail.setTargetObjectId(orgwide); 

mail.setOrgWideEmailAddressId(orgwide);

Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail});

 

for(Messaging.SendEmailResult res : r){

if(res.isSuccess() == false)

System.debug(res.geterrors().get(0).getMessage());

}



Why am I doing this seemingly pointless debugging? Well since you asked so nicely I'll tell you :) One of the biggest issues when pushing code into sites are permission, whether they be profile issues, sharing rules, record types etc. What we're trying to do here is determine if your guest user has access to all the information that your secured user has. If they do, then we'll take if from there.

 

 

Wes 

grigri9grigri9

Hi, 

  

I should have included this in the code I copied but I actually had a debug statement printing out the who, what, template and orgwide ID's. They were the same with both users. who was a contact ID, what was an Opportunity ID, template was a VF email template ID and orgwide was an orgwide email ID.

 

Hardcoding the ID values also gave me the same result (logged in user worked and sites user failed).

 

I tried switching SaveAsActivity from true to false like you suggested but unfortunately got the same result as before.

 

I agree that it must be some sort of permissions issue but I'm at a loss for what it could be. This class does not implement 'with sharing' so the code should be running at the system level in any case.

WesNolte__cWesNolte__c

Hey

 

Have you checked your field level permissions on your site profile for the objects referenced in the who and what ids? Perhaps your template uses fields that the profiles can't see?

 

You might also want to try sending an email that doesn't use a template first, just as a check.

 

Wes

osamanosaman

Did any body get the solution for this? I am experiencing the same issue. All of the classes that are involved in this process have 'without sharing" keyword.

vitonevitone

Has any one solved this yet?  I can't figure out why I cant send an email from a public site.  I have exactly the same problem...

 

-------------------------------

 

I'm leaving my solution here in hopes that it can save someone else from a lost Friday night. 

 

Messaging.sendEmail(...) runs under the (Public) Site's user's permission set _regardless_ of whether or not the surrounding class specifyies "without sharing".  That means the guest user associated with the site has to have permission to, at a minimum, read the "what" and "who" objects passed to the mail message.   You will need to edit the site user's profile to allow access to the objects. If you have a restrictive environment, as I do, then you will also need to add the site user to a group and then create a sharing rule to give it access.

 

Also, make sure that if your sharing settings call for Contact access to be "controlled by parent" that they are related to an Account or else you will receive the same error when you try to send to them.

 

Suresh_SoftsquareSuresh_Softsquare

I encountered the same problem and was able to fix that. Whay vitone mentioned in the above post is absolutely correct. In my case, even though I gave Read permission to the object (Contact) in the user Site user profile, I got the same error. Then, I checked the sharring setting. It was marked as private and through a sharing rule, Read access was provided to a Group. I added the site uer to that Group and the problem is resolved!!!

 

I would suggest the owner of this thread to make vitone's response as Resolved.

SivarajanSivarajan
Hi Folks, 

I am in the same boat, getting the same error ''SendEmail failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []'. I tried the above solution mentioned by Suresh and Vitone but it does not work out. 

In the Site guest user profile, enabled object access related to whatId, whoId mentioned in the SingleEmail message. As Contact OWD sharing is private, created sharing rule for Account and Case and assigned to a public group. but no luck. 

If any one has resolved this issue, Please share your resolution or experience.

Thanks in advance.
 
Darshana AminDarshana Amin
Hi Guys,

I too am facing a similar issue. Currently when I am changing the OWD access for the object to Public Read, I do not encounter any error. However mine is a restrictive environment & I hence I am forced to keep the OWD as private only.

Moreover my Site User has 'Customer Community Login' license due to which I am unable to add these users to a group & grant them Read access.

Do we have a feasible alternative for this? 

Thank You,
Darshana

 
AthiSachiAthiSachi
Hi Darshana,

Did you find any solution for OWD private and need readonly for guest user. We have exactly same situation?

Thanks
Athi
Jaap ScheperJaap Scheper
When I ran into this problem, it turned out that I used a wrong merge-field. Try sending a fresh new emtpy test email tempate. When that works,  turn back to your original template, remove all merge fields and bring them back one by one to see which one gives you trouble.
RyanCoxRyanCox
I was able to fix this problem by unchecking ’Secure guest user record access’ on Setup > Sharing Settings. This allows org-wide defaults for external users to be applied. The help for this field says: Secure the access that guest users have to your org’s data. Guest users’ org-wide defaults are set to Private for all objects, and this access level can’t be changed. You can create guest user sharing rules, but you can’t add guest users to groups or queues or manually share records with them.

User-added image
then set the org-wide default external access for the objects that used in your email template

User-added image
 
George Liu @randGeorge Liu @rand
Hi guys,

I ran into this error ''SendEmail failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []' after I removed the guest user from public group and configured a sharing rule to share the records. The sharing rule I created would share ALL records to the guest user. However, if we grant VIEW ALL privilege to the guest user for the custom object in question, everything would work.

Any help would be appreciated.

Thanks,

George
Suraj Tripathi 47Suraj Tripathi 47
Hi,
Greetings!

Maybe the email template you are using, the current user may not have access to the email template.
So please check the permission of the current user.
If you give this user or profile permission to access the email template, then no error will occur.

If you find your Solution then mark this as the best answer. 

Thank you!

Regards,
Suraj Tripathi