+ Start a Discussion
NecroGigglesNecroGiggles 

Trigger not working on insert

I am having a problem with the before insert part of this trigger I think. The problem is  when a new lead or contact is added to the campain member object. The below trigger does not work it only seems to work right when a update to a exsting campain record object. I dont know were to start trying to fix it. 
 
trigger CampaignMemberTrigger on CampaignMember (before insert, before update) {
// Get a list of fields from the sObject Marketing_KPI_Target__c and name the list kpiTargetList
List<Marketing_KPI_Target__c> kpiTargetList = [SELECT Type_of_Sale__c, ECI_Product__c, CurrencyIsoCode,KPI_Target_Date__c FROM Marketing_KPI_Target__c];

for (CampaignMember member : Trigger.New) {
    for (Marketing_KPI_Target__c kpiTarget : kpiTargetList) {
        if (kpiTarget.Type_of_Sale__c == member.Type_of_Sale__c &&
            kpiTarget.ECI_Product__c == member.ECI_Product__c &&
            kpiTarget.CurrencyIsoCode == member.Currency__c &&
            kpiTarget.KPI_Target_Date__c.month() == member.Response_Tracking_Date__c.month() &&
            kpiTarget.KPI_Target_Date__c.year() == member.Response_Tracking_Date__c.year() &&
            member.HasResponded == True)
        {
            member.Marketing_KPI_Target__c = kpiTarget.Id;
            continue;
        }
    }
}
}

 
Best Answer chosen by NecroGiggles
NecroGigglesNecroGiggles
I got it this is what i eneded up with and it works for both update and new records. Thank you for you help PrateekSinghSengar.
 
trigger CampaignMemberTrigger on CampaignMember (after insert,before update) {
List<Marketing_KPI_Target__c> kpiTargetList = [SELECT Type_of_Sale__c, ECI_Product__c, CurrencyIsoCode,KPI_Target_Date__c FROM Marketing_KPI_Target__c];
List<CampaignMember> cmUpd = new List<CampaignMember>();
{
    if (Trigger.isBefore){
        if(Trigger.isUpdate) 
    for (CampaignMember member : Trigger.New) {
    for (Marketing_KPI_Target__c kpiTarget : kpiTargetList) {
        if (kpiTarget.Type_of_Sale__c == member.Type_of_Sale__c &&
            kpiTarget.ECI_Product__c == member.ECI_Product__c &&
            kpiTarget.CurrencyIsoCode == member.Currency__c &&
            member.Response_Tracking_Date__c != Null &&
            kpiTarget.KPI_Target_Date__c.month() == member.Response_Tracking_Date__c.month() &&
            kpiTarget.KPI_Target_Date__c.year() == member.Response_Tracking_Date__c.year() &&
            member.HasResponded == True)
        {
            member.Marketing_KPI_Target__c = kpiTarget.Id;
            continue;
        }            
    }   
    }
    }
}
        if (Trigger.isAfter){
        if(Trigger.isInsert)     
            
    for (CampaignMember member1 : Trigger.New) {
    for (Marketing_KPI_Target__c kpiTarget : kpiTargetList) {
        if (kpiTarget.Type_of_Sale__c == member1.Type_of_Sale__c &&
            kpiTarget.ECI_Product__c == member1.ECI_Product__c &&
            kpiTarget.CurrencyIsoCode == member1.Currency__c &&
            member1.Response_Tracking_Date__c != Null &&
            kpiTarget.KPI_Target_Date__c.month() == member1.Response_Tracking_Date__c.month() &&
            kpiTarget.KPI_Target_Date__c.year() == member1.Response_Tracking_Date__c.year() &&
            member1.HasResponded == True)
        {
            //create instance of campaignmember
            CampaignMember cm = new CampaignMember(id = member1.Id, Marketing_KPI_Target__c = kpiTarget.Id);
            cmUpd.add(cm);
            continue;
        }
    }
}
}
update cmUpd;
}

 

All Answers

Prateek Singh SengarPrateek Singh Sengar
Hi NecroGiggles,
I believe that you are selected the responded option when adding a campaign member. Can you try converting the trigger from before insert to after insert. I am assuming that the hasResponded flag is set implicitly by salesforce as part of insert. Therefore its not set to true on before trigger. 
NecroGigglesNecroGiggles
Prateek,
When I try setting it to after insert i get a error 
"execution of AfterInsert caused by: System.FinalException: Record is read-only: Trigger.CampaignMemberTrigger: line 15, column 1""

I think it had to do with Trigger.new on line 05 but i dont know what to change to get around it.
Prateek Singh SengarPrateek Singh Sengar
Hi Necro,
Yes Trigger.New is read only in after insert trigger, in after insert trigger you need to do a manual update 
  • Create a list of type CampaignMember
  • Add the records to this list
  • in last statement perform an update 
NecroGigglesNecroGiggles
I am not a 100% sure how to do that and have a few questions. 
1 How do a limit how many recoreds are added to the list ? can I use trigger.new as a limit or SOQL query, maybe  I want to use something like this 
Set<ID> ids = Trigger.newMap.keySet();
   List<CampaignMember > c = [SELECT Id FROM CampaignMember WHERE CampaignMember in :ids];

2 what would the update stament look like ? 
Prateek Singh SengarPrateek Singh Sengar
Try this
 
