1

I have a page that is doing a lot of heavy-lifting server-side and then showing up as a page-within-a-page on a custom object layout.

When something goes wrong (usually finding no available products) I let the user know. I'd like to also log these in Chatter so users don't have to tell us that something's not available--we can just follow an "exception Chatterer"

I'm running into the DML not allowed because all that calc runs in the constructor (I want it to happen when the page loads). Posting to chatter is considered DML.

I tried making it @future, but that's not allowed in constructors, either. Any suggestions on this? We liked the idea of a chatter exception log, but it's really only helpful if all the classes can use it.

2

Well DMLs are not supported from a constructor. What you can do is call a method of controller from the action attribute of the <apex:page>

For Example below code calls the MyMethod() from the controller on page load.

<apex:page action="{!MyMethod}">
| improve this answer | |
  • +1, this is the right answer. Due to that silly constructor limitation, I've gotten in the habit of performing all logic in page actions rather than constructors for my controller code - it helps reduce developer error over time. – jkraybill Mar 7 '13 at 2:27
  • One note of caution on this approach - I have used it a few times in managed packages, and it sometimes gets red flagged by the security scanner...so I would do a check for that until you go too far down that path... – BritishBoyinDC Mar 7 '13 at 21:03

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.