+ Start a Discussion
SeraphSeraph 

Q>How to fix “DML currently not allowed” when Accessing Visualforce Page

I edited my class and saved it successfully but when I accessed my page which uses the class, I get an error of
DML currently not allowed
An unexpected error has occurred. Your development organization has been notified.

controller
public class DefinitionController {

    public DefinitionController() {
            this.DefTable();
    }

  public void DefTable(){
    listplatforms = [select Name,Status__c from Platform__c];

      for(Platform__c idlistplatforms : [select Id from Platform__c]){
        List<Def__c> existplatforms = [select Platform__c from Def__c where Platform__c=:idlistplatforms.Id];

            if(existplatforms.size() > 0)
            {
                idlistplatforms.Status__c = 'Set';
                //update idlistplatforms;
                System.debug('Found'+ idlistplatforms);
            } 
            else 
            {
                idlistplatforms.Status__c = 'Not Set';
                //update idlistplatforms;
                System.debug('Not Found'+ idlistplatforms);
            }
            update idlistplatforms;
      }
  }
    
}
I think the problem is because of the update part. How do I overcome this error?

Thanks in advance !

 
Best Answer chosen by Seraph
Shrikant BagalShrikant Bagal
Please use following class:
 
public class DefinitionController {

    public DefinitionController() {
           
    }

  public void DefTable(){
    listplatforms = [select Name,Status__c from Platform__c];

      for(Platform__c idlistplatforms : [select Id from Platform__c]){
        List<Def__c> existplatforms = [select Platform__c from Def__c where Platform__c=:idlistplatforms.Id];

            if(existplatforms.size() > 0)
            {
                idlistplatforms.Status__c = 'Set';
                //update idlistplatforms;
                System.debug('Found'+ idlistplatforms);
            } 
            else 
            {
                idlistplatforms.Status__c = 'Not Set';
                //update idlistplatforms;
                System.debug('Not Found'+ idlistplatforms);
            }
            update idlistplatforms;
      }
  }
    
}

With following VF:
 
<apex:page Controller="DefinitionController " action="{!DefTable}"> 
<!-- your page Logic ->
​</apex:page>

If its helps, please mark as best answer so it will help to other who will serve same problem.
​Thanks! 

All Answers

Usman AslamUsman Aslam
You are getting this error because DML statements from Constructor are not allowed.

Alternatively;

 - Remove statement this.DefTable(); from Contrustor.
 - Change return type of DefTable() to be PageRefernce and return null from this method.
 - Add action attribute in VF page's <apex:page> tag. i.e <apex:page controller="DefinitionController" action="{!defTable}" > ... </apex:page>

Hope this will resolve your issue.

Thanks
Usman
KaranrajKaranraj
Serap - You can't perform DML operation in the constructor method. If you want to perform DML operation on load of visualforce page, call your controller method from the 'Action' attribute of <apex:page> tag.  See the below sample code

Visualforce Page
<apex:page Controller="DefinitionController " action="{!DefTable}"> 
<!-- your page Logic ->
​</apex:page>
Don't forgot to comment the code where you are callin the DefTable method in your controller. 

Thanks,
Karanraj (http://www.karanrajs.com)

 
Shrikant BagalShrikant Bagal
Please use following class:
 
public class DefinitionController {

    public DefinitionController() {
           
    }

  public void DefTable(){
    listplatforms = [select Name,Status__c from Platform__c];

      for(Platform__c idlistplatforms : [select Id from Platform__c]){
        List<Def__c> existplatforms = [select Platform__c from Def__c where Platform__c=:idlistplatforms.Id];

            if(existplatforms.size() > 0)
            {
                idlistplatforms.Status__c = 'Set';
                //update idlistplatforms;
                System.debug('Found'+ idlistplatforms);
            } 
            else 
            {
                idlistplatforms.Status__c = 'Not Set';
                //update idlistplatforms;
                System.debug('Not Found'+ idlistplatforms);
            }
            update idlistplatforms;
      }
  }
    
}

With following VF:
 
<apex:page Controller="DefinitionController " action="{!DefTable}"> 
<!-- your page Logic ->
​</apex:page>

If its helps, please mark as best answer so it will help to other who will serve same problem.
​Thanks! 
This was selected as the best answer
SeraphSeraph
Thx for replying @Usman Aslam..I applied all the changes you suggested but I still get the same error when trying to access the page
Usman AslamUsman Aslam
@Seraph, can you please share your latest code? Both class and page?

Thanks
SeraphSeraph

@Usman Aslam..my bad..I forgot this part Remove statement this.DefTable(); from Contrustor. It worked.
@Shrikant Bagal..thanks for replying. I didn't know it was that simple.

After spending some time on the code, I think its better to put it on an apex trigger instead.

How will I do this?

Shrikant BagalShrikant Bagal
please mark as best answer so it will help to other who will serve same problem.
​Thanks!