Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

So I understand DML Operations aren't allowed in getters/setters,constructors but it was my understanding that they were allowed in methods that were invoked by page-actions like so:

page:

<apex:page controller="mycontroller" action="{!init}">...</apex:page>

controller:

public mycontroller(){
   public void init(){
        //perform some DML, in my case inserting a custom setting if one doesn't exist...
        insert whatever...;
   }
}

This worked fine for a while, but than when I attempted to deploy my app into a Spring '13 sandbox org I get an exception:

DML currently not allowed An unexpected error has occurred. Your development organization has been notified.

Has this always been the case and I'm doing something different I didn't realize?

If not some suggestions how I could create my necessary custom settings if they don't exist on page-init would be great.

share|improve this question
    
I can use DML in a page action in a Winter '13 DE without any problem, so if it isn't working it is new in Spring '13. Is it in a component? Then you need to specify the allowDML="true". –  Peter Knolle Jan 23 '13 at 23:39
    
It's not in a component, it's in a page that is loaded inside of a visualforce tab though... –  jordan.baucke Jan 23 '13 at 23:40
    
Works for me in a VF tab as well in a Winter '13 DE. Seems like it should "just work" if you haven't changed API versions of the page, class, or anything like that. –  Peter Knolle Jan 23 '13 at 23:50
    
This is weird. I just moved the location of the call and it's working for me as well! Ok -- going to leave this for the moment until I figure it out. –  jordan.baucke Jan 23 '13 at 23:58

1 Answer 1

I once started seeing this error and couldn't work out for the life of me why... I eventually realised I was being a bit dim, and following my oft-used practice had a call to Init() inside a constructor, and you can't perform DML operations like this inside the constructor!

Update Jan 2014 - Something else to watch out for!

Almost a year after writing this answer I hit a similar issue yesterday, this time because the controller was for a Visualforce component as opposed to a page. In a component you must explicitly allow DML like so:

<apex:component controller="myController" allowDML="true">
share|improve this answer
    
Nice caveat @LaceySnr! How do you get these pages and components through Security Review process with DML in the action? Did you need to justify an exceptional case to them? –  bigassforce Jan 7 at 23:04
    
This is just in an action fired from a button, not on load. I do have one on load though and yes, I just had to explain that the auto-test reported a false positive and that the DML would be in no way damaging to system data. –  LaceySnr Jan 8 at 2:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.