9

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?

3 Answers 3

6

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.

4

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.

2
  • I think this is as close as you're going to get to directly performing DML from a VF template. Sep 1, 2012 at 20:02
  • Thanks @ca_peterson :) grigriforce - can you let me know if this solution works for you?
    – Sathya
    Sep 2, 2012 at 8:45
0

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

1
  • This won't work as the javascript isn't run server-side. The commandButton would try and run in the recipient's email client, which would not have a valid session cookie and would fail (if it even made it that far). Aug 22, 2012 at 0:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .