+ Start a Discussion
KevSnellKevSnell 

Apex Code Create Lead and Assign to Campaign?

Hi all,

 

I have a visualforce page that creates a new lead, however, how can I get it to add the new lead to a campaign, like a web-to-lead form does.  In a web-to-lead form you would use campaign_id and member_status but that doesn't work in the apex.  I also have a primary_campaign_id__c field in the lead so could I use that and if so how?

 

Here is the apex code:

 

public class Reserve {

    public Lead lead {get; private set;}

    public String company {get; set;} 
    public String email {get; set;}
    public String phone {get; set;}
    public String firstName {get; set;}
    public String lastName {get; set;}
    public String title {get; set;}

    public String getName() {
        return 'Reserve';
    }

    // save button is clicked
    public PageReference save() {
        
        Lead l = new Lead(
        OwnerId = '00GE0000000hCBH',
        LeadSource = 'Reserve',
        FirstName = firstName,
        LastName = lastName,
        Email = email,
        Phone = phone,
        Title = title,
        Company = company,
        List_Assignment__c = 'Reserve',
        Primary_Campaign_ID__c = '701E0000000TwCG'
        );
        
       
    try {
      insert l; // inserts the new record into the database
    } catch (Exception e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Error,'Error adding Reserve List.'));
      return null;
    }
        return Page.confirmationReserveEntry;      
 
    } 
  
}

Thanks

Kev

Best Answer chosen by Admin (Salesforce Developers) 
Sonali BhardwajSonali Bhardwaj

Lead and Campaign are not directly related to each other. There is a middle object CampaignMember in between them. Here is an example how to relate Lead with Campaign.

Campaign c = [Select id from Campaign limit 1];
Lead l = new Lead(lastname='11', company='11');
insert l;
CampaignMember mem = new CampaignMember (campaignid=c.id, leadid=l.id);
insert mem;

 

All Answers

Sonali BhardwajSonali Bhardwaj

Lead and Campaign are not directly related to each other. There is a middle object CampaignMember in between them. Here is an example how to relate Lead with Campaign.

Campaign c = [Select id from Campaign limit 1];
Lead l = new Lead(lastname='11', company='11');
insert l;
CampaignMember mem = new CampaignMember (campaignid=c.id, leadid=l.id);
insert mem;

 

This was selected as the best answer
KevSnellKevSnell

Thanks:

 

Here is the final code for anyone's information which assigns the lead to a specific campaign:

 

public class Reserve {

    public Lead lead {get; private set;}

    public String company {get; set;} 
    public String email {get; set;}
    public String phone {get; set;}
    public String firstName {get; set;}
    public String lastName {get; set;}
    public String title {get; set;}

    public String getName() {
        return 'Reserve';
    }

    // save button is clicked
    public PageReference save() {
        
        Lead l = new Lead(
        OwnerId = '00GE0000000hCBH',
        LeadSource = 'Reserve',
        FirstName = firstName,
        LastName = lastName,
        Email = email,
        Phone = phone,
        Title = title,
        Company = company,
        List_Assignment__c = 'Reserve',
        Primary_Campaign_ID__c = '701E0000000TwDT'
     
        );
        
       
    try {
      insert l; // inserts the new record into the database
    } catch (Exception e) {
      ApexPages.addMessage(new ApexPages.message(ApexPages.severity.Error,'Error adding Candidate to Reserve List.'));
      return null;
    }
          
     CampaignMember mem = new CampaignMember (campaignid='701E0000000TwDT', leadid=l.id);
     insert mem;
      
        return Page.confirmationReserveEntry;      
    } 
  
}