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

I Have Tried in So Many Ways, I am Not able to find out the Answer.

This is My tried Trigger Code:

trigger myfirstlead100 on Lead (after insert) 
{

      for(lead cf:trigger.new)
       {

            lead cd= new lead(id=cf.id);
            Database.LeadConvert lc = new database.LeadConvert();
            lc.setLeadId(cd.id);
            lc.setDoNotCreateOpportunity(true);
            Database.LeadConvertResult lcr = Database.convertLead(lc);
            System.assert(lcr.isSuccess());
            Profile pf = [Select id,name from Profile where name ='test' limit 1];
            Contact con = [select id,name,email,lastname,firstname,accountid from contact where id=:lcr.getcontactid()limit 1];


    }
}

Please Anyone Guide Me for the Answer

  • Are you converting the lead on insert? – Tushar Sharma Sep 13 '16 at 8:46
  • yes @TusharSharma while Inserting – SFDC Sep 13 '16 at 8:47
0

Here is code sample which will help you. Few points I write this code in console and didn't test so there might be few small bugs. Also if there is multiple match that case I didn't handle here.

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 from Account Where Name IN: cmpnyName])
        cmpnyAccMap.put(acc.name, Acc.id);
    for(contact con: [Select Id from contact Where LastName IN: cmpnyName])
        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);
        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());
    }
}
  • Error: Invalid Data. Review all error messages below to correct your data. Apex trigger myfirstlead100 caused an unexpected exception, contact your administrator: myfirstlead100: execution of AfterInsert caused by: System.DmlException: ConvertLead failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Validation error on Account: key is already Exist: []: Trigger.myfirstlead100: line 33, column 1 – SFDC Sep 13 '16 at 9:32
  • @user36188 there are some validation rule. Check them. – Tushar Sharma Sep 13 '16 at 9:34
  • @user36188 this code only have 32 lines and you are getting exception in line 33. Share the code you are using,.. – Tushar Sharma Sep 13 '16 at 9:44
  • For Account is Working ,Contact is Not Working – SFDC Sep 13 '16 at 9:48
  • Accountid is pointing,Contactid is not pointing,Guide me Tushar Sharma – SFDC Sep 13 '16 at 10:00

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.