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
Brandon NelsonBrandon Nelson 

APEX Trigger To Update Field Value

APEX Noob here :)

So I found out that I can not use a field of TEXT format when using an EMAIL ALERT. What I did was created a field called "Email" and gave it an EMAIL format. I would like to update this field with the email address based on the name in another field.

Layout:
Case Object: I have a field named: Wealth Planner (This is a lookup field linked on the EMPLOYEES custom object)
Case Object: I have a field named: Email (I would like this field to populate the EMAIL address for Wealth Planner (above) on the EMPLOYEES object)

I just dont know enough APEX to do this yet. Any help????

I hope this make sense.....
Best Answer chosen by Brandon Nelson
GauravGargGauravGarg

Hi Brandon,

Please contact me on skype, we will fix the issue together. 

Thanks,

Gaurav
Skype: gaurav62990

All Answers

GauravGargGauravGarg
Hi Brandon,

Please create a formula field for "EMAIL" and add formula "wealth_planner__r.email_Address" in this. 

Hope this helps!!!

Thanks,
Gaurav
Skype: gaurav62990
Brandon NelsonBrandon Nelson
@GauravGarg I did that the first time and I got the email address over fine, but when I go to do an Email Alert, the option to email that field is not there. Upon reading why it was not there I found out you can t use a formula field in an Email Alert. Also reading I found if you use a EMAIL field the that WILL populate as an option on the Email Alert..... Any thoughts?
GauravGargGauravGarg

Hi Brandon,

Did you tried creating the formula fields as return type "text". Let me know if you still face this issue.

Thanks,

Gaurav

Brandon NelsonBrandon Nelson

@GauravGarg Yes. I created a formula field with a TEXT output type and for the formula I used  (Wealth_PLanner_New_Field__r.Email__c).

That got me the email address, but when I go to create the Email Alert that field does NOT show up so I can't email them.

GauravGargGauravGarg

Brandon,

in this case we can populate Email address in trigger code. Do you have any trigger written over this object?

Thanks,

Gaurav

ShawnHepkerShawnHepker
trigger updateWealthPlannerEmail on Case (Before Insert, Before update){
   list<string> wealthPlanners = new list<string>();
   for(case caseRecord : trigger.newMap.values()){
      if(caseRecord.Wealth_Planner_Field_Name__c != null){
         wealthPlanners.add(caseRecord.Wealth_Planner_Field_Name__c);
      }
   }

   Map<Id, WealthPlanner__c> wealthPlannerRecords = new Map<Id, WealthPlanner__c>(
        [select Id, Email__c from WealthPlanner__c where id in :wealthPlanners]);

   for(case caseRec : trigger.new){
      if(caseRec.Wealth_Planner_Field_Name__c != null && wealthPlannerRecords.get(caseRec.Wealth_Planner_Field_Name__c) != null){
         caseRec.Email_Field__c = wealthPlannerRecords.get(caseRec.Wealth_Planner_Field_Name__c).Email__c;
      }
   }
}

Something along those lines should do. you can just replace Wealth_Planner_Field_Name__c with the lookup field name, Email__c with the email field on wealth planner, and Email_Field__c with the name of the field on the case object, etc.
Brandon NelsonBrandon Nelson
@ShawnJepker I'm lost and I know I shouldnt be LOL.... I'm just not understanding what is going on in the code so I don't know what to put where.

Do you think you could walk me through the lines?

Story: On the CASE object I have a field API name: Wealth_PLanner_New_Field__c that is a LOOKUP field on the EMPLOYEES object.
Also on the CASE object I have a field API name: Wealth_Planner_Email__c that is a EMAIL field and not related to anything.
On the EMPLOYEES object I have 3 fields

1. Name
2. Email__c
3. Title__c

I would like for the trigger to reach out to the EMPLOYEES object and grab the EMAIL address based on the name in the Wealth_PLanner_New_Field__c on the CASES object.

Does that make sense? I know I should understand this because I write in VBA, C# but trying to learn APEX is proving harder than I thought....

Thanks,
Brandon Nelson
GauravGargGauravGarg

Hi Brandon,

Please contact me on skype, we will fix the issue together. 

Thanks,

Gaurav
Skype: gaurav62990

This was selected as the best answer
Brandon NelsonBrandon Nelson
Thanks very much to Gaurav! Helped me and showed me what was going on and now the issue is resolved! Very quick and would highly recommend you see him for issues! 
GauravGargGauravGarg
Brandon,

Thanks a lot...

Thanks
Gaurav