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
JesterJester 

DML in Controller Constructor?

Hi,

When I try to perform DML (a simple update) in my controller constructor I get an error.  Why is this not allowed?  Is there any way to perform such an update? 

I am aware of the action parameter in the page tag.  That is not sufficient for me as I am working under an interesting constraint, which unfortunately means no javascript.  (Or at least the type that calls back to the controller).

Thanks!
Best Answer chosen by Admin (Salesforce Developers) 
mtbclimbermtbclimber
Maybe you aren't aware of what page.action does.  There is no javascript involved in that - it's 100% server side. When the page is called the controller is constructed and if an action is bound to page.action it is invoked before the page is constructed.

All Answers

mtbclimbermtbclimber
Maybe you aren't aware of what page.action does.  There is no javascript involved in that - it's 100% server side. When the page is called the controller is constructed and if an action is bound to page.action it is invoked before the page is constructed.

This was selected as the best answer
michaelforcemichaelforce

That makes sense... I ran into the same problem and assumed that 'action' wouldn't allow DML either on the record (or records if list controller) being loaded... but because action runs before the constructor it works.  Thanks!

 

 

Additional thought:  Between the 'action' and 'rendered' properties, pages can be extremely flexible and dynamic... good stuff.