You need to sign in to do that
Don't have an account?
KevSnell
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
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.
All Answers
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.
Thanks:
Here is the final code for anyone's information which assigns the lead to a specific campaign: