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
thecrmninjathecrmninja 

Conditional Looping in Cross Object Trigger

I am designing a trigger to update a Campaign Lookup field on the Lead object based on the various Campaigns a Lead is a member of. My ultimate goal is to achieve the following....

-If Lead has membership in a Campaign NOT NAMED "General Marketing" or "Online Application" and is from the last 365 Days, select the oldest Campaign Membership to use in updating the Lead Lookup
-Else if Lead has membership in a Campaign NOT NAMED "General Marketing" or "Online Application" and is NOT from the last 365 Days, select the oldest Campaign Membership to use in updating the Lead Lookup
-Else if Lead has membership in a Campaign NAMED "Online Application" update Lead lookup with Online Application Campaign
-Else assign Lead Lookup current Campaign being associated to it

I have constructed a trigger on the Campaign Member object that is able to find Leads related to the new Campaign Member(s), find the oldest Campaign Membership for said Lead(s) in the last 365 days and update a Custom Lookup on the Lead with the Campaign Name from the appropriate Campaign Membership.
To accomplish this, I had to introduce Limits on the results my SOQL query returned because the Relationship Query used to build the "LeadWithCampMembs" list would return duplicate Lead IDs, to avoid generating a "Duplicate ID" error.

What is the best way to facilitate conditional statements into this trigger so that if I don't meet the current set of criteria ("LeadsWithCampMembs"), I can move on to a second set of criteria and then a third?
Is it possible to workaround having a Limit in my Relationship Query and then get down to one record to add to the leadUpdate2 from inside my for loop?

///////////////////CURRENT TRIGGER///////////////////////

trigger LeadCreditedCampaign on CampaignMember (After Insert) { List<ID> leadCPGsIDs = New List<ID>(); //Run through Tasks in trigger, for those associated to Lead, add to above List for (CampaignMember c:System.Trigger.new) {if (c.Leadid<>Null) { leadCPGsIDs.add( c.LeadId ) ; }} List<Lead> LeadWithCampMembs = [select id, name, Credited_Campaign__c, (select id, CreatedDate, CampaignId from CampaignMembers where LeadId IN :leadCPGsIDs AND CreatedDate = LAST_N_DAYS:365 Order by CreatedDate Asc Limit 1) from Lead where Id IN :leadCPGsIDs]; List<lead> leadUpdate2 = new List<lead>(); for(Lead l : LeadWithCampMembs){ for(CampaignMember cb: l.CampaignMembers){ l.Credited_Campaign__c = cb.CampaignId; leadUpdate2.add(l); } } Update LeadUpdate2; }

 

jkucerajkucera

Your first 2 actions are the same - why not combine them?

 

Also for the first 2 cases, since this is a new campaign member, it will only be the "oldest" if this is the first campaign member, so simpler logic would be: 

 

if (Campaign.Name !='General'){ if(CmCount=1 || Campaign.Name=='Online Application'){ //select this cm } else { //do nothing as the oldest campaign member would already be assigned } }

 

Note the above isn't true syntax, but just illustration of the logic.

 

To get CmCount, you probably want to use the latest SOQL aggregation to avoid doing more than 20 queries in the trigger (which isn't allowed).