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