Find your content:

Search form

You are here

Can you perform DML operations from visualforce email templates?

 
Share

Visualforce email templates allow you to include visualforce components with allowDML="true" but there doesn't appear to be any way to actually use DML. From the docs:

You can't use data manipulation language (DML) operations in a “getxxx” method in a controller.

You can't use data manipulation language (DML) operations in a constructor method in a controller.

You can't use the @future annotation in a “getxxx” or “setxxx” method in a controller, or in the constructor for a controller.

That seems to cover all the ways (I assume batch apex is also not allowed). So is there any way to do this?


Attribution to: Greg Grinberg

Possible Suggestion/Solution #1

I don't think you can use DML in this way. I was able to get some code with DML in it to fire by adding an attribute to the component and executing DML in the setter. However, this results in the following error:

Error occurred trying to load the template for preview: common.apex.runtime.impl.ExecutionException: Attempt to de-reference a null object. Please try editing your markup to correct the problem.

so it looks like there's something about the context that doesn't allow the DML to execute correctly.


Attribution to: Bob Buzzard

Possible Suggestion/Solution #2

How about having a body tag inside component and onload of the body tag (or a div onload) click an apex:commandbutton through javascript.. and for that commandbutton specify the action and in that action method you can perform your dml


Attribution to: Sathya

Possible Suggestion/Solution #3

Here is another answer. Not sure if this will work for you.

  1. Create an email service

  2. Add your email address

  3. You can send an email using Sendsingleemailmessage to the email address that you have added in the email service from your component controller's constructor. For the email message body, do a json.serialize of your object and assign it to Plaintextbody or HTMLbody and send it.

  4. Write a class to processes the email using Messaging.inboundemailhandler interface

  5. Perform a DML operation inside that method after deserializing the string to your object

Let me know if this worked.


Attribution to: Sathya
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/723

My Block Status

My Block Content