Thursday 29 November 2012

Solution for System.Exception: "DML currently not allowed"

Hi, Guys you can't execute DML in the Constructor (or execute a method containing DML from a Constructor) in the Apex Class. Like that :--

Scenario 1 :

*******************************************


{!acc}

*******************************************

public class testClass{
    public Account acc{get;set;} 
    //Constructor
    public testClass(){
        acc = [SELECT id, Name FROM Account WHERE Name = 'test1' LIMIT 1];
        acc.Name = 'test2';
        update acc;//DML
    }
}
*******************************************


Scenario 2 :

*******************************************


{!acc}

*******************************************

public class testClass{
    public Account acc{get;set;} 
    //Constructor
    public testClass(){
        updateAcc();    
    }
    public void updateAcc(){
        acc = [SELECT id, Name FROM Account WHERE Name = 'test1' LIMIT 1];
        acc.Name = 'test2';
        update acc;//DML
    }
}
*******************************************


Solution for System.Exception: DML currently not allowed :

1. if this is the controller for a VF page, you can use the "action" parameter on the page.
2. if this is the controller for a Component , you can use the "allowDML" parameter on the Component.

No comments:

Post a Comment