0

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;
}

}

  • Is the incoming CampaignMember has leadId filled already filled-in? or may be it already has contactId already filled-in. The documentation says, campaignMember can have either LeadId or contactId. – javanoob Aug 20 '15 at 18:46
  • salesforce.stackexchange.com/questions/47193/… Please refer this and let us know if you still have questions..I can definitely help you out on this. – javanoob Aug 20 '15 at 18:49
  • You have many issues here: bulkification, referencing data you haven't queried, order of operations, etc. It needs quite a bit of help. – sfdcfox Aug 20 '15 at 18:51
  • The incoming CampaignMember should only have a contact ID. I am trying to create a brand new Lead and a separate new campaign member for that Lead. The incoming campaign member should only have an associated contact but the new lead should be tied to the campaign with a new campaign member – Chris P Aug 20 '15 at 18:55
  • Yes it does need quite a bit of help I have not written triggers for SFDC before this. – Chris P Aug 20 '15 at 18:59
0
trigger LeadGenfromCampaignMember on CampaignMember (after insert) {
    // Determine initial eligibility
    CampaignMember[] processed = new CampaignMember[0];
    for(CampaignMember record: Trigger.new) {
        if(record.ContactId != null) {
            processed.add(record);
        }
    }
    // Quit if no contacts present
    if(processed.isEmpty()) {
        return;
    }
    // Find eligible campaigns
    Set<Id> campaignIds = new Map<Id, Campaign>(
        [SELECT Id FROM Campaign WHERE Id IN (SELECT CampaignId FROM CampaignMember WHERE Id IN :processed) AND Provider_Sales_Campaign__c = TRUE]
    ).keySet();
    // Quit if no campaigns are eligible
    if(campaignIds.isEmpty()) {
        return;
    }
    // Load associated contacts
    Map<Id, Contact> contacts = new Map<Id, Contact>(
        [SELECT FirstName, LastName, Email, Account.Name FROM Contact WHERE Id IN (SELECT ContactId FROM CampaignMember WHERE Id IN :processed)]
    );
    // Create new Leads, store temporary members
    CampaignMember[] members = new CampaignMember[0];
    Lead[] leads = new Lead[0];
    for(CampaignMember record: Trigger.new) {
        if(contacts.containsKey(record.ContactId) && campaignIds.contains(record.CampaignId)) {
            Contact contactRecord = contacts.get(record.ContactId);
            leads.add(new Lead(FirstName=contactRecord.FirstName, LastName=contactRecord.LastName, Email=contactRecord.Email, Company=contactRecord.Account.Name));
            members.add(new CampaignMember(Campaignid=record.CampaignId, MemberStatus='Sent'));
        }
    }
    insert leads;
    // Associate temporary members with newly created leads
    for(Integer index = 0, size = leads.size(); index < size; index++) {
        members[index].LeadId = leads[index].Id;
    }
    insert members;
}
  • This worked EXQUISITELY @sfdcfox anything I can do to help boost reputation, rank, etc. I am happy to. I am testing this in our sandbox and it is working well on every front. – Chris P Aug 20 '15 at 20:28
0

The problem in your code is that :

You are trying to get the Lead Id before it is inserted to database. Please see the below corrected code:

    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);
    insert newLead; //This line is missing..If you do not insert, you don't get the Id    
    CampaignMember newcpm = new CampaignMember(CampaignId = cpm.CampaignId, LeadId = newLead.Id);



    if (cpm.Campaign.Provider_Sales_Campaign__c == True){
            insert newLead;
            insert newcpm;
    }
    }

But as sfdcfox pointed out you should definitely bulkify the trigger.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.