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
impalacrazy69impalacrazy69 

update lead fields based on campaign field

So here is what i am trying to accomplish:

 

I need a trigger that basically does the following:

  1. Every time a CampaignMember object is created…
  2. Check if the GEO on the parent campaign is 'Mid Market'
  3. If it is, update the related lead:
    1. Set the Lead_Routing_Override__c field to be 'Mid Market'
    2. Set Trigger_Assignment_Rules__c to true

 

Here is what i have thus far but i feel i am missing someting becasuse my statement to see if the GEO code = Mid Market wont let me save the file.

 

trigger Lead_Campaign_Mid_Market on CampaignMember(after insert, after update) {

    List<Lead> ldlist;
    for(CampaignMember cmpmember: Trigger.new){
        if(
        cmpmember.LeadId !=null 
        //Campaign.GEO__c ='Mid Market'
        ){
              Lead ld = [SELECT id,  Lead_Routing_Override__c , Trigger_Assignment_Rules__c FROM Lead WHERE id =:cmpmember.LeadId] ; 
              ld.Lead_Routing_Override__c = 'Mid Market';
              ld.Trigger_Assignment_Rules__c= true;
               ldlist = new List<Lead>();
              ldlist.add(ld);
        }      
    }
    if(ldlist !=null && ldlist.size()>0){
        upsert ldlist;
    }
}

 can anyone help me figure out what im missing to get the end result.

 

thx

Rajesh SriramuluRajesh Sriramulu

Hi,

 

Try this,

 

trigger Lead_Campaign_Mid_Market on CampaignMember(after insert, after update) {

List<Lead> ldlist = new List<Lead>();
set<id> idset = new set<id>();


for(CampaignMember cmm : Trigger.new)
{
if(cmm.GEO__c=='Mid Market' && LeadId !=null)
idset.add(cmm.leadid);
}


List<Lead> ld = [SELECT id, Lead_Routing_Override__c , Trigger_Assignment_Rules__c FROM Lead WHERE id IN =:Idset];


for(lead ldd : ld)
{

ldd.Lead_Routing_Override__c = 'Mid Market';
ldd.Trigger_Assignment_Rules__c= true;

ldlist.add(ldd);
}

if(ldlist !=null && ldlist.size()>0){
upsert ldlist;
}
}

 

Please let me know if any queries,

 

Regards,

Rajesh.

impalacrazy69impalacrazy69

Thanks Rajesh for the reply.

 

So put in the code but got the folloiwng errors when trying to save it:

 

If i remove the '=' sign in error number 2 i get this message when i try to save...

cmm.GEO__c=='Mid Market' error: Invalid field GEO__C on campaign memeber object.

     * so the GEO__c field is located on the campaign itself which is what i think i need to delcare as well

 

If i leave the '=' in error 1 goes away and i get this message

WHERE id IN=:Idset]; error: unexpected token '='

 

Should i be using the IN function in the soql statement to ge tthe lsit for lead. shouldnt the where just be where id ="Idset;

 

 

impalacrazy69impalacrazy69

So this i what ive added to try and pull the campaign id and geo but now im getting a :Illegal assignment from ID to list<campiang>

 

trigger Lead_Campaign_Mid_Market on CampaignMember(after insert, after update) {

List<Lead> ldlist = new List<Lead>();
List<Campaign> campid = new List<Campaign>();
set<id> idset = new set<id>();


for(CampaignMember cmm : Trigger.new)
{
	if(cmm.CampaignId !=null)
	campid = cmm.CampaignId;
	String campGeo = [Select id, GEO__C from Campaign Where id=:campid].GEO__C;
	
	
if(campGeo=='Mid Market' && Lead.Id !=null)
idset.add(cmm.leadid);
}

List<Lead> ld = [SELECT id, Lead_Routing_Override__c , Trigger_Assignment_Rules__c FROM Lead WHERE id =:Idset];


for(lead ldd : ld)
{
ldd.Lead_Routing_Override__c = 'Mid Market';
ldd.Trigger_Assignment_Rules__c= true;

ldlist.add(ldd);
}

if(ldlist !=null && ldlist.size()>0){
upsert ldlist;
}
}

 

Rajesh SriramuluRajesh Sriramulu

Hi,

 

Try this, and if u got any error means plz specify the the line in red color and and also post  error message.

 

trigger Lead_Campaign_Mid_Market on CampaignMember(after insert, after update) {
List<Lead> ldlist = new List<Lead>();
set<id> idset = new set<id>();

for(CampaignMember cmm : Trigger.new)
{
if(cmm.GEO__c =='Mid Market' && LeadId !=null)
idset.add(cmm.leadid);
}

List<Lead> ld = [SELECT id, Lead_Routing_Override__c , Trigger_Assignment_Rules__c FROM Lead WHERE id IN:Idset];


for(lead ldd : ld)
{
ldd.Lead_Routing_Override__c = 'Mid Market';
ldd.Trigger_Assignment_Rules__c= true;

ldlist.add(ldd);
}

if(ldlist !=null && ldlist.size()>0){
upsert ldlist;
}
}

 

Regards,

Rajesh.

impalacrazy69impalacrazy69

Hey Rajesh,

 

so they asked for a small change in the process. So I had to break it up into 2 for loops one to get the ID and some info and the other to add data into the lead if they are added to the matching campaign.

 

So it works great in adding 'Mid Market' into my lead routing field but for some reason i cannot get it to update the checkbox so that it reads true. Can you see what im missing to get the checkbox on the lead record to be true. Im not getting any errors either so i dont see why its not being flagged.

 

trigger Lead_Campaign_Mid_Market on CampaignMember(before update, after insert, after update) {

List<Lead> ldlist = new List<Lead>();

set<id> campaignIDs = new set<id>();
set<id> leadIDs = new set<id>();

for(CampaignMember cmm : Trigger.new)
{
	
campaignIDs.add(cmm.CampaignID);
	

}

Map<ID,Campaign> campaignMap = new Map<ID,Campaign>([Select id, GEO__C from Campaign Where id IN :campaignIDs]);


for(CampaignMember cmm : Trigger.new)
{
	if (campaignMap.get(cmm.CampaignID).GEO__c =='Mid Market'){
		Lead L =new Lead(id=cmm.LeadID, Lead_Routing_Override__c = 'Mid Market');
		l.Trigger_Assignment_Rules__c = true;
		ldlist.add(L);
	}
}

if(ldlist.size()>0){
	update ldlist;
}

 

Rajesh SriramuluRajesh Sriramulu

Hi,

 

Please share the details of fields to be updated of particular object to be update and based on condition on particular object.

 

As u mentioned that meets the scenario in above trigger as I mentioned. If not means please specify briefly.

 

Regards,

Rajesh.