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('{"leadRecords":[{"LeadId":"00Qf400000BMSLkEAP","Email":"[email protected]","Phone":"(850) 644-4200","Name":"Bertha Boxer"}],"idType":"LeadOrContactIds","createListView":false,"contactRecords":[{"ContactId":"003f400000vVPQfAAO","Email":"[email protected]","Phone":"9873341234","Name":"asdf asdf"}],"addToNewCampaign":true,"addToCampaign":true,"accountRecords":[]}');
ba1.createNewCampaign();
}
}
The test fails at ba1.createNewCampaign(); I apologize for improper post. This is my first ever project and testing. So, the clumsiness.
upsert campaignMembers2Add
. and throws exception.@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.