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
dbush2765dbush2765 

Test Method: Campaign Member (Contact) has no Account ID

I've been fighting this all day and can't seem to figure out what's going on.

I have a test method in which I need to have a Campaign Member, which is tied to a Contact. In the test method, I create an Account, Campaign, Contact, and Campaign Member.

 

The issue is that the Campaign Member seems to have no idea that the Contact's Account ID even exists. I can't see force Campaign Member to see it.

 

@isTest
global class TestGainsightUserGroupBatch implements Schedulable{
	
	static testMethod void GainsightUserGroupBatchTest() {

		// --------------- BEGIN SET UP --------------- //

		// Create new Account
		Account ACC = new Account(
			Name = 'Test Account'
			);
		insert ACC;

		// Create new Contact
		Contact CON = new Contact(
			FirstName = 'Test',
			LastName = 'Name',
			AccountId = ACC.Id
			);
		insert CON;

		// Create new Campaign
		Campaign CAM = new Campaign(
			Name = 'Test User Group Attendee',
			StartDate = Date.valueOf('2013-09-01')
			);
		insert CAM;

		// Create new Campaign Member
		CampaignMember CM = new CampaignMember(
			ContactId = CON.Id,
			CampaignId = CAM.Id,
			Status = 'Attended'
			);
		insert CM;

		System.Debug('---> Campaign Name = ' + CAM.Name);
		System.Debug('---> Campaign Member Account ID = ' + CM.Contact.AccountId);
		System.Debug('---> Campaign Member Account ID 2 = ' + CM.Contact.Account.Id);
		System.Debug('---> Account ID = ' + ACC.Id);
		System.Debug('---> Contacts Account ID = ' + CON.AccountId);

 I can always see the Account ID, and can see it when I just check CON.AccountID. It always returns null when I try to find it through the Campaign Member, though. Any ideas?

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

You will have to explicitly query campaignmember for crossreferenced fields..

 

 

// Create new Account
Account ACC = new Account(
Name = 'Test Account'
);
insert ACC;

// Create new Contact
Contact CON = new Contact(
FirstName = 'Test',
LastName = 'Name',
AccountId = ACC.Id
);
insert CON;

// Create new Campaign
Campaign CAM = new Campaign(
Name = 'Test User Group Attendee',
StartDate = Date.valueOf('2013-09-01')
);
insert CAM;

// Create new Campaign Member
CampaignMember CM = new CampaignMember(
ContactId = CON.Id,
CampaignId = CAM.Id,
Status = 'Attended'
);
insert CM;
CM = [select contact.accountid,contact.Account.Id from campaignmember where id = : cm.id];
System.Debug('---> Campaign Name = ' + CAM.Name);
System.Debug('---> Campaign Member Account ID = ' + CM.Contact.AccountId);
System.Debug('---> Campaign Member Account ID 2 = ' + CM.Contact.Account.Id);
System.Debug('---> Account ID = ' + ACC.Id);
System.Debug('---> Contacts Account ID = ' + CON.AccountId);