You need to sign in to do that
Don't have an account?
StevekBlueSnap
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?
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.
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
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
{
//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());
}
}
}
}
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
Thanks! I'll give that a try
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?
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.
that's still what I'm trying to determine. Would I use a field name?
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:
...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.