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
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! 
dinesh kumar 2257dinesh kumar 2257

Actually I have tried similar way, But I am getting issue as 

Error: Method is not visible: [cres_cc_HeaderController].InActiveLightFixtureCart()
Error: Error occurred while loading a Visualforce page.

 

Visual Force page

<apex:page id="CSTN_Global_Header" applyHtmlTag="false" docType="html-5.0" sidebar="false" showHeader="false"
           standardStylesheets="false" controller="cres_cc_HeaderController" action="{!InActiveLightFixtureCart}">

Controller: 

global with sharing class cres_cc_HeaderController {
    
    public cres_cc_HeaderController(){
     
    }
    
    public PageReference InActiveLightFixtureCart(){
        //System.debug('InActiveLightFixtureCartsByUserId - ccrz.cc_CallContext ' + ccrz.cc_CallContext.currUserId);
        System.debug('InActiveLightFixtureCartsByUserId - ccrz.cc_CallContext5');
        System.debug('InActiveLightFixtureCartsByUserId - ccrz.cc_CallContext5 ' + ccrz.cc_CallContext.currUserId);
        
        return null;
    }
}

Please guide 

sunil puchakatlasunil puchakatla

I am getting same type of error in omniscripts,

when I am opening omniscripts i am getting

vlocity_cmt:DML currently not allowed
An unexpected error has occurred. Your solution provider has been notified. (vlocity_cmt) 

this error