7

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?

5

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.

2

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.

  • I think this is as close as you're going to get to directly performing DML from a VF template. – ca_peterson Sep 1 '12 at 20:02
  • Thanks @ca_peterson :) grigriforce - can you let me know if this solution works for you? – Sathya Sep 2 '12 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

  • 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). – ca_peterson Aug 22 '12 at 0:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.