0

Lead Conversion in Salesforce: Lead is Converted into Account and Contact,if we Want Opportuinty we can set , while Creating the Lead Record,Name Should be Converted to Contact,Company Should be Converted into Account

I Want Trigger Code:If name and Company what you are Giving in the Lead Record is Already Exists,lead should be Converted but Converted name and Company is Pointed out to the Existing Contact and Account Record,No New Contact and Account Should be Created

Answer is:

trigger myfirstlead100 on Lead (after insert) 
{
    Set<String> cmpnyName = new Set<String>();
    Set<String> ledName = new Set<String>();

    map<string,Id> cmpnyAccMap = new map<string,Id>();
    map<string,Id> leadConMap = new map<string,Id>();
    for(lead cf:trigger.new)
    {
        cmpnyName.add(cf.Company);
        ledName.add(cf.Name);
    }

    for(Account acc: [Select Id,Name  from Account Where Name IN: cmpnyName])
        cmpnyAccMap.put(acc.name, Acc.id);
    for(contact con: [Select Id,LastName  from contact Where LastName IN: ledName])
        leadConMap.put(con.LastName,con.id);

    for(lead cf:trigger.new)
    {

        lead cd= new lead(id=cf.id);

        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(cd.id);
         lc.ConvertedStatus ='Closed - Converted';
        if(cmpnyAccMap.containsKey(cf.Company))
            lc.setAccountId(cmpnyAccMap.get(cf.Company));
        if(leadConMap.containsKey(cf.Name))
            lc.setContactId(leadConMap.get(cf.Name));
        lc.setDoNotCreateOpportunity(true);

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

    }
}

I am not getting the any error,Lead is Converting,account is Pointing in the Existing Account ,Contact is Not Pointing existing Contact

Please Anyone Guide Me for the Answer

1 Answer 1

0

In your Lead Name Set, you are adding the full name of the Lead record

ledName.add(cf.Name);

and when you are querying Contacts, you are checking it against the last name

Select Id,LastName from contact Where LastName IN: ledName

you can change your query to match the full name of the contact against the full name of the lead.. then you should see the lead mapping to existing contact if the name matches..

Select Id,LastName from contact Where Name IN: ledName
1
  • I have Changed like this for(contact con: [Select Id,LastName from contact Where Name IN: ledName]) leadConMap.put(con.Name, con.id);
    – SFDC
    Sep 13, 2016 at 11:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .