• Charni Wiggins 9
  • NEWBIE
  • 15 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 3
    Replies

I am creating a trigger which upon creation of a lead, searches for any contacts with a matching email, and if found - convert the lead, and set the ContactId on the lead. I am just having some trouble figuring out how to create a map, I think it should be <LeadId, ContactId> and then pass this to the Database.leadconvert class to set the contactId.
Forgive me for my code - admin trying to be a dev. Thank you for any help!

trigger convertLead on Lead (before insert) {

    Set<String> formLeads = new Set<String>();
    // Loop through leads, store in set when FORM == TRUE
    for(Lead l : Trigger.New) {
        if(l.Form_Submission_ADMIN_USE_ONLY__c == True) {
            l.Description = 'Trigger ran';
            formLeads.add(l.email);  
        }      
    }  

    // List of contacts - Select contacts when email IN emailstocompare
    List<Contact> matchingCons = [SELECT Id, Email FROM Contact 
                                    WHERE Email IN :formLeads];
    // Add cons to set
    Set<Id> conIds = new Set<Id>();
    // loop through contacts, add to set 
    for(Contact cons:matchingCons) {
        conIds.add(cons.Id);
    }

    /* START Convert Lead Stuff */
    LeadStatus convertStatus = [SELECT MasterLabel FROM LeadStatus 
                                WHERE IsConverted = true LIMIT 1];
    List<Database.LeadConvert> leadsConvert = new List<Database.LeadConvert>();
    /* END Convert Lead Stuff */

    // if lead and contact match
    if(conIds.size()>0) {

        Map<Id, Set<Id>> leadtoConvert = new Map<Id, Set<Id>>();
        // construct map lead id to contact id - set in SetContactId method
        for(Lead l : Trigger.New) {
            leadtoConvert.put(l.Id, conIds);

            // convert lead
            Database.LeadConvert lc = new Database.leadConvert();
            lc.setLeadId(l.Id);
            lc.setConvertedStatus(convertStatus.Masterlabel);
            lc.SetContactId(leadtoConvert.get(l.Id));

            database.LeadConvertResult lcr = Database.convertLead(lc); 
            System.assert(lcr.isSuccess());
        }


    }       

    // loop through contacts, do update

}
 

 

Hi,

We have many web-to-lead forms and many repeat customers, it would be great to have a trigger which checks if the lead already exists as a contact, and if so convert the lead and update contact if needed. 
Apologies for the bad code, I am an admin so struggling to get this right. 

Here is my attempt so far.. 

trigger ConvertLead on Lead (before insert) {

    // every new lead - check if exists as contact via email
    List<String> leadEmails = new List<String>();

    for(Lead lead:Trigger.new){
        leadEmails.add(lead.Email);
        
    }

    // Search existing contacts with matching email
    List<Contact> contacts = [
        SELECT 
            Id, Email 
        FROM 
            Contact
        WHERE 
            Email IN :leadEmails
    ];

    // set of con emails if exists - create map instead? con id to lead id 
    Set<Id> contactIds = new Set<Id>();
    for(Contact contact:contacts){
        contactIds.add(contact.Id);
    }

    Map<Id, Id> conIds = new Map<Id, Id>();
    
    
    for(Lead lead:Trigger.new){
        // if cons exists - map con id to lead converted con id? - setcontactId
        //                       - set lead to converted - bypass GDPR rules
        if(contactIds.contains(lead.Email)){            
			//LeadConvert.setContactId(contactIds);
            Database.LeadConvert lc = new Database.LeadConvert();
            lc.setLeadId(lead.Id);
            lc.setContactId(conIds.get(contactIds).Id);
        }
    }
    
    // update leads and cons outside for loop
    
    
}
Thanks so much in advance! 
Hi,

There is one class I am trying to increase code coverage for. I am an admin so am struggling a little! Would appreciate any help! I need to add an assertion (I think), to check the account record type, after the test ends. These lines in the class are not covered. 
The class converts all leads and sets the acc record type according to a custom lead field. 

Test class:
 
