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
vamsi lankisettyvamsi lankisetty 

There is an email field in Account and contact object. If the value of Email Field on account is updated, then it should reflect the same on contact.

Hi all,

There is an email field in Account and contact object.

If the value of Email Field on account is updated, then it should reflect the same on contact.

Can anyone help me, how to achieve this thorugh Apex

Thanks
L Vamsi
chanchal_:)chanchal_:)
Do you want to reflect the email address on all related records of that particular account?
vamsi lankisettyvamsi lankisetty
In Account object we are having Email__c field. and also in contact object we are having Email__c field.

My requirement is, if i enter some value in Email__c field in Account object. then it should reflect in Contact object of Email__c field too.
Khan AnasKhan Anas (Salesforce Developers) 
Hi Vamsi,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
trigger UpdateConEmailWithAcc on Account (after update) {

    Set<Id> accountIds = new Set<Id>();
    for (Account a : Trigger.new) {
        Account old = Trigger.oldMap.get(a.Id);
        if (a.Email__c != old.Email__c) {
            accountIds.add(a.Id);
        }
    }
    if (accountIds.size() > 0) {
        Contact[] con = [SELECT Id, AccountId FROM Contact WHERE AccountId in :accountIds];
        for (Contact c : con) {
            Account a = Trigger.newMap.get(c.AccountId);
            c.Email = a.Email__c;
        }
        UPDATE con;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
vamsi lankisettyvamsi lankisetty
Hi Khan Anas,

First of all, thanks for the support. As i am new to coding, As per the Coding Best practices, they mentioned that logic or code should not write in the triggers. 

I have seen your code completely written in trigger part.

I am having 3 questions

1) Cant we achieve this through Apex instead of writing code in trigger.

2) Here i tried in my org. Let me explain how it is working.
First we have to create record in Account object then again create a record in contact object and do account lookup. Then modify the record in Account object. Finally the trigger will get update in the contact record.

3) What i am expecting is, when we create a record in Account.. directly it should create in contact record also ( is it possible )
Example : i have inserted Name,email and created a record in Account. After creating the record in account. The same record has to get updated in contact as well.( Is it possible or my question is wrong)

Can you please clear my doubts.

Thanks in advance.
Khan AnasKhan Anas (Salesforce Developers) 
Hi Vamsi,

Yes, you can write the logic in a handler class. Also, you can create a Contact record when Account is created. Please try below code:

Handler:
public class Handler_CreateContact {
    
    public static void createContact(List<Account> accounts) {
         List<Contact> conList = new List<Contact>();
    
        for(Account acc : accounts) {
            Contact con = new Contact();
            con.AccountId=acc.id;
            con.LastName=acc.Name;
            con.Email=acc.Email__c;
            conList.add(con);
        }
        if(conList.size()>0){
            INSERT conlist;
        }
    }
}

Trigger:
trigger CreateContact on Account (after insert) {
    
    if(Trigger.isInsert) {
        Handler_CreateContact.createContact(Trigger.new);
    }
}

I hope it helps you!
vamsi lankisettyvamsi lankisetty
Hi Khan Anas,

I have made modifications and tried the code bleow like this

Handler Class : 

public class Account_CreateContact 
{
    
    public static void createContact(List<Account> accounts) 
    {
         List<Contact> conList = new List<Contact>();
    
        for(Account acc : accounts) 
        {
            Contact con = new Contact();
            con.AccountId=acc.id;
            con.LastName=acc.Name;
            con.Email_Contact__c=acc.Email_Account__c;
            conList.add(con);
        }
        if(conList.size()>0)
       {
            INSERT conlist;
        }
    }
}

Trigger :

trigger CreateContact on Account (after update) 
{
    if(Trigger.isupdate) 
    {
        Account_CreateContact.createContact(Trigger.new);
    }
}


But not luck.. Nothing is getting updated. or When i am creating Account record, Email_contact__c in Contact is not getting updated.

Can you please help me, where i have done mistake or where i have gone wrong.

Thanks in Advance.
 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Vamsi,

isUpdate returns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or the API. 
You are using 'after update', which will be fried when the record is going to update. So, when you are creating an Account record, this trigger will not fire. You need to use the insert trigger event.

More information: https://trailhead.salesforce.com/en/content/learn/modules/apex_triggers/apex_triggers_intro