0

I have a trigger which embeds the account's field value to the child contact field. I have a field named account code on account and when ever a new contact is created under this account, account code will be automatically displayed on saving the record. It used to work fine but we have made the field on contact to required. From then, we cannot able to view account code on contact object. Even after we removed it from required, we are not receiving the result. Can anyone let me know if there is any issue with the code below?

trigger ContatctoAccount on Contact (before insert, before update) {

list<Contact> Cont = new list<Contact>();
Map<String,Account> mapCodeWithAccount = new Map<String,Account>();
for(Account acc: [select id,Account_Code__c from Account where Account_Code__c != Null]){
    if(acc.Account_Code__c!= null){
        mapCodeWithAccount.put(acc.Account_Code__c, acc);
    }
}
for(Contact C : trigger.new){
    if(mapCodeWithAccount.containsKey(c.Account_Code__c)){
        c.AccountID = mapCodeWithAccount.get(c.Account_Code__c).id;
        Cont.add(c);
    }
}
  • Your code doesn't seem to populate Account_Code__c on contact, its rather populating AccountId based on Account_Code__c of Contact. So, this code will work only when you populate Account_Code__c on Contact. Do you want to to work other way round ? – Vijay Ganji Apr 25 '19 at 8:23
  • This is something that could be achieved by using a formula field instead of a trigger. Is there any particular reason why you aren't using a formula field on Contact to pull the Account_Code__c of the related Account? – Derek F Apr 25 '19 at 12:38
  • If it is a formula field, Users cannot edit that field. We need users to enter the field while creating a contact and if they miss to fill that field, based on the parent account, the value will be auto populated. If at all they miss to add the account, based on the field value, account will be auto assigned to the contact – S kanth Apr 25 '19 at 12:46
  • @Skanth, Updated the answer. That should work for you !! – Vijay Ganji Apr 26 '19 at 7:09
1

From your latest comment, I understand that you want to populate Account_Code__c on Contact with the Account_Code__c of Account based on AccountId and similarly, you want to update the AccountId field on Contact with Id of Account based on Account_Code__c.

If that is what your requirement is then this is how your trigger code should be

trigger ContatctoAccount on Contact (before insert, before update) {
    Map<Id,Account> mapIDWithAccount = new Map<Id,Account>();
    Map<string,Account> mapAccountCodeWithAccount = new Map<string,Account>();

    for (Account acc : [select id,Account_Code__c from Account where Account_Code__c != Null ){
        mapIDWithAccount.put(acc.Id,acc);
        mapAccountCodeWithAccount.put(acc.Account_Code__c,acc);
    }
    for(Contact c : trigger.new){
        if(c.Account_Code__c ==null && c.AccountId !=null){
            if(mapIDWithAccount.containsKey(c.AccountId)){
                c.Account_Code__c = mapIDWithAccount.get(c.AccountId).Account_Code__c;
            }
        }
        if(c.AccountId ==null && c.Account_Code__c!=null){
            if(mapAccountCodeWithAccount.containsKey(c.Account_Code__c)){
                c.AccountId = mapAccountCodeWithAccount.get(c.Account_Code__c).Id;
            }
        }
    }
}
| improve this answer | |
  • Thanks Vijay When I have tested this in QA, it worked in both methods. When I test in Prod, it is only working when contact is created from account. When contact is created individually, it is not assigning to related account based on Account code. Can you please suggest any changes? – S kanth Apr 26 '19 at 11:52
  • I have somthing to add here We have made this field as required from field page under account after this trigger was deployed in Prod. Does this change made the feature unstable and unresponsive? – S kanth Apr 26 '19 at 12:03
  • Nope that should not impact. I have made minor change in the code, can you check if that works ? – Vijay Ganji Apr 26 '19 at 12:38
  • Code Level, it is super perfect. When it comes to Prod, it is not working when contact is created individually. If there is any issue in Code, it shouldn't work in Sandbox right....This issue is only with Prod and they both seems to be in sync. Wondering what changes made this to break – S kanth Apr 26 '19 at 12:47
  • Does Contact's Account_Code__c still has any value or is it getting nulled out after you save the Contact. Also, check if there exists a Account with the same Account_Code__c (Check if there is any space in Account_Code__c's value) – Vijay Ganji Apr 26 '19 at 12:52

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.