I am trying to set up a trigger that will run a SOQL query to find all Campaign Member records that are associated to a Lead record only. Once I have that list I would like to run a loop through the records and find ones that have two fields that are null.
If they are null, then they should be populated with the values from the Campaign record (values that were pulled in the SOQL).
The issue that I am running into is on bold lines 13 - 14. The error is that there is an Illegal assignment from Schema.SObjectField to String.
Any advise on how to pull out the lead records, assign them a new value, then update the leads would be appreciated!
Thanks!
trigger addCampaignMemberInfo on CampaignMember (after insert) {
List<CampaignMember> campMemWithLead = [SELECT Id, LeadId, Campaign.System_Type__c, Campaign.Systems_Group__c, Lead.System_Type__c, Lead.Systems_Group__c
FROM CampaignMember
WHERE LeadId != NULL AND ContactId = NULL AND Id IN :Trigger.newMap.keySet()];
List<Lead> leadsToUpdate = new List<Lead>{};
//For loop to iterate through all the queried Campaign Member records
for (Lead l: campMemWithLead){
//check if Leads have System Type and Systems Group values are null
if(l.System_Type__c == NULL && l.Systems_Group__c == NULL){
//write the Campaign's Systems Group and System Type to the Lead record
**l.Systems_Group__c = Campaign.Systems_Group__c;
l.System_Type__c = Campaign.System_Type__c;**
//add Lead records to the list to update
leadsToUpdate.add(l);
}
}
//perform the Update DML statement
update leadsToUpdate;
}
I updated the trigger using the formatting that David had advised and was getting a nullpointer exception on line 25. I added some system debugs and saw that the cm.Lead.Systems_Group__c value was null. Not sure why that would be null.
My updated trigger is as follows:
trigger deleteThisTrigger on CampaignMember (before insert) {
Set<Id> campaignIds = new Set<Id>();
for (CampaignMember cm : Trigger.new) {
if (cm.LeadId != NULL && cm.ContactId == NULL && cm.Lead.System_Type__c == NULL && cm.Lead.Systems_Group__c == NULL) {
campaignIds.add(cm.campaignId);
System.debug(campaignIds);
}
}
Map<Id, Campaign> campaignMap = new Map<Id, Campaign> ([SELECT System_Type__c, Systems_Group__c
FROM Campaign
WHERE Id IN :campaignIds]);
System.debug(campaignMap);
for (CampaignMember cm : Trigger.new) {
if (cm.LeadId != NULL && cm.ContactId == NULL && cm.Lead.System_Type__c == NULL && cm.Lead.Systems_Group__c == NULL) {
Campaign thisCampaign = campaignMap.get(cm.CampaignId);
System.debug(thisCampaign.Systems_Group__c);
System.debug(cm.Lead.Systems_Group__c);
cm.Lead.Systems_Group__c = thisCampaign.Systems_Group__c;
cm.Lead.System_Type__c = thisCampaign.System_Type__c;
}
}
}