简体   繁体   English

问>访问visualforce页面时如何解决“当前不允许DML”

[英]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. 当前不允许DML发生意外错误。 Your development organization has been notified. 您的开发组织已收到通知。

here is my 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. 我认为问题是由于更新部分。 Can someone please edit my code so that I will overcome that error? 有人可以编辑我的代码,以便克服该错误吗?

Thanks in advance! 提前致谢!

In short, DML operations are not allowed in a class constructor because it can be a reason of a dangerous side effects. 简而言之,类构造函数中不允许DML操作,因为这可能是危险副作用的原因。

Good answer to your question you can find here 很好的答案,您可以在这里找到

sorry dude but just like pavel said you can't put DML in you constructor. 对不起,老兄,但就像帕维尔说的那样,您不能在构造函数中添加DML。

what you can do is to let the Visual force page to load and then call you apex with Remote Action to make DML queries. 您可以做的是加载Visual Force页面,然后使用Remote Action调用您的顶点进行DML查询。

Make sure that you do that in document.ready of jquery. 确保在jquery的document.ready中执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM