0

We are trying to add some custom code in lead conversion process. When converting a lead, we need to generate a record of a custom child object or Account?

I am not thinking about a custom lead conversion process, but is there a way to generate something into the code at all?

1 Answer 1

1

Yes you have options as below:

  1. Create a custom page/component which has inputs for lead object. Then have below code in apex:
    // fields on myLead object
    insert myLead;

    Database.LeadConvert lc = new Database.LeadConvert();
    lc.setLeadId(myLead.id);

    LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
    lc.setConvertedStatus(convertStatus.MasterLabel);

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

Further read: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dml_convertLead.htm. You have standard methods to get account id and contact id. Also you can programmatically create contact or account and set it.

  1. Just implement a trigger on lead convert.
    trigger LeadConvert on Lead (after update) {

      // no bulk processing; will only run from the UI
      if (Trigger.new.size() == 1) {

        if (Trigger.old[0].isConverted == false && Trigger.new[0].isConverted == true) {

          // if a new account was created
          if (Trigger.new[0].ConvertedAccountId != null) {

            // update the converted account with some text from the lead
            Account a = [Select a.Id, a.Description From Account a Where a.Id = :Trigger.new[0].ConvertedAccountId];
            a.Description = Trigger.new[0].Name;
            update a;

          }          

          // if a new contact was created
          if (Trigger.new[0].ConvertedContactId != null) {

            // update the converted contact with some text from the lead
            Contact c = [Select c.Id, c.Description, c.Name From Contact c Where c.Id = :Trigger.new[0].ConvertedContactId];
            c.Description = Trigger.new[0].Name;
            update c;

            // insert a custom object associated with the contact
            MyObject obj = new MyObject();
            obj.Name = c.Name;
            obj.contact__c = Trigger.new[0].ConvertedContactId;
            insert obj;

          }

          // if a new opportunity was created
          if (Trigger.new[0].ConvertedOpportunityId != null) {

            // update the converted opportunity with some text from the lead
            Opportunity opp = [Select o.Id, o.Description from Opportunity o Where o.Id = :Trigger.new[0].ConvertedOpportunityId];
            opp.Description = Trigger.new[0].Name;
            update opp;

            // add an opportunity line item
            OpportunityLineItem oli = new OpportunityLineItem();
            oli.OpportunityId = opp.Id;
            oli.Quantity = 1;
            oli.TotalPrice = 100.00;
            oli.PricebookEntryId = [Select p.Id From PricebookEntry p Where CurrencyIsoCode = 'USD' And IsActive = true limit 1].Id;
            insert oli;

          }         

        }

      }     

    }

Taken from : https://blog.jeffdouglas.com/2009/02/13/enhancing-the-lead-convert-process-in-salesforce/.
You can also create other custom child objects in lead trigger.

You must log in to answer this question.

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