0

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);
          campaignMembers2Add.add(cm);
        } else {
          System.debug('BulkActionController::AddTo Lead Id ' + leadId + ' is already present');
        }
      }
    }

    if (!campaignMembers2Add.isEmpty()) {
      try {
        upsert campaignMembers2Add;
        setCon = null;
        getSetCon();
      } 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:

@isTest
private class BulkActionControllerTest {
  @isTest
  static void testCreateAndAdd() {
    BulkActionController ba1 = new BulkActionController();
    Campaign campaignTest2 = new Campaign(Name = 'Test2');
    ba1.setCampaign(campaignTest2);
    ba1.setrecordsFromWave('{&quot;leadRecords&quot;:[{&quot;LeadId&quot;:&quot;00Qf400000BMSLkEAP&quot;,&quot;Email&quot;:&quot;bertha@fcof.net&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;asdf@adf.com&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;:[]}');
    ba1.createNewCampaign();
  }
}

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? – Sebastian Kessel Apr 12 '19 at 21:45
  • when i run the test cases in the new dev. the test stops at upsert campaignMembers2Add. and throws exception. – Vishnu Varma Apr 12 '19 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 Apr 13 '19 at 1:08
  • 1
    We really need to see the test, then. – Sebastian Kessel Apr 13 '19 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? – Vishnu Varma Apr 15 '19 at 19:49
0

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

&quot;LeadId&quot;:&quot;00Qf400000BMSLkEAP&quot

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.