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 

Put another object value in workflow

I have an Account object that includes another "Payee Agreement" object. In that Payee Agreement Object is a List of "Payee" objects. My issue is that I had created a workflow that tries to send an email to the email address listed in the "Contract Email" field in the Payee object. So if that Account has 3 Payee Agreeements listed, then I need to obtain the three different "Contract emails" listed in the Payee object and send the email to those three address. Obviously, my current workflow cant do that because workflows cant do cross objects. What I did was created another field in Account called "Contract Email 2". Then I tried to update this field with the list of email values in Payee through a trigger. I dont think I wrote the trigger correct. For one,  I think I have the list generated correctly, but I dont think I am physically updating this ACCOUNT field. Also, how do I properly create the "Contract Email 2" field because my understanding is that it will not fill it with a list of values, correct? My trigger is below (if its even needed). If I am doing this whole thing the wrong way, please let me know how I should restart this problem. If I need to explain this problem clearer, please let me know as well. Essentially, I am just hoping that a correct "list" shows up when I select the "recipient" section in my workflow.

trigger PayeeCntractEmail on Payee_Client_Agreements__c (before insert, after update, before delete, after delete) {
    List<Account> accountemailList = new List<Account>();
    String clientNumber = '';
    
    for (Payee_Client_Agreements__c pca : Trigger.New) {
        String payeeAgreementID = pca.Payee_ID__c;
        clientNumber = pca.Client_Number__c;
        Account aObj = [SELECT ID, Contract_Email__c from Account WHERE Client_Number__c =: clientNumber];
        List<Payee__c> contractEmailList = [SELECT Contract_Email__c, Payee_Type__c from Payee__c WHERE Payee_ID__c =: payeeAgreementID];
        
        for(Payee__c email: contractEmailList){
            if(email.Payee_Type__c == 'Broker'){
                aObj.Contract_Email__c = email.Contract_Email__c;
                accountemailList.add(aObj);
            }
        }
        update accountemailList;
        //update aObj.Contract_Email__c;
        
    }
    

}
Vladimir SaturaVladimir Satura
Hi Afzaal,
this is not doable using workflow rules. WF rules can send email only to email fields (type 'Email'). But email field can hold only one email address, not multiple.
Even if you populated your field with list of emails, you will not be able to create WF Email Alert for it.

You can use Account trigger with same run condition as your Account WF and use apex to get list of Payees and send emails to them

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_forcecom_email_outbound.htm
Afzaal HassanAfzaal Hassan
Hello @Vladimir Satura,
I have already created an elaborate workflow/approval process of past requirements. I dont want to undo all that work. How would I approach this(what would the trigger look like) if I was to put one email?
Vladimir SaturaVladimir Satura
If you want to reuse your workflow / process, you can just update it to instead of sending email, it will update one specific field on account (like Account_Send_Email_Checkbox__c) and you apex trigger on Account object will look for updates to this field and then will send emails to all the payees for updated Accounts.
Afzaal HassanAfzaal Hassan
Hi @Vladimir Satura,
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;
            } 
            
        } 
    } **/
}

My thought process is that if I were to be able to have the email populate from payee to the field in the account, then it will show up in my workflow and i can simply select it and my problem will be solved
Vladimir SaturaVladimir Satura
That is not correct. Try ignoring the fact that your 'Contract_Email_2' field is not populated and go and finish your workflow. What is a value in that field - if it is populated or not - that makes no difference in it being shown in workflow or not. When creating a workflow you are working just with the field definition, not individiual values in individual records

If 'Contract_Email_2' is type Text, you won't be able to select it in your WF
If 'Contract_Email_2' is type Email, you will be able to select it in WF, but you won't be able to populate the field with multiple emails. (you can try manually)
Afzaal HassanAfzaal Hassan
@Vladimir Satura,
I am not sure I understand your direction. What I did was first create an Email field called Contract_Email_2 on Account object just so that I could have a place to store the value(email) from Payee. The trigger is on Payee object so that when I update/change that email, it should then take the value and place it in the Account objects email field. This way, if there is an email in the account, then whatever workflow I run on that specific account object (in this case send an email to that specific person), I can have that correct email available (which was not available before because it was in Payee object). You said I was not correct. Can you please correct what I did wrong? How do I correct my trigger? 
Vladimir SaturaVladimir Satura
Hey Afzaal, 
you mentioned that one Account can have multiple Payees. That means you want to have multiple emails in the Contract_Email_2 field. Is that correct?
List of emails for all of the payees like: payee1@email.com; payee2@email.com; payee3@email.com.....

It is not possible to store value like that in Email field. Your Contract_Email_2 field can store only one email address, not multiple. 
Afzaal HassanAfzaal Hassan
Valdimir Satura,
I guess to simplify, I was told that I need to loop through all the multiple payees, collect the emails Payee1email, payee2email, but in each payee if that type is "Broker", then I just take that first email I find thats type broker (Payee_Type__c) and put in the field of account. So in that case, I will have only one email in the account field. You are right, I cant store multiple emails in field so this simplification should make this doable correct? In that case, what am I doing wrong with my code?
Vladimir SaturaVladimir Satura
In that case it is doable. Your field will hold only one email address for one payee, even tho there might be more of them.
Your code is in this case correc, there is just a small bug there: 
if (accountsToupdate.isEmpty()){
        update accountsToupdate;
      }
needs to be updated to 'if NOT empty':
if (!accountsToupdate.isEmpty()){
        update accountsToupdate;
      }
Afzaal HassanAfzaal Hassan
Still not working. I am so confused. Thanks for helping me @Vladimir