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

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
| improve this answer | |
  • 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 '16 at 11:47

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.