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
SoundarSoundar 

Contact Update based on Account Code

I am trying to update a contact based on accout Code not a account name (field of contact). this Account code is available in Account.
When i create a new contact i will give a account code , at that time account name need to update based on this account code only...

Here is the following Code, can you please give me a proper trigger for this process

********************************************************************************
trigger ContactMapToAccount on Contact (before insert , before update) {
     
    //Set Of Id In Contact Code & Id
    Set<Id> conId = New Set<Id>();
    Set<Id> conCode = New Set<Id>();
    for(Contact c : Trigger.New){
        conId.add(c.Id);
        
    }
    
    List<Contact> lstCont = [Select id, Account_Code__c, name from Contact Where ID IN :conId];
    Map<Id,Account> acMap = new Map<Id,Account>();
    for(Account a : lstCont){
        acMap.put(a.id, a);
        
    }
    
    for(Contact c : lstCont){
        
        c.Account_Code__c = acMap.get(a.Account_Code__c);
    }
}


**************************

Thnaks in advance
Best Answer chosen by Soundar
SoundarSoundar

Hi Vadivel(Merfantz),

Thanks for your quick response , yeah we can't get Id before insert . I make some changes in my coding and it's working as well.

 

​trigger ContactMapToAccount on Contact (after Insert ) {
      List<Contact> lsCon = New List<Contact>();
    System.debug('Starting ++');
    //Set Of Id In Contact Code & Id
    Set<Id> conId = New Set<Id>();
    Set<String> conCode = New Set<String>();
    
    for(Contact c : Trigger.New){
         if(c.Account_Code__c != Null){
        conId.add(c.Id);
        conCode.add(c.Account_Code__c);
         } 
    }
    if(conCode.size() > 0)  {    
     System.debug('ContactId : ' + conId);
    
    List<Contact> lstCont = [Select id, Account_Code__c, name from Contact Where ID IN:conId];
    System.debug('Contact : ' + lstCont);
    
    List<Account> lstAcc  =[Select id,Account_Code__c, name from Account Where Account_Code__c IN:conCode];
    System.debug('Account : ' + lstAcc);
    
    Map<String,Account> acMap = new Map<String,Account>();
    for(Account a : lstAcc){
        acMap.put(a.Account_Code__c, a);
    }
    
   /* Map<Id,Contact> conMap =  New Map<Id,Contact>();
    for(Contact c : lstCont){
        conMap.put(c.Id, c);
    }*/
   
    for(Contact c : lstCont){
        if(c.Account_Code__c != Null){
            Contact cont = New Contact(id = c.id);
        cont.AccountId = acMap.get(c.Account_Code__c).id;
        lsCon.add(cont);    
    } 
        
   }
    if(lsCon.size() > 0){
        
        update lsCon;
    }
  }  
}

Regards,
Soundar Raj
+91- 7418425418​

All Answers

ajay rawat 14ajay rawat 14
Hi Soundar,

 "for(Account a : lstCont){"
Use List of Account instead of List of contact.

Thanks
Ajay Rawat
 
Vadivel MuruganVadivel Murugan
Hi Soundar,

In before insert not able get id for insertion time. I think your total code approach is wrong. You can add list codes in list and query the account with list and Map the contact.
SoundarSoundar
Hi Ajay,

Hearty Thanks for your quick Reply

Now i have maken a few changes in the code . now i getting invalid Id Error while save new contact.

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger ContactMapToAccount caused an unexpected exception, contact your administrator: ContactMapToAccount: execution of BeforeInsert caused by: System.StringException: Invalid id: AP001: External entry point 



******************************************************************************************************************
trigger ContactMapToAccount on Contact (before insert , before update) {
     
    System.debug('Starting ++');
    //Set Of Id In Contact Code & Id
    Set<Id> conId = New Set<Id>();
    Set<Id> conCode = New Set<Id>();
    for(Contact c : Trigger.New){
        conId.add(c.Id);
        conCode.add(c.Account_Code__c);
        
    }
        
     System.debug('ContactId : ' + conId);
    
    List<Contact> lstCont = [Select id, Account_Code__c, name from Contact Where ID =:conId];
    System.debug('Contact : ' + lstCont);
    
    List<Account> lstAcc  =[Select id,Account_Code__c, name from Account Where ID =:conCode];
    System.debug('Account : ' + lstAcc);
    
    Map<Id,Account> acMap = new Map<Id,Account>();
    for(Account a : lstAcc){
        acMap.put(a.id, a);
    }
    
   /* Map<Id,Contact> conMap =  New Map<Id,Contact>();
    for(Contact c : lstCont){
        conMap.put(c.Id, c);
    }*/
    
    for(Contact c : lstCont){
        if(c.Account_Code__c != Null){
        c.Account = acMap.get(c.Account_Code__c);
    } 
   }
}

 

**********************************************************************************


May I Know Why This Error is Showing Right Now!!
SoundarSoundar

Hi Vadivel(Merfantz),

Thanks for your quick response , yeah we can't get Id before insert . I make some changes in my coding and it's working as well.

 

​trigger ContactMapToAccount on Contact (after Insert ) {
      List<Contact> lsCon = New List<Contact>();
    System.debug('Starting ++');
    //Set Of Id In Contact Code & Id
    Set<Id> conId = New Set<Id>();
    Set<String> conCode = New Set<String>();
    
    for(Contact c : Trigger.New){
         if(c.Account_Code__c != Null){
        conId.add(c.Id);
        conCode.add(c.Account_Code__c);
         } 
    }
    if(conCode.size() > 0)  {    
     System.debug('ContactId : ' + conId);
    
    List<Contact> lstCont = [Select id, Account_Code__c, name from Contact Where ID IN:conId];
    System.debug('Contact : ' + lstCont);
    
    List<Account> lstAcc  =[Select id,Account_Code__c, name from Account Where Account_Code__c IN:conCode];
    System.debug('Account : ' + lstAcc);
    
    Map<String,Account> acMap = new Map<String,Account>();
    for(Account a : lstAcc){
        acMap.put(a.Account_Code__c, a);
    }
    
   /* Map<Id,Contact> conMap =  New Map<Id,Contact>();
    for(Contact c : lstCont){
        conMap.put(c.Id, c);
    }*/
   
    for(Contact c : lstCont){
        if(c.Account_Code__c != Null){
            Contact cont = New Contact(id = c.id);
        cont.AccountId = acMap.get(c.Account_Code__c).id;
        lsCon.add(cont);    
    } 
        
   }
    if(lsCon.size() > 0){
        
        update lsCon;
    }
  }  
}

Regards,
Soundar Raj
+91- 7418425418​
This was selected as the best answer