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
Afzaal HassanAfzaal Hassan 

populate field of an object by value from another object

Hello 
I have a field called "Contract Email 2" in the Account object. I want this field to be filled with the email that is on the field "Contract Email" in the Payee object. It shouls also update if that Payee object email changes. I wrote this trigger but my Contract Email 2 is always blank and not filling with the email that is on the Payee object. Can someone please help with this? My trigger is below:
So I am taking this step by step. For now, if someone changes or adds the contract email in Payee__c, I would like that email address/value to populate the field "Contract_Email_2" in the Account object. I wrote the following trigger, but the field in the account object is not working. Any ideas why?
trigger PayeeUpdateOnAccount on Payee__c (after update) {
    List<Payee_Client_Agreements__c> pcaList = [SELECT ID, Payee_Type__c, Payee__r.Contract_Email__c, Client__c from Payee_Client_Agreements__c WHERE Payee_ID__c IN :Trigger.newMap.keyset()];
    List<Account> accountsToupdate = new List <Account>();
    
    Map<Id, Payee_Client_Agreements__c> clientToPcaMap = new Map<Id, Payee_Client_Agreements__c>();
    for(Payee_Client_Agreements__c pca : pcaList){
        if(pca.Payee_Type__c == 'Broker'){       
        clientToPcaMap.put(pca.Client__c, pca);
       }
     }
    List<Account> acctList = [SELECT ID, Contract_Email_2__c from Account Where ID IN :clientToPcaMap.keySet()];
    for( Account acct : acctList ){
        if(acct.Contract_Email_2__c != clientToPcaMap.get(acct.Id).Payee__r.Contract_Email__c){
            acct.Contract_Email_2__c = clientToPcaMap.get(acct.Id).Payee__r.Contract_Email__c;
            accountsToupdate.add(acct);
        }         
    }
    if (accountsToupdate.isEmpty()){
        update accountsToupdate;
      }
    
    /**for (Payee__c py : Trigger.new) {
        String payeeID = py.Payee_ID__c;
        //List<Payee_Client_Agreements__c> pcaList = [SELECT ID, Payee_Type__c, Payee__r.Contract_Email__c from Payee_Client_Agreements__c where Payee_ID__c =: payeeID];
        if(!pcaList.isEmpty()){
            for (Payee_Client_Agreements__c pObj : pcaList) {
                update pObj;
            } 
            
        } 
    } **/
}
 
Raj VakatiRaj Vakati
Change if condition to below.  If it is not empty you need to update but you are checking another way
 
if (!accountsToupdate.isEmpty()){
        update accountsToupdate;
      }

 
Raj VakatiRaj Vakati
trigger PayeeUpdateOnAccount on Payee__c (after update) {
    List<Payee_Client_Agreements__c> pcaList = [SELECT ID, Payee_Type__c, Payee__r.Contract_Email__c, Client__c from Payee_Client_Agreements__c
	WHERE Payee_ID__c IN :Trigger.newMap.keyset()];
    List<Account> accountsToupdate = new List <Account>();
    
    Map<Id, Payee_Client_Agreements__c> clientToPcaMap = new Map<Id, Payee_Client_Agreements__c>();
    for(Payee_Client_Agreements__c pca : pcaList){
        if(pca.Payee_Type__c == 'Broker'){       
        clientToPcaMap.put(pca.Client__c, pca);
       }
     }
    List<Account> acctList = [SELECT ID, Contract_Email_2__c from Account Where ID IN :clientToPcaMap.keySet()];
    for( Account acct : acctList ){
        if(acct.Contract_Email_2__c != clientToPcaMap.get(acct.Id).Payee__r.Contract_Email__c){
            acct.Contract_Email_2__c = clientToPcaMap.get(acct.Id).Payee__r.Contract_Email__c;
            accountsToupdate.add(acct);
        }         
    }
    if (!accountsToupdate.isEmpty()){
        update accountsToupdate;
      }
    
    /**for (Payee__c py : Trigger.new) {
        String payeeID = py.Payee_ID__c;
        //List<Payee_Client_Agreements__c> pcaList = [SELECT ID, Payee_Type__c, Payee__r.Contract_Email__c from Payee_Client_Agreements__c where Payee_ID__c =: payeeID];
        if(!pcaList.isEmpty()){
            for (Payee_Client_Agreements__c pObj : pcaList) {
                update pObj;
            } 
            
        } 
    } **/
}

 
Afzaal HassanAfzaal Hassan
@Raj V. 
I am putting some system.debug statments and its not even entering my trigger when i make updates/put an email in Payee field, so therefore i feel like the account is not going to update
 
Raj VakatiRaj Vakati
Your trigger will fire only if you update data on Payee__c  object . not when you update the Payee__c  filed on account 
Afzaal HassanAfzaal Hassan
Raj V
That is what I want, as soon as I put the value of email in Payee, it should start a trigger that takes the value and updates the account's field. Its not doing that. nothing is happening. nothing is firing that trigger
Raj VakatiRaj Vakati
Is your trigger is active??
Afzaal HassanAfzaal Hassan
@Raj V 
The trigger is active