+ Start a Discussion
VollyVolly 

Update email field with the value from a pick list

I'm new to Apex and I'm having a bit of a time understanding it. I'm hoping for a bit of assistance. I have a huge amount of workflow rules (+180) to handle email alerts as well as field updates. This is real messy and I really like to have an Apex trigger handle most of the work.

 

What I'm having a hard time wraping my mind around is how to do field updates. What I'm looking to do for the moment is make a simple Apex trigger that simply reads the Pick list value from a pick list that contains email addresses and then sets that value in the email field. I then can use one workflow to read that email address field and send the email. This will elimate %80 of the workflows I'm having to use on this custom object.

 

Would someone be so kind in showing me a code example specifically on how to do this? I've been searching for hours in books, online, and so forth. I know I'm asking for someone to code this and normally I know I should make an attempt, but I'm a bit lost. Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
sforce2009sforce2009

Here is the trigger. You really don't need a trigger here. Make hidden_vendor_email as formula field which holds vendor email value. but as you are learning, this might be helpful

 

trigger myTrigger on yourcustomobject(before insert, before update)

{

    for(Integer i = 0; i < trigger.new.size(); i++)

   {

        trigger.new[i].hidden_vendor_email__c = trigger.new[i].Vendor_Email__c;

   }

}

All Answers

sforce2009sforce2009

I can help you definitely. Can you a bit more elobarative on what you need.

 

VollyVolly

OK,

there are 2 fields

- Picklist called "Vendor Email"

- Email field called "hidden_vendor_email"

 

The user selects the correct email address from "Vendor Email"

Clicks the "Save" button to save changes.

"hidden_vendor_email" is updated with the value selected from "Vendor Email"

sforce2009sforce2009

Here is the trigger. You really don't need a trigger here. Make hidden_vendor_email as formula field which holds vendor email value. but as you are learning, this might be helpful

 

trigger myTrigger on yourcustomobject(before insert, before update)

{

    for(Integer i = 0; i < trigger.new.size(); i++)

   {

        trigger.new[i].hidden_vendor_email__c = trigger.new[i].Vendor_Email__c;

   }

}

This was selected as the best answer
VollyVolly

I thought of using the formula field, but the problem is you can't reference a formula field in an email alert.

VollyVolly

Just tried your code. Works perfectly. Thank you very much!