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
RahulRahul 

Hi Friends, when Iam converting leads, its converted into Account and contact.I have one more trigger on account with the field contact_associated__C which is the lookup to contact.(Please read full in the description)

Hi Friends, when Iam converting leads, its converted into Account and contact.I have one more trigger on account with the field 
contact_associated__C which is the lookup to contact. When account is created it checks if contact_associated__C is null,
if its null, then it takes( name, email and phone) from account and creates a contact and that is associated with 
this contact_associated__C. But now the problem is when Iam converting the lead , its creating account and contact,
 the trigger which i have written on Account fires and associates that contact with  contact_associated__C, 
 but i want that the contact which is coming when lead is converted should be associated with contact_associated__C. 
 but this is not happening . Please find the code below.
 
 trigger updateFundraiser on Account (before insert, before update, after insert) {
    List<contact> newConls = new List<contact>();
    if(Trigger.isUpdate && Trigger.isBefore ) {
        set<id> conIds = new set<id>(); 
        for(account a1: trigger.new) {
            if(a1.Contact_Associated__c != null) {
                conIds.add(a1.Contact_Associated__c);
            }
        }
        system.debug('conIds::'+conIds);
        map<id,contact> conMap = new map<id,contact>([select id, Name, Beneficiary_Name__c,Email, MobilePhone from contact where id IN :conIds]);
        system.debug('conMap::'+conMap);
        for(account a1:trigger.new){
            system.debug('a1.Contact_Associated__c::'+a1.Contact_Associated__c);
            system.debug('!!!!!!'+trigger.oldMap.get(a1.id).Contact_Associated__c);
            //if(a1.Contact_Associated__c != trigger.oldMap.get(a1.id).Contact_Associated__c) {
                system.debug('herere!!!');
                if(a1.Contact_Associated__c != null){
                    a1.Mobile_Number__c = conMap.get(a1.Contact_Associated__c).MobilePhone;
                    a1.Created_By_Email__c = conMap.get(a1.Contact_Associated__c).Email;   
                    a1.Name = conMap.get(a1.Contact_Associated__c).Name;   
    
                    system.debug('herere!!!');
                    system.debug('a1'+a1);  
                } 
            //}
        }
    } 
    // second part 
    // for after insert
    system.debug('here!!!');
    if(Trigger.isAfter && Trigger.isInsert) {
        system.debug('here!!!'+trigger.new);
        for(account a1:trigger.new){
            if(a1.Contact_Associated__c == null || a1.contacts.size() < 0) {
                if(a1.Lead_Converted__c == false) {
                    contact c1 = new contact();
                    c1.Beneficiary_Name__c = a1.Name;
                    c1.LastName = a1.Name;   
                    c1.Email = a1.Created_By_Email__c;
                    c1.MobilePhone = a1.Mobile_Number__c;
                    c1.AccountId = a1.id;
                    newConls.add(c1);
                }
               
            }
        }
        if(newConls.size()>0) {
            insert newConls;
        }
    }
    // for after update
    // updating account associated Ids 
    set<id> conIds = new set<id>();
    map<id,id> accIdsVsConIds = new map<id, id>();
    for(contact c1: newConls) {
        accIdsVsConIds.put(c1.AccountId, c1.id);
    }
    List<account> accToUpdate = [select id, Contact_Associated__c from account where id IN : accIdsVsConIds.keyset()]; 
    for(account a1: accToUpdate) {
        a1.Contact_Associated__c = accIdsVsConIds.get(a1.id);   
    }
    system.debug('accToUpdate::'+accToUpdate);
    update accToUpdate;
}