function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
StevekBlueSnapStevekBlueSnap 

trigger script

I have a trigger code deployed that upon a set parameter, it will automatically convert a Lead to an Account with an Opportunity. What type of additional line of code would I need to add to enable these converted records be attached to existing Accounts rather than simply creating a net new record?

Jeff MayJeff May

Assuming the trigger is on the Lead record.  You will need to use some field on the Lead record so you can find the existing Account. It might be CompanyName, or maybe the domain of the Lead email.  Use that field to query Account and if you find only 1 match, you can set the AccountID as part of the LeadConvert.

 

 

StevekBlueSnapStevekBlueSnap

thanks; I'mstill sorting this out as I'm new to trigger building - someone helped with this and am nowhere near Developer certified. Where and what in this code shoud be added per your suggestion? CompanyName would be most logical but we also employ an external Account # as well

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
trigger LeadConvert on Lead (after insert,after update) 
{
    //Bulkified

    List<Lead> convertRecords = new List<Lead>();

    for(Lead myLead: Trigger.new)
    {
        if((myLead.isconverted==false) && (myLead.Status == 'Confirmed')) 
        {
            convertRecords.add(myLead);
        }
    }

    System.debug('----------------- Convert Records --------------------------'+convertRecords);

    if(convertRecords.size() > 0)
    {
        List<Database.LeadConvert> leadsToConvert = new List<Database.LeadConvert>();

        for(Lead ld : convertRecords)
        {
            Database.LeadConvert lc = new Database.LeadConvert();

            lc.SetLeadId(ld.Id);
            lc.SetConvertedStatus('Confirmed');

            leadsToConvert.add(lc);
        }

        System.debug('----------------- Leads to Convert --------------------------'+leadsToConvert);

        List<Database.LeadConvertResult> lcr = Database.convertLead(leadsToConvert);
        
        for(Database.LeadConvertResult lcx : lcr)
        {
            if(!lcx.isSuccess())
            {
                System.debug('------------------------failed-----------------------------------------------'+lcx.getErrors()); 
            }
            else
            {
                System.debug('--------------------------success---------------------------------------------'+lcx.isSuccess()); 
            }
        }
    }
}
sf_evolutionsf_evolution

 

 

Try this:

(look for the "// NEW" comments)

caveat:   it assigns all our leads to convert to the same account... but it should give you the idea

 

trigger LeadConvert on Lead (after insert,after update)  {  
    //Bulkified  List<Lead> convertRecords = new List<Lead>();  
    
    for(Lead myLead: Trigger.new)  {  
        if((myLead.isconverted==false) && (myLead.Status == 'Confirmed'))   {  
            convertRecords.add(myLead);  
            }  
        }  
    Account a = [Select Id from Account Where (...some criteria is specified...)]  //NEW 
    
    System.debug('----------------- Convert Records --------------------------'+convertRecords);  
    if(convertRecords.size() &gt; 0)  {  
        List&lt;Database.LeadConvert&gt; leadsToConvert = new List&lt;Database.LeadConvert&gt;();  
        for(Lead ld : convertRecords)  {          
            Database.LeadConvert lc = new Database.LeadConvert();  
            lc.SetLeadId(ld.Id);  
            lc.setAccountId(a.Id);   // NEW
            lc.SetConvertedStatus('Confirmed');  
            leadsToConvert.add(lc);  
            }  
        System.debug('----------------- Leads to Convert --------------------------'+leadsToConvert);  List&lt;Database.LeadConvertResult&gt; 
        lcr = Database.convertLead(leadsToConvert);    
        for(Database.LeadConvertResult lcx : lcr)  {  
            if(!lcx.isSuccess())  {  
               System.debug('--------------------    failed-----------------------------------------------'+lcx.getErrors());   
               }  
            else  {  
               System.debug('--------------------------success---------------------------------------------'+lcx.isSuccess());   
               }  
            }  
        } 
    }

 

StevekBlueSnapStevekBlueSnap

Thanks! I'll give that a try

StevekBlueSnapStevekBlueSnap

I found a line to insert at line 27:

lc.setAccountId('001S000000ce8YJ');

however it is sset to attach to only one account

 

how do I edit the ID to assign to the correct related account. when I inserted

lc.setAccountId('001xxxxxxxxxxxx');

at line 27, the trigger saved but when I tried to convert using the trigger, I got this error:

 

caused by: System.StringException: Invalid id: 001xxxxxxxxxxxx

 

Trigger.LeadConvert: line 27, column 1

 

what parameters (after insert, after update) do I need to change or is there something in this line that can be edited or do I need to set a where Account is like parameter?

sf_evolutionsf_evolution

Hi Steve,

 

What criteria are you using on the SOQL call?

If you're trying to hard-code an accountId, that's not a good idea.

 

StevekBlueSnapStevekBlueSnap

that's still what I'm trying to determine. Would I use a field name?

sf_evolutionsf_evolution

Sorry I'm so late with this..   hope it helps.

....But if (and it's a big if... your - lead name matches *exactly* your account company name, you can do something like this:

 

trigger LeadConvert on Lead (after insert,after update)  {  
    //Bulkified  List<Lead> convertRecords = new List<Lead>();  
     Map Ids<Id, String> = new Map <Id, String>();
     
    for(Lead myLead: Trigger.new)  {  
        if((myLead.isconverted==false) && (myLead.Status == 'Confirmed'))   {  
            convertRecords.add(myLead);  
            IDs.put(myLead.Id, myLead.Company);
            }  
        }  
    
    Map AcctIDs<Id, String> = new Map <Id, String>([Select Id, Name from Account Where Name in :convertRecords.values()])  //NEW
    
    
    System.debug('----------------- Convert Records --------------------------'+convertRecords);  
    if(convertRecords.size() > 0)  {  
        List<Database.LeadConvert> leadsToConvert = new List<Database.LeadConvert>();  
        for(Lead ld : convertRecords)  {          
            Database.LeadConvert lc = new Database.LeadConvert();  
            lc.SetLeadId(ld.Id);  
            lc.setAccountId(AcctIDs.get(ld.id));   // NEW
            lc.SetConvertedStatus('Confirmed');  
            leadsToConvert.add(lc);  
            }  
        System.debug('----------------- Leads to Convert --------------------------'+leadsToConvert);  List&lt;Database.LeadConvertResult&gt;
        lcr = Database.convertLead(leadsToConvert);    
        for(Database.LeadConvertResult lcx : lcr)  {  
            if(!lcx.isSuccess())  {  
               System.debug('--------------------    failed-----------------------------------------------'+lcx.getErrors());   
               }  
            else  {  
               System.debug('--------------------------success---------------------------------------------'+lcx.isSuccess());   
               }  
            }  
        }
    }

 

...You'll be matching the account by the company name... dunno if it will compile, bit it should be pretty close, but it's reallyjust a matter of you determining what the criteria is between what's in the lead, and what will match in a *specific*  account.. otherwise you'll me assigning a lead to the incorrect account.