Testing my apex controller in my dev org runs fine. However, testing the same file in another dev org gives error INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

Here is the controller snippet

if (leadsList != null) {
      anyContactsOrLeadsGiven = true;
      for (Map<String, String> lead2Add :leadsList) {
        String leadId = lead2Add.get(LEAD_ID_FIELD);
        if (!currentCampaignMemberIds.contains(leadId)) {
          CampaignMember cm = new CampaignMember(CampaignId = selectedCampaignId,
            LeadId = leadId, Status = 'Sent');
          System.debug('BulkActionController::AddTo Adding Lead Id ' + leadId);
        } else {
          System.debug('BulkActionController::AddTo Lead Id ' + leadId + ' is already present');

    if (!campaignMembers2Add.isEmpty()) {
      try {
        upsert campaignMembers2Add;
        setCon = null;
      } catch (DmlException e) {
        errMsg = BULKACTION_ERRSTRING_PREFIX + 'Error inserting campaign members' +  +e.getMessage();
        System.debug('BulkActionController::AddTo ' + errMsg);
        throw new UpdateCampaignException(errMsg);
        // ApexPages.addMessages(e);

Test Case:

private class BulkActionControllerTest {
  static void testCreateAndAdd() {
    BulkActionController ba1 = new BulkActionController();
    Campaign campaignTest2 = new Campaign(Name = 'Test2');
    ba1.setrecordsFromWave('{&quot;leadRecords&quot;:[{&quot;LeadId&quot;:&quot;00Qf400000BMSLkEAP&quot;,&quot;Email&quot;:&quot;[email protected]&quot;,&quot;Phone&quot;:&quot;(850) 644-4200&quot;,&quot;Name&quot;:&quot;Bertha Boxer&quot;}],&quot;idType&quot;:&quot;LeadOrContactIds&quot;,&quot;createListView&quot;:false,&quot;contactRecords&quot;:[{&quot;ContactId&quot;:&quot;003f400000vVPQfAAO&quot;,&quot;Email&quot;:&quot;[email protected]&quot;,&quot;Phone&quot;:&quot;9873341234&quot;,&quot;Name&quot;:&quot;asdf asdf&quot;}],&quot;addToNewCampaign&quot;:true,&quot;addToCampaign&quot;:true,&quot;accountRecords&quot;:[]}');

The test fails at ba1.createNewCampaign(); I apologize for improper post. This is my first ever project and testing. So, the clumsiness.

  • where is the error occurring? Commented Apr 12, 2019 at 21:45
  • when i run the test cases in the new dev. the test stops at upsert campaignMembers2Add. and throws exception. Commented Apr 12, 2019 at 23:56
  • This behavior can occur if you use @isTest(seeAllData=true) in your unit tests, or if you hard-code Ids in your unit tests. You'll need to edit your question to include your test class to really get a more specific answer than that.
    – Derek F
    Commented Apr 13, 2019 at 1:08
  • 1
    We really need to see the test, then. Commented Apr 13, 2019 at 1:43
  • @DerekF I did almost exactly the same. I am sorry if this is not the right platform. Is there a better way of doing it? Commented Apr 15, 2019 at 19:49

1 Answer 1


The data you are (presumably) deserializing in BulkActionController, as supplied in your unit test, includes Salesforce Ids. For example, you have


Those Ids will not be visible within your test context, as the unit test executes in an isolated environment where you must create your own records. If you deserialize this data and those Id values end up in the Id field or any lookup fields within the resultant sObjects, you will reliably get

INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

because those Ids don't exist.

The variance between tested orgs is most likey to be because one of them is running with seeAllData=true and the records exist in that environment. An alternate explanation would be if the hard-coded Ids were for metadata entities that are always visible in test context, but they exist only in one environment. I do not see that that appears to be happening here, though.

You must log in to answer this question.

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