I'm getting this error from a SendEmail call in production. The email is sent just fine in a sandbox with no errors.

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

public static void sendRenewalEmail(Id contactId, Id templateId){
    Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
    List<Messaging.SingleEmailMessage> messagesToSend = new List<Messaging.SingleEmailMessage>();


    try {
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { message });
    } catch (Exception e) {
        Mailer.getInstance().SendMailByEmail('Renewals Workflow Error: InvocableRenewalsNextStep.sendRenewalEmail', 'Error: ' + e.getMessage(), new String[] {'[email protected]'});


Any ideas?

Thank you!

  • Can a system administrator execute the code without errors in your production org? Have you tested in a sandbox using a non-administrator account? Commented Sep 28, 2015 at 19:17

1 Answer 1


IDs aren't persisted from Sandbox to Production, meaning an ID in your Sandbox org won't be the same once deployed to Production. In fact, one of the best practices in Apex is to avoid hardcoding IDs.

This line:


should be updated to reflect the value in Production, or even better, refer to a custom setting or label so that it can be changed configurationally, without having to redeploy the actual Apex code when the value changes.

  • Thank you for your reply! I've edited the line listed here to this: for(OrgWideEmailAddress owa : [SELECT Id, Address FROM OrgWideEmailAddress]) { if(owa.Address.contains('renewals')) message.setOrgWideEmailAddressId(owa.Id); } I'm still getting the same error. Commented Sep 28, 2015 at 18:25
  • Then one (or both) of the two IDs you're passing as a parameter to this method are hardcoded somewhere before the method call - check to see where contactId and templateId are defined before this method is called Commented Sep 28, 2015 at 18:50
  • 1
    Turns out the template ID's were hard-coded as well. Thank you so much. Commented Sep 28, 2015 at 21:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .