0

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;


        }
    }
}

1 Answer 1

2

You don't need to run that SOQL query at all. All of the data you need is already present in the records in Trigger.new. Further, you likely don't need to be running this after insert and incurring the overhead of another DML operation.

Instead, run the trigger before insert, iterate over Trigger.new, and apply your logic via a simple if statement.

As to your direct question,

l.Systems_Group__c = Campaign.Systems_Group__c;
l.System_Type__c = Campaign.System_Type__c;

This won't work because you are attempting to traverse a relationship by using the bare sObject name, Campaign. You can't do that in Apex; that reference is to the field entity itself, which has type Schema.SObjectField.

Instead, you'd need to collect the Campaign Ids of the Campaign Members matching your criteria, query the Campaigns into a Map, and then access the values by keying into the Map with the Campaign Id. Now, you could also do this by querying the CampaignMembers themselves and traversing the relationship in SOQL, but then you're locked in to running the trigger after insert and using another DML operation.

In total, your trigger would have a structure much like this:

trigger MyTrigger on CampaignMember (before insert) {
    Set<Id> campaignIds = new Set<Id>();

    for (CampaignMember cm : Trigger.new) {
        if (THIS CAMPAIGN MEMBER MATCHES CRITERIA) {
            campaignIds.add(cm.CampaignId);
        }
    }

    Map<Id, Campaign> campaignMap = new Map<Id, Campaign>([
        SELECT (FIELDS DESIRED)
        FROM Campaign
        WHERE Id IN :campaignIds
    ]);

    for (CampaignMember cm : Trigger.new) {
        if (THIS CAMPAIGN MEMBER MATCHES CRITERIA) {
            Campaign thisCampaign = campaignMap.get(cm.CampaignId);

            cm.YOUR_DESIRED_FIELD__C = thisCampaign.YOUR_SOURCE_FIELD__C;
        }
    }
}

No DML required at all.

1
  • Thanks for confirming David. I updated the trigger formatting as you advise, but am getting a nullpointer exception as directed above. I ran the system debug and am not sure why it was coming up as null.
    – Jeff
    Commented Feb 12, 2020 at 15:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .