You need to sign in to do that
Don't have an account?
jaw999
Linking Campaign Member back to Account directly Trigger
Hi, users want to see all Campaign Members for Contacts at an Account on the Account page in a related list. Makes sense.
So I need to link the Campaign Member object directly to the Account.
Here's my code - am not sure why it isn't working.
Oddly the system debug tells me the field Contact on the Campaign member is Null even though the Member is linked to a Contact via it.
Any help appreciated thanks!
trigger AccountCampaignMember on CampaignMember (after insert, after update) { for (CampaignMember t : Trigger.new) { system.debug('camp memb id================'+t.id); system.debug('camp memb Contact================'+t.Contact); system.debug('camp memb ContactAccount================'+t.Contact.Account); if (t.Contact<> null) { If (t.Account__c==null){ t.Account__c= t.Contact.AccountId; } } //update t; }}
Hi,
Try this,
trigger AccountCampaignMember on CampaignMember (before insert, before update){
Set<Id> contactIds = new Set<Id>();
for (CampaignMember t : Trigger.new){
if (t.ContactId != null){
contactIds.add(t.ContactId);
}
}
Map<Id, Contact> mapContacts = new Map<Id, Contact>([SELECT AccountId FROM Contact WHERE Id IN:contactIds]);
for (CampaignMember t : Trigger.new){
if (t.ContactId != null){
t.Account__c= mapContacts.get(t.ContactId).AccountId;
}
}
}
All Answers
Hi,
Try this,
trigger AccountCampaignMember on CampaignMember (before insert, before update){
Set<Id> contactIds = new Set<Id>();
for (CampaignMember t : Trigger.new){
if (t.ContactId != null){
contactIds.add(t.ContactId);
}
}
Map<Id, Contact> mapContacts = new Map<Id, Contact>([SELECT AccountId FROM Contact WHERE Id IN:contactIds]);
for (CampaignMember t : Trigger.new){
if (t.ContactId != null){
t.Account__c= mapContacts.get(t.ContactId).AccountId;
}
}
}
Ah, looking great! thanks
Hello, I am having issue with testing, when deploying I get result:
AccountCampaignMemberTest.AccountCampaignMember() Class 17 1 Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Attempted to add a campaign member where either the member id 'null' or the campaign id 'null' is null.: []", Failure Stack Trace: "Class.AccountCampaignMemberTest.AccountCampaignMember: line 17, column 1"
I thought I specified the ids
Here is my test class:
CampaignMember DudeAtCamp = new CampaignMember(Contact=dude.id, Campaign=Camp.id);
Hi,
Try this,
Thanks. I got this error on deployment:
Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AccountCampaignMember: execution of AfterInsert caused by: System.FinalException: Record is read-only Trigger.AccountCampaignMember: line 16, column 1: []", Failure Stack Trace: "Class.AccountCampa...
that line is:
read only?
thanks
This may be oversimplifying things, but I just added a formula field to the Campaign Member object called Account. In my case, I'm using it as a hyperlink, but you could modify it to contain just the ID of the Account. The formula is below:
HYPERLINK(Contact.Account.Id , Contact.Account.Name )
Eva, this does not create a link between the objects. This formula doesn't create a related list on the Account page of all the Contacts in Campaigns the way the look-up field does.
This has been great. I am now trying to have this Not fire on a particular Campaign type, Reference List.
making line 13 this isn't working:
Is it b/c I haven't grabbed the type yet? thanks
the system debug tells me the Type is null