0

I have suppose achild object 'A' to Lead.Now if I convert Lead ,it is converted to Account ,opportunity.My requirement is that the on Lead conversion the 'A' object should be attached to the Account and Opportunity as a child object. What is the feasible solution

1 Answer 1

0

@rashi

You can achieve this using Process Builder. Apex code solution will be to write a trigger on Lead. In below example we are relining child cases on lead to account. You can modify the code as per your requirement. and I will leave the troubleshooting of syntax error up to you :)

    trigger LeadConvert on Lead (after update) {
    //try{
       LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
       System.debug('>>>>>Converted Status: '+convertStatus.MasterLabel);
       List<Database.LeadConvert> leadsToBeConverted = new List<Database.LeadConvert>();
       Database.LeadConvert leadConvert;

       for(Lead ld : trigger.new){
            leadConvert = new database.LeadConvert();
              leadConvert.setLeadId(ld.Id);
              leadConvert.setConvertedStatus(convertStatus.MasterLabel);

              leadsToBeConverted.add(leadConvert);
          }
          System.debug('Lead Id: ' + ld.Name + 'Owner Id ' + ld.OwnerId);
      }

      List<Database.LeadConvertResult> leadConvertResults = Database.ConvertLead( leadsToBeConverted );

      list<Case> caselist = new list<Case>();


      set<id> convertedleadids = new set<id>();

      for(Database.LeadConvertResult lCR : leadConvertResults){
          System.debug('Lead Id: ' + lCR.getLeadId() + '; Converted Status: ' + lCR.isSuccess());
          convertedleadids.add(lCR.getLeadId());
      }
      // Lead_Name__c is lookup filed to lead
   caselist = [select Lead_Name__c, Lead_Name__r.ConvertedAccountId, Lead_Name__r.ConvertedContactId , AccountId from Case where Lead_Name__c in : convertedleadids];
    System.debug('caselist '+caselist );  
    list<Case> updatecases = new list<Case>();
    for(Case cs: caselist){
        cs.AccountId= cs.Lead_Name__r.ConvertedAccountId;
        cs.ContactId= cs.Lead_Name__r.ConvertedContactId ;
        cs.Lead_Name__c = null;
        updatecases.add(cs);
    }
    System.debug('updatecases'+updatecases);
    update updatecases;

You must log in to answer this question.

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