/***********************************************************************************************************************
Date       : 20-03-16
Description: This is the test class for handler class and Lead Trigger
- Data validation for all types of leads , Relating to contacts and accounts based on Email and Company
- Creating Centre Visits for Vistor Registraion Type
- Creating Events and Event Registartion for Event brite
*************************************************************************************************************************/


@isTest
private Class LeadTriggerHandler_Test
{

    private static TestMethod void createLead()
    {
        Profile p = [SELECT Id FROM Profile WHERE Name='XXX'];

        User u = new User(Alias = 'standt', Email='standarduser34@testorg.com',EmailEncodingKey='UTF-8', LastName='Tester', LanguageLocaleKey='en_US',LocaleSidKey='en_US', ProfileId = p.Id, TimeZoneSidKey='America/Los_Angeles', UserName='standarduser34@testorg.com');
        
        System.runAs(u) {

        Test.startTest();
        Lead_Source__c source=new Lead_Source__c(); // Checking for Lead Source
        source.Name ='Pitstop';
        insert source;
        
        Camapaign_Location__c location=new Camapaign_Location__c(); // Campaign Location 
        location.Name ='London newsletter';
        location.Location__c ='london';
        insert location;
        
        List<Account> lstAccount=new List<Account>();
        for(Integer i=0;i<4;i++)
        {    
            Account acc=new Account(Name ='demo'+i);
            lstAccount.add(acc);
        }
        insert lstAccount;
        
        List<Contact> lstContact=new List<Contact>();
        for(Integer i=0;i<1;i++)
        {    
            Contact c=new Contact(FirstName = 'test', LastName = 'test', Email = 'test@test.com', AccountId = lstAccount[0].Id);
            lstContact.add(c);
        }
        insert lstContact;
        
        
        List<Account_Names__c> lstAccNames=new List<Account_Names__c>();
        Account_Names__c defaultaccName=new Account_Names__c(Name ='Default',Id__c =lstAccount[0].Id);
        Account_Names__c digitalaccName=new Account_Names__c(Name ='Digital C Visitors',Id__c =lstAccount[1].Id);
        lstAccNames.add(defaultaccName);
        lstAccNames.add(digitalaccName);
        
        insert lstAccNames;
        
        
        Campaign campaign=new Campaign();
        campaign.Name = 'London newsletter';
        insert campaign;
        
        
        DC_Events__c events=new DC_Events__c(Event_Brite_ID__c ='1234',Name ='test');
        insert events;
        
        
        List<Lead> lstLead=new List<Lead>();
        final String chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
        for(Integer i=0;i<3;i++)
        {
            Lead lead=new Lead();
            lead.LastName ='test'+chars.substring(i, i+1);
            lead.Email = 'test'+i+'@test.com';
            lead.FirstName ='testlead';
            lead.Company ='test'+i;
            lead.Company_turnover__c = 0;
            lead.numberofemployees = 0;
            lead.LeadSource ='Pitstop';
            if(i== 0)
            {
                lead.recordtypeId = Schema.SobjectType.Lead.getRecordTypeInfosByName().get('General Lead').getRecordTypeId();    
                lead.Visiting__c =UserInfo.getName();
                lead.Centre_Location__c ='london';
                lead.Newsletter_OptIn__c ='Yes';
                lead.Event_Location__c ='london';
            }else if(i==1 || i==2)
            {
                lead.recordtypeId = Schema.SobjectType.Lead.getRecordTypeInfosByName().get('General Lead').getRecordTypeId();
                if(i==1)
                {
                    lead.Eventbrite_ID__c ='1234';
                }else{
                    lead.Eventbrite_ID__c ='12345';
                }
                //lead.Event_Location__c ='london';
                lead.Centre_Location__c ='london';
                lead.Newsletter_OptIn__c ='Yes';
            }
            lstLead.add(lead);
        }
        
        Lead lead=new Lead();
        lead.firstName ='test';
        lead.LastName ='test';
        lead.Email = 'test@test.com';
        lead.Company ='NA';
        lead.Company_turnover__c = 5000000;
        lead.numberofemployees = 5;
        lead.LeadSource ='Pitstop';
        lead.RecordTypeId = Schema.SobjectType.Lead.getRecordTypeInfosByName().get('General Lead').getRecordTypeId();
        lstLead.add(lead);
        
        Lead lead1=new Lead();
        lead.firstName ='test';
        lead1.LastName ='test';
        lead1.Email = 'test@test.com';
        lead1.Company ='demo0';
        lead.Company_turnover__c = 9000000;
        lead.numberofemployees = 9;
        lead1.LeadSource ='Pitstop';
        lead1.Organisation_Type__c ='Public Sector';
        lstLead.add(lead1);
            
        Lead lead2=new Lead();
        lead.firstName ='test';
        lead1.LastName ='test';
        lead1.Email = 'test@test.com';
        lead1.Company ='demo1';
        lead.Company_turnover__c = 9000000;
        lead.numberofemployees = 9;
        lead1.LeadSource ='Pitstop';
        lead1.Organisation_Type__c ='Tech Startups and Scaleups';
        lstLead.add(lead2);
            
        Lead lead3=new Lead();
        lead.firstName ='test';
        lead1.LastName ='test';
        lead1.Email = 'test@test.com';
        lead1.Company ='demo2';
        lead.Company_turnover__c = 9000000;
        lead.numberofemployees = 9;
        lead1.LeadSource ='Pitstop';
        lead1.Organisation_Type__c ='Industry';
        lstLead.add(lead3);
            
        Lead lead4=new Lead();
        lead.firstName ='test';
        lead1.LastName ='test';
        lead1.Email = 'test@test.com';
        lead1.Company ='demo3';
        lead.Company_turnover__c = 9000000;
        lead.numberofemployees = 9;
        lead1.LeadSource ='Online Application';
        lead1.Organisation_Type__c ='Small or Medium Enterprise';
        lstLead.add(lead4);
        
        insert lstLead;
        Test.StopTest();
            
            
    }
  }
}

