Skip to main content
added 23 characters in body
Source Link
David Reed
  • 93.7k
  • 14
  • 90
  • 166

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.

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 = [
        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.

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.

Source Link
David Reed
  • 93.7k
  • 14
  • 90
  • 166

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 = [
        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.