trigger CampaignMemberTrigger on CampaignMember (after insert) {
List<Marketing_KPI_Target__c> kpiTargetList = [SELECT Type_of_Sale__c, ECI_Product__c, CurrencyIsoCode,KPI_Target_Date__c FROM Marketing_KPI_Target__c];

List<CampaignMember> cmUpd = new List<CampaignMember>();

for (CampaignMember member : Trigger.New) {
    for (Marketing_KPI_Target__c kpiTarget : kpiTargetList) {
        if (kpiTarget.Type_of_Sale__c == member.Type_of_Sale__c &&
            kpiTarget.ECI_Product__c == member.ECI_Product__c &&
            kpiTarget.CurrencyIsoCode == member.Currency__c &&
            kpiTarget.KPI_Target_Date__c.month() == member.Response_Tracking_Date__c.month() &&
            kpiTarget.KPI_Target_Date__c.year() == member.Response_Tracking_Date__c.year() &&
            member.HasResponded == True)
        {
            //create instance of campaignmember
            CampaignMember cm = new CampaignMember(id = member.Id, Marketing_KPI_Target__c = kpiTarget.Id);
            cmUpd.add(cm);
            continue;
        }
    }
}

update cmUpd;
}

 
Prateek Singh SengarPrateek Singh Sengar
Hi NecroGiggles,
Please update back if it works, I am curious to know if my assymption on hasresponded flag is true or not. :)
NecroGigglesNecroGiggles
Pateek,
the new trigger works great for on insert but not on update.
If i add before update i get error 
"CampaignMemberTrigger: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 00vW0000002iNpfIAE; first error: SELF_REFERENCE_FROM_TRIGGER, Object (id = 00vW0000002iNpf) is currently in trigger CampaignMemberTrigger, therefore it cannot recursively update itself: []: Trigger.CampaignMemberTrigger: line 23, column 1"

if I change it to after update i get error
"execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 00vW0000002iNpfIAE; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, CampaignMemberTrigger: maximum trigger depth exceeded CampaignMember trigger event AfterUpdate for [00vW0000002iNpf] CampaignMember trigger event AfterUpdate for [00vW0000002iNpf] CampaignMember trigger event AfterUpdate for [00vW0000002iNpf] CampaignMember trigger event AfterUpdate for [00vW0000002iNpf] CampaignMember trigger event AfterUpdate for [00vW0000002iNpf] CampaignMember trigger event AfterUpdate for [00vW0000002iNpf] CampaignMember trigger event AfterUpdate for [00vW0000002iNpf] CampaignMember trigger event AfterUpdate for [00vW0000002iNpf] CampaignMember trigger event AfterUpdate for [00vW0000002iNpf] CampaignMember trigger event AfterUpdate for [00vW0000002iNpf] CampaignMember trigger event AfterUpdate for [00vW0000002iNpf] CampaignMember trigger event AfterUpdate for [00vW0000002iNpf] CampaignMember trigger event AfterUpdate for [00vW0000002iNpf] CampaignMember trigger event AfterUpdate for [00vW0000002iNpf] CampaignMember trigger event AfterUpdate for [00vW0000002iNpf] CampaignMember trigger event AfterUpdate for [00vW0000002iNpf]: []: Trigger.CampaignMemberTrigger: line 23, column 1"

Can you in the trigger have a if statement that is on after insert run this code and on before update run this ? I know i could just have both parts of the code in 2 diffrent tirggers if thats best but I would like to keep it to 1 tirgger if i can. 
NecroGigglesNecroGiggles
I got it this is what i eneded up with and it works for both update and new records. Thank you for you help PrateekSinghSengar.
 
trigger CampaignMemberTrigger on CampaignMember (after insert,before update) {
List<Marketing_KPI_Target__c> kpiTargetList = [SELECT Type_of_Sale__c, ECI_Product__c, CurrencyIsoCode,KPI_Target_Date__c FROM Marketing_KPI_Target__c];
List<CampaignMember> cmUpd = new List<CampaignMember>();
{
    if (Trigger.isBefore){
        if(Trigger.isUpdate) 
    for (CampaignMember member : Trigger.New) {
    for (Marketing_KPI_Target__c kpiTarget : kpiTargetList) {
        if (kpiTarget.Type_of_Sale__c == member.Type_of_Sale__c &&
            kpiTarget.ECI_Product__c == member.ECI_Product__c &&
            kpiTarget.CurrencyIsoCode == member.Currency__c &&
            member.Response_Tracking_Date__c != Null &&
            kpiTarget.KPI_Target_Date__c.month() == member.Response_Tracking_Date__c.month() &&
            kpiTarget.KPI_Target_Date__c.year() == member.Response_Tracking_Date__c.year() &&
            member.HasResponded == True)
        {
            member.Marketing_KPI_Target__c = kpiTarget.Id;
            continue;
        }            
    }   
    }
    }
}
        if (Trigger.isAfter){
        if(Trigger.isInsert)     
            
    for (CampaignMember member1 : Trigger.New) {
    for (Marketing_KPI_Target__c kpiTarget : kpiTargetList) {
        if (kpiTarget.Type_of_Sale__c == member1.Type_of_Sale__c &&
            kpiTarget.ECI_Product__c == member1.ECI_Product__c &&
            kpiTarget.CurrencyIsoCode == member1.Currency__c &&
            member1.Response_Tracking_Date__c != Null &&
            kpiTarget.KPI_Target_Date__c.month() == member1.Response_Tracking_Date__c.month() &&
            kpiTarget.KPI_Target_Date__c.year() == member1.Response_Tracking_Date__c.year() &&
            member1.HasResponded == True)
        {
            //create instance of campaignmember
            CampaignMember cm = new CampaignMember(id = member1.Id, Marketing_KPI_Target__c = kpiTarget.Id);
            cmUpd.add(cm);
            continue;
        }
    }
}
}
update cmUpd;
}

 
This was selected as the best answer