we are using a system that integrates with Salesforce called Hubspot that generates contacts and leads. How it works is that when a Hubspot form is filled out, the entry is brought into salesforce. If the email is not associated with any contacts in SF, it creates a leads. If the email IS associated with any contacts, it just updates that existing contact (and ties it to the realted salesforce campaign. I have created a Process Builder process that makes it so whenever one of these contacts is tied to a campaign, that a lead is automatically generated. (The goals is to only have leads coming in from this form).
Since "Campaign" is a lookup field on leads, I am having trouble making so the newly generated lead is associated with the desired campaign. I have not been able to finda way to populate the "Campaign" field on the new leads with a campaign. I have begun a trigger to try and and insert a new lead and campaign member for that new lead but it is coming together poorly and does not work.
trigger LeadGenfromCampaignMember on CampaignMember (after insert) {
CampaignMember cpm = Trigger.new[0];
Lead newLead = new Lead(FirstName = cpm.Contact.FirstName, LastName = cpm.Contact.LastName, Email = cpm.Contact.Email);
CampaignMember newcpm = new CampaignMember(CampaignId = cpm.CampaignId, LeadId = newLead.Id);
if (cpm.Campaign.Provider_Sales_Campaign__c == True){
insert newLead;
insert newcpm;
}
}