You need to sign in to do that
Don't have an account?
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?
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
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.
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
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.
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.
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.
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.
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
Did you find any solution for OWD private and need readonly for guest user. We have exactly same situation?
Thanks
Athi
then set the org-wide default external access for the objects that used in your email template
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
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