[SalesForce] Failing to save an object from a constructor of a standard controller class

We have a use case in which we wish to modify/save fields in the Lead objects from the constructor of a standard controller class.

Changing the fields goes well, but trying to modify the lead using "Standard Controller->Save()" method, or using "Upsert" fails due to "System.LimitException: DML currently not allowed"

I also tried to use @Future or schedule a job to do that change, but unfortunately @Future is not allowed in constructor and scheduling a job also fails; I can't find any trace for the job and I don't get any exceptions.

Appreciate your advice

Best Answer

You can't perform DML operations in a constructor, you can however use the VF page action function. This will allow you to perform any logic or DML operations and it is invoked before the page is rendered.

You use the action method in the tage like this

<apex:page controller="myController" action="{!myMethod}">

and in the controller

public void myMethod{
    //your logic and code here
} 

You can learn more about the Visualforce page action method here

http://www.salesforce.com/us/developer/docs/pages/index_Left.htm