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
Steven HoughtalenSteven Houghtalen 

DML error message: "DML not currently allowed"

I am new to apex coding but thought I understood most of what I need to do.  I have created a vf page that displays some fields amd updates some fields.  When I execute it, I get the following error: "DML not currently allowed" due to the DML "update" statement.  This has me puzzeled as it works from the Anonymous Window.  I would appreciate any help on this.  Following is the class and VF Page. Thanks.

<apex:page controller="Test2">
    <apex:form >
    <apex:pageblock title="Greetings {!$User.FirstName}">
        <apex:pageBlockTable value="{!Clt}" var="c">
            <apex:column value="{!c.Name}"/>
            <apex:column value="{!c.Gender__c}"/>
            <apex:column value="{!c.Age__c}"/>
             <apex:column value="{!c.SDLM__c}"/>
 
        </apex:pageBlockTable>
    </apex:pageblock>
    </apex:form>                                           
</apex:page>


public with sharing class Test2 {
        Public Test2() {
        }
        Public List <Client__c> clt;
        Integer sdlm=1;
         
        Public List <Client__c> getclt (){
            clt = [select Client__c.Name,Client__c.Age__c,
                   Client__c.Gender__c,Client__c.SDLM__c
            From Client__c
                   Order by Client__c.Name DESC];   
     
            For(Client__c c: clt){
                c.SDLM__c = 1;
            }   
                     
            Update clt;
            return clt;
            }
 }


 
Best Answer chosen by Steven Houghtalen
debradebra
You could create a action method in your controller and then call the method when your VF page loads:

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

Here is a good post on this topic with pros/cons of options.
http://salesforce.stackexchange.com/questions/23923/call-apex-method-from-visual-force-page-not-in-constructor

All Answers

Amit Chaudhary 8Amit Chaudhary 8
This issue is cominge because as the same method your are constructing the List and doing DML same like "DML not allowed in Constructor".
Check below post for same
1) http://salesforce.stackexchange.com/questions/28833/why-is-dml-not-allowed-in-constructor
2) http://salesforce.stackexchange.com/questions/32138/system-limitexception-dml-currently-not-allowed/32140
3) https://th3silverlining.com/2009/11/23/dml-currently-not-allowed/


Situations that may cause ‘DML currently not allowed’
  • Venturing a DML operation within a component controller.
  • Set in motion a DML operation within a class constructor.
  • Undertaking a DML operation within a getter or setter.

Let us know if this will help you
 
Steven HoughtalenSteven Houghtalen
Thank you Amit.  I kind of suspected that from a few things I saw when I googled the error message.  What is an alternative way to accomplish the update?
debradebra
You could create a action method in your controller and then call the method when your VF page loads:

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

Here is a good post on this topic with pros/cons of options.
http://salesforce.stackexchange.com/questions/23923/call-apex-method-from-visual-force-page-not-in-constructor
This was selected as the best answer
Steven HoughtalenSteven Houghtalen
Perfect!  Thank you Debra.