• Keran Wijetunga
  • NEWBIE
  • 20 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 9
    Replies
Hi,

I've had some help creating some Apex code which is supposed to do the following

Via Web-to-Lead I'm populating a field in our Lead Object called Promo_Code__c. Promo_Code__c has also been setup in our Campaign Object as an External ID. What I want to be able to do, is upon creation/update of a Lead is to cross reference the Promo_Code__c with the relevant Campaign and link the Lead to that Campaign (using Promo_Code__c External ID).

The code below saves okay, and works correctly if I create a Lead Record, save it, then edit it and include the Promo_code__c. It doesn't work correctly however if I create a brand new Lead and input the Promo_Code__c before saving. If I do this, the error code is as follows:

There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger Update_Campaign caused an unexpected exception, contact your administrator: Update_Campaign: execution of BeforeUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, This entity is already a member of this campaign: []: Trigger.Update_Campaign: line 32, column 1".

The Lead Trigger is as follows:

trigger Update_Campaign on Lead (after insert, before update) {

  List<ID> LeadIds = new List<ID>();
  List<String> prStr = new List<String>();
  List<CampaignMember> newList = new List<CampaignMember>();

for(Lead LD: Trigger.new){
    if(LD.Promo_Code__c!=null)
    {
        prStr.add(LD.Promo_Code__c); //Assuming Promo_Code__c is of Text type;
    }
}

 
Map<String,Id> cmMap = new Map<String,ID>();

for(Campaign cm1 :[select id,Promo_Code__c from Campaign where Promo_Code__c IN: prStr])
{
    cmMap.put(cm1.Promo_Code__c,cm1.Id);
}

for(Lead LD: Trigger.new){
    if(LD.Promo_Code__c!=null)
    {
        CampaignMember cm = new CampaignMember(CampaignId=cmMap.get(LD.Promo_Code__c),LeadId=Ld.id);
        newList.add(cm);
    }
}

if(newList.size()>0)
{
    insert newList;
}


Any help would be Much Appreciated!!

Hi All,

I had no knowledge of triggers but I've gien some coding a shot. Via Web-to-Lead I'm populating a field in our Lead Object called Promo_Code__c. Promo_Code__c has also been setup in our Campaign object as an External ID. What I want to be able to do, is upon creation/update of a Lead is to cross reference the Promo_Code__c with the relevant Campaign and link the Lead to that Campaign (using External ID). The trigger code I've written so far is as follows:

trigger Update_Campaign on Lead (before insert, before update) {

  set<ID>sObjectID = new set<ID>();

for(Lead LD: Trigger.new){
  if(LD.Campaign != null){
    sObjectID.add(LD.Campaign);
  }
}

if(!sObjectID.isEmpty()){
  
     Map<ID,Campaign> sObjectMap = new Map<ID,Campaign>([select ID,Name,Promo_Code__c from Campaign where Id IN: sObjectID]);

for(Lead LD : trigger.new){
if(sObjectMap.get(LD.Campaign).Promo_Code__c != Null){
//fill the Campaign on Lead with Promo Name on Campaign Object
LD.Promo_Code__c = sObjectMap.get(LD.Campaign).Promo_Code__c;
}
}
}
}

I get an error "Error: Compile Error: Invalid field Campaign for SObject Lead at line 7 column 19". Any help with this would be nothing short of absolutely awesome
Hi,

I've had some help creating some Apex code which is supposed to do the following

Via Web-to-Lead I'm populating a field in our Lead Object called Promo_Code__c. Promo_Code__c has also been setup in our Campaign Object as an External ID. What I want to be able to do, is upon creation/update of a Lead is to cross reference the Promo_Code__c with the relevant Campaign and link the Lead to that Campaign (using Promo_Code__c External ID).

The code below saves okay, and works correctly if I create a Lead Record, save it, then edit it and include the Promo_code__c. It doesn't work correctly however if I create a brand new Lead and input the Promo_Code__c before saving. If I do this, the error code is as follows:

There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger Update_Campaign caused an unexpected exception, contact your administrator: Update_Campaign: execution of BeforeUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, This entity is already a member of this campaign: []: Trigger.Update_Campaign: line 32, column 1".

The Lead Trigger is as follows:

trigger Update_Campaign on Lead (after insert, before update) {

  List<ID> LeadIds = new List<ID>();
  List<String> prStr = new List<String>();
  List<CampaignMember> newList = new List<CampaignMember>();

for(Lead LD: Trigger.new){
    if(LD.Promo_Code__c!=null)
    {
        prStr.add(LD.Promo_Code__c); //Assuming Promo_Code__c is of Text type;
    }
}

 
Map<String,Id> cmMap = new Map<String,ID>();

for(Campaign cm1 :[select id,Promo_Code__c from Campaign where Promo_Code__c IN: prStr])
{
    cmMap.put(cm1.Promo_Code__c,cm1.Id);
}

for(Lead LD: Trigger.new){
    if(LD.Promo_Code__c!=null)
    {
        CampaignMember cm = new CampaignMember(CampaignId=cmMap.get(LD.Promo_Code__c),LeadId=Ld.id);
        newList.add(cm);
    }
}

if(newList.size()>0)
{
    insert newList;
}


Any help would be Much Appreciated!!

Hi All,

I had no knowledge of triggers but I've gien some coding a shot. Via Web-to-Lead I'm populating a field in our Lead Object called Promo_Code__c. Promo_Code__c has also been setup in our Campaign object as an External ID. What I want to be able to do, is upon creation/update of a Lead is to cross reference the Promo_Code__c with the relevant Campaign and link the Lead to that Campaign (using External ID). The trigger code I've written so far is as follows:

trigger Update_Campaign on Lead (before insert, before update) {

  set<ID>sObjectID = new set<ID>();

for(Lead LD: Trigger.new){
  if(LD.Campaign != null){
    sObjectID.add(LD.Campaign);
  }
}

if(!sObjectID.isEmpty()){
  
     Map<ID,Campaign> sObjectMap = new Map<ID,Campaign>([select ID,Name,Promo_Code__c from Campaign where Id IN: sObjectID]);

for(Lead LD : trigger.new){
if(sObjectMap.get(LD.Campaign).Promo_Code__c != Null){
//fill the Campaign on Lead with Promo Name on Campaign Object
LD.Promo_Code__c = sObjectMap.get(LD.Campaign).Promo_Code__c;
}
}
}
}

I get an error "Error: Compile Error: Invalid field Campaign for SObject Lead at line 7 column 19". Any help with this would be nothing short of absolutely awesome