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
VDubVDub 

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});
}

}





Best Answer chosen by VDub
MaxPowerForceMaxPowerForce
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?

All Answers

MaxPowerForceMaxPowerForce
It seems like this should work.  Where are you calling this from?  Why did you define the method signature as accepting a user object, but are not using it at all in the method?  Does the error occur on the sendemail line or somewhere else?

If you run the following from the developer console (execute anonymous), does it work?  Replace ANYTASKID with the actual record Id of a task.
SendEmailCreated ec = new SendEmailCreated();
ec.sendmail(new user(), [SELECT Assigned_To_Email__c from Task WHERE Id = 'ANYTASKID']);


Vinit_KumarVinit_Kumar
Try below code :-

public class SendEmailCreated{   
public void sendMail (User u, Task t) {
//Query the Assigned_To_Email__c field
Task t1=[select Assigned_To_Email__c from Task where id=:t.id limit 1];

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String [] toAddress = New String [] {t1.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});
If this helps,please mark this as best answer to help others :)

VDubVDub
MaxPowerForce

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


User-added image

 
VDubVDub
Vinit Kumar 

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
MaxPowerForceMaxPowerForce
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?
This was selected as the best answer
VDubVDub
MaxPowerForce

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