function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Baguiar2Baguiar2 

Converted lead to match an account Id

Hi,

 

I have pretty much the trigger working, but when the lead is converted and it creates the contact, I need the Contact created to have the same account ID that is on the "acctmatch" list on the trigger. What is happening now is that the conversion is creating a new account and NOT assigning the created Contact to the acctmatch[0].id  that i want.

 

Pretty much, do NOT create a new account and assign the created contact from the conversion, to the Account from the acctmatch SOQL.  here is the code:

 

trigger LeadConvert on Lead (after insert,after update) {

Set<Id> recordIds = new Set<Id>();
List<String> LeadNames = new List<String>{};
List<String> Leaddomain = new List<String>{};
Set<id> acctId = new Set<id>{};

for(Lead myLead: Trigger.new){
 if((myLead.isconverted==false) && (myLead.convertme__c == True) && (myLead.status <> 'Unqualified') && (mylead.email <> Null) && (mylead.autodomain__c <> null) ) {
 
     recordIds.add(myLead.id);
     Leaddomain.add(myLead.autodomain__c);
    
    Account[] acctmatch = [Select id, name, domain__c from account where domain__c in :Leaddomain];
    IF (acctmatch.size() > 0){
      acctId.add(acctmatch[0].id);

Database.LeadConvert lc = new database.LeadConvert();

        lc.setLeadId(myLead.Id);
        lc.convertedStatus = 'Qualified';
        //Database.ConvertLead(lc,true);
        lc.setDoNotCreateOpportunity(true);
        Database.LeadConvertResult lcr = Database.convertLead(lc);
        System.assert(lcr.isSuccess()); 
        
        
        if (myLead.IsConverted) {
            // Assign the value from the Lead to Contact
            Contact con = [SELECT Id FROM Contact WHERE Contact.Id = :mylead.ConvertedContactId];
            con.Accountid = acctmatch[0].id;
            update con;         
        }
        
    }
   }
  }
}

 Thanks! almost there.. ;)

Best Answer chosen by Admin (Salesforce Developers) 
Baguiar2Baguiar2

sorry.. quick fix..

 

just add 

 

lc.accountid = acctmatch[0].id;

 

and remove all the 

 
        if (myLead.IsConverted) {
            // Assign the value from the Lead to Contact
            Contact con = [SELECT Id FROM Contact WHERE Contact.Id = :mylead.ConvertedContactId];
            con.Accountid = acctmatch[0].id;
            update con;         
        }