0

I have a requirement where I have to assign the Contact to the Account Contact Roles as the primary contact,whenever a lead is converted.

But I am not sure how it will be achieved? If it is going to be achieved with the help of a trigger,then on what object should I use the trigger,since lead object gets destroyed after getting converted to Account,Contact & Opportunity.

Please Help.

0

You have to use Database.LeadConvert for converted leads.So i share some code related to auto convert a lead when Lead status = webform. Customize based on your requirements.

trigger AutoConverter on Lead (after insert) {
 LeadStatus convertStatus = [
      select MasterLabel
      from LeadStatus
      where IsConverted = true
      limit 1
 ];
 List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();

 for (Lead lead: Trigger.new) {
      if (!lead.isConverted && lead.WebForm__c == 'Free Trial') {
           Database.LeadConvert lc = new Database.LeadConvert();
           String oppName = lead.Name;

           lc.setLeadId(lead.Id);
           lc.setOpportunityName(oppName);
           lc.setConvertedStatus(convertStatus.MasterLabel);

           leadConverts.add(lc);
      }
 }

 if (!leadConverts.isEmpty()) {
      List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
 }

}

If you face any issues let me know @Shobhit Saxena

| improve this answer | |
  • -1, you clearly did not read the question. OP was not asking how to convert a lead programmatically – Novarg Jun 20 '16 at 13:34
0

Thanks for giving your suggestions @All. Finally I got my task done.Following is the piece of code that worked for me:

trigger PopulateContactRoleOnConversion on Lead (before update) 
{
    PopulateContactRoleOnConversionHandler obj1=new PopulateContactRoleOnConversionHandler ();    //Creating an object of Handler class.
    obj1.onBeforeUpdate(trigger.new);    //Calling the Handler class method


}

The Handler class :

public with sharing class PopulateContactRoleOnConversionHandler 
{
    public void onBeforeUpdate(List<Lead> lstLeads)
    {
        Map<Id,String> leadAccountStatus = new Map<Id,String>(); // Map of the converted AccountID and the Lead Status
        //Map<Id,String> leadContactStatus = new Map<Id,String>(); // Map of the converted ContactID and the Lead Status
        for(Lead lead :lstLeads) 
        {
              if (lead.IsConverted)

              leadAccountStatus.put(lead.ConvertedAccountId,lead.Status);



        }
        List<Account> lstAccounts = [SELECT Id from Account WHERE Id IN :leadAccountStatus.keySet()];

        for ( Account acc : lstAccounts) 
        {
              Contact con=[SELECT Id,Phone,Email FROM Contact where Account.ID =:acc.ID];
              AccountContactRole objAccountContactRole=new AccountContactRole();
              objAccountContactRole.AccountId=acc.Id;
              objAccountContactRole.ContactId=con.Id;
              objAccountContactRole.Role='Business User';
              objAccountContactRole.isPrimary=true;
              insert objAccountContactRole;


        }
    }
}
| improve this answer | |

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.