You need to sign in to do that
Don't have an account?
VDub
INVALID_CROSS_REFERENCE_KEY:invalid cross reference id Error on Class
Hi,
I wrote a class to send an email and I'm getting an error when I try to save it to the Sandbox.
INVALID_CROSS_REFERENCE_KEY:invalid cross reference id
The only thing being referenced is a custom field on Task. Assigned_To_Email__c, the custom field is a formula.
I double checked to make sure it was accessible by all users.
I'm an Administrator working in a Sandbox so I should have access to all fields.
What level of access is needed to get past the error?
Thanks, Vanessa
Class Code:
public class SendEmailCreated{
public void sendMail (User u, Task t) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String [] toAddress = New String [] {t.Assigned_To_Email__c};
mail.setToAddresses(toAddress);
mail.setSubject('CS-Accommodation Order Task Created');
mail.setPlainTextBody('You are being sent this email because an CS-Accommodation Order Task has been created and assigned to you.\n\nThanks, SFDC');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
}
}
I wrote a class to send an email and I'm getting an error when I try to save it to the Sandbox.
INVALID_CROSS_REFERENCE_KEY:invalid cross reference id
The only thing being referenced is a custom field on Task. Assigned_To_Email__c, the custom field is a formula.
I double checked to make sure it was accessible by all users.
I'm an Administrator working in a Sandbox so I should have access to all fields.
What level of access is needed to get past the error?
Thanks, Vanessa
Class Code:
public class SendEmailCreated{
public void sendMail (User u, Task t) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String [] toAddress = New String [] {t.Assigned_To_Email__c};
mail.setToAddresses(toAddress);
mail.setSubject('CS-Accommodation Order Task Created');
mail.setPlainTextBody('You are being sent this email because an CS-Accommodation Order Task has been created and assigned to you.\n\nThanks, SFDC');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
}
}
Are you saying that you cannot save the Apex class at all? If that is the case, try saving it with a different name. Where exactly are you seeing this error?
All Answers
If you run the following from the developer console (execute anonymous), does it work? Replace ANYTASKID with the actual record Id of a task.
If this helps,please mark this as best answer to help others :)
I tried the to run the code you provided in the execute anonymous and it didn't work. I just get the error below. I'm going to be calling this from a trigger.
I forgot to delete the user object, I was trying different queries to get past the Invalid Cross Reference error. I don't know where the error is happening. The only error I get is when I save the class and I get the Invalid Cross Reference error
Is there a setting in salesforce that has to do with sending email that I might have missed?
Thanks, Vanessa
I tried your code and I still get the same error. I also tried a different query (see below) and still getting the same Invalid Cross Reference.
I don't know what is causing it.
CLASS WITH USER QUERY
public class SendEmailCreated{
public void sendMail (User u, Task t) {
User u1 =[select Email from User where Id =: t.OwnerId limit 1];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddress = new String[] {u1.Email};
mail.setToAddresses(toAddress);
mail.setSubject('CS-Accommodation Order Task Created');
mail.setPlainTextBody('You are being sent this email because an CS-Accommodation Order Task has been created and assigned to you.\n\nThanks, SFDC');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
}
}
Thanks, Vanessa
Are you saying that you cannot save the Apex class at all? If that is the case, try saving it with a different name. Where exactly are you seeing this error?
Yes, that is correct. I can't save the Apex class.
I re-named the class and it saved. smh. That fixed the problem.
Thank you so much,
Vanessa