Thank you!!
We recently had a contractor develop a simple API for us, who has since left and the endpoint has changed. I believe I have changed the endpoint where I need to. Is it possible to trigger the API via the dev console to test it? I was thinking using execute anonymous window. Would appreciate any help, as an admin! Thank you
The screenshot below shows that there is no ContentDocId, but the Decision still reads it as not null? I just want to perform an update afterwards, if the file has been uploaded. Thank you

User-added image

Hi,

We have many web-to-lead forms and many repeat customers, it would be great to have a trigger which checks if the lead already exists as a contact, and if so convert the lead and update contact if needed. 
Apologies for the bad code, I am an admin so struggling to get this right. 

Here is my attempt so far.. 

trigger ConvertLead on Lead (before insert) {

    // every new lead - check if exists as contact via email
    List<String> leadEmails = new List<String>();

    for(Lead lead:Trigger.new){
        leadEmails.add(lead.Email);
        
    }

    // Search existing contacts with matching email
    List<Contact> contacts = [
        SELECT 
            Id, Email 
        FROM 
            Contact
        WHERE 
            Email IN :leadEmails
    ];

    // set of con emails if exists - create map instead? con id to lead id 
    Set<Id> contactIds = new Set<Id>();
    for(Contact contact:contacts){
        contactIds.add(contact.Id);
    }

    Map<Id, Id> conIds = new Map<Id, Id>();
    
    
    for(Lead lead:Trigger.new){
        // if cons exists - map con id to lead converted con id? - setcontactId
        //                       - set lead to converted - bypass GDPR rules
        if(contactIds.contains(lead.Email)){            
			//LeadConvert.setContactId(contactIds);
            Database.LeadConvert lc = new Database.LeadConvert();
            lc.setLeadId(lead.Id);
            lc.setContactId(conIds.get(contactIds).Id);
        }
    }
    
    // update leads and cons outside for loop
    
    
}
Thanks so much in advance! 
We recently had a contractor develop a simple API for us, who has since left and the endpoint has changed. I believe I have changed the endpoint where I need to. Is it possible to trigger the API via the dev console to test it? I was thinking using execute anonymous window. Would appreciate any help, as an admin! Thank you