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
CokeCoke 

Trigger an email alert from a Button

I'm not sure that this would be created in Visualforce but what I am trying to do is have an email alert trigger when a button is clicked. Basically I have created a button called "Pricing Request" and by clicking on this button I would like an email alert to be sent out. Is this possible? Is visualforce the right place to construct this? Any help would be much appreciated.

 

Thanks,

Coke

Best Answer chosen by Admin (Salesforce Developers) 
JimRaeJimRae

Just create a Field Update workflow action on the new checkbox field you add to the opportunity, when executed, set to a selected value of False.

 

Your workflow will trigger whenever the checkbox is set, then execute two actions:

1) the Email Alert

2) the Field Update

 

If you need multiple workflows for the email alert, that is fine, they can all execute the same field update action, just use the existing one, instead of creating a new one.

All Answers

JimRaeJimRae

Depending on exactly what you are trying to accomplish, this may very well be the way to go.

You could create a VF page with a button to execute your email code.

You could also create a trigger to send your code when a certain criteria on an object is met.

For example, if your "Request Price Quote" process is part of an Opportunity, you could add a checkbox on the opportunity called "Request Price Quote" and then create a trigger that checks for before insert and before update on the opportunity for that checkbox value to be true.  You could then de-select the checkbox (reset it, in effect) and send the email for the quote out.  This would also give you access to the fields in the Opportunity as part of the body of the email message.

 

 

CokeCoke
Thanks Jim. I would prefer to be able to have a button generate the email alert but I am not the best with code so it may be too much for me to figure out the code on my own. Creating a check box on the Opportunity would probably work but I'd just need to figure out how I'd allow multiple requests on the same opportunity. I guess I'd just need to have the user de-select the box.
JimRaeJimRae

By using a before trigger, you could reset the checkbox yourself.  As soon as the email is sent, the checkbox is already deselected on the opportunity record.  If the user wants another request, they check it again.

 

Code for the trigger, and for the VF page would be very similar.

What object did you want to put the button on?  That is, where would the user see the button to select?

CokeCoke
If the field was able to re-set each time that would be great. The object I'd creating this for would be an Opportunity. Basically they would have to plug data into some required fields, check the box and then an alert would go out to the person who would generate the quote. Could you help me with the code or before trigger to de-select the check-box?
JimRaeJimRae

Here is a simple version of the trigger that should work OK. You will need to tweak the addresses and the message bodies.

 

 

trigger requestQuoteOnOpportuntity on Opportunity (before insert, before update) { //create a list to hold all of the mail messages and send in batch Messaging.SingleEmailMessage[] maillist = new Messaging.SingleEmailMessage[]{}; for(Opportunity o:Trigger.new){ //requestquote__c is the name of the new checkbox on the opportunity record if(o.requestquote__c){ o.requestquote__c=false; //this will reset the checkbox for future use // Create a new single email message object // that will send out a single email to the addresses in the To, CC & BCC list. Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); // Strings to hold the email addresses to which you are sending the email. String[] toAddresses = new String[] {'user@acme.com'}; String[] ccAddresses = new String[] {'smith@gmail.com'}; // Assign the addresses for the To and CC lists to the mail object. mail.setToAddresses(toAddresses); mail.setCcAddresses(ccAddresses); // Specify the address used when the recipients reply to the email. mail.setReplyTo('support@acme.com'); // Specify the name used as the display name. mail.setSenderDisplayName('Salesforce Support'); // Specify the subject line for your email address. mail.setSubject('New Quote Request for Opportunity: ' + o.Name); // Set to True if you want to BCC yourself on the email. mail.setBccSender(false); // Optionally append the salesforce.com email signature to the email. // The email address of the user executing the Apex Code will be used. mail.setUseSignature(false); // Specify the text content of the email. mail.setPlainTextBody('Your Text body here'); mail.setHtmlBody('Your HTML Body here'); maillist.add(mail); } } // Send the email you have created. Messaging.sendEmail(maillist); }

 

 

 

CokeCoke
Thank you Jim. Rather than writing all of the email content and addresses into the code would it be possible to just trigger an email alert by checking the box? But then I would obviously still want to figure out a way to have the box uncheck itself once the email/ request has been submitted.
JimRaeJimRae

Where would the email format come from, and what address would it get sent to?

 

Your other option here would be to do the same thing using workflow.

You could set up an on change workflow, that would have 2 actions, one would be to reset the checkbox, the other would be to send an email using a template.

 

Would that be easier for you?

CokeCoke

The email format would come from an Email template and I would have to set up multiple workflows to manage which requests should go to which Project Administrator.

 

I think that using workflows would be a lot easier given my limited knowledge with code. I have never set up two actions on a worklfow though. How would I go about formulating a command to reset the checkbox and have the email sent out?

JimRaeJimRae

Just create a Field Update workflow action on the new checkbox field you add to the opportunity, when executed, set to a selected value of False.

 

Your workflow will trigger whenever the checkbox is set, then execute two actions:

1) the Email Alert

2) the Field Update

 

If you need multiple workflows for the email alert, that is fine, they can all execute the same field update action, just use the existing one, instead of creating a new one.

This was selected as the best answer
CokeCoke

Perfect! I just tested it and it will do exactly what I want it to do. Eventually I'd like to figure out how I can tie the button to a VisualForce page but I just don't have the coding expertise right now.

 

Thank you very, very much for all your help Jim.

 

Coke