System.LimitException: DML currently not allowed

When i am adding header component to new visual force page then it is returning exception saying

Visualforce Error

System.LimitException: DML currently not allowed

Class.ExceptionManager.save: line 14, column 1
Class.ExceptionService.saveException: line 25, column 1
Class.BaseController.<init>: line 348, column 1
Class.SignUpController.<init>: line 79, column 1

How do i get a solution for this problem ?

<apex:page showHeader="false" controller="RequestTradeController" standardStylesheets="false" >
    <script>
        document.title = '{!$Label.lblCR} - New Trade Relationship';
    </script> 
    <c:CommonComponent />
    <apex:form >
        <h2>Request New Trade Relationship</h2>

        <table width="70%">
            <tr><td><apex:pageMessage severity="warning">Select how you would like to send your request.</apex:pageMessage>    </td></tr>
            <tr><td align="center" class="button_link bgcolor-tab"><apex:commandButton styleClass="button_style button_link" action="{!test}" value="Request New Relationship using the Seller's CashRelease ID" id="theButton1"/></td></tr>
            <tr><td align="center"><b>OR</b></td></tr>
            <tr><td align="center" class="button_link bgcolor-tab"><apex:commandButton styleClass="button_style button_link" action="{!redirectToNewTrade}" value="Request New Relationship by Sending an Email Invitation to the Seller" id="theButton2"/></td></tr>
        </table>
    </apex:form>

</apex:page>

In this is already allowing the DML. so why i am getting this error?

Answer

You need to set the allowDML attribute of your component to true for DML to be allowed by your component. Take a look at the documentation on apex:component.

For example:

Component

<apex:component controller="MyComponentController" allowDML="true">
  <apex:form>
      <apex:commandButton value="Execute DML" action="{!executeDML}"/>
  </apex:form>
</apex:component>

Component Controller

public with sharing class MyComponentController {

     public PageReference executeDML() {
         Account a = new Account(Name = 'Test');
         insert a;

         return null;
     }
}

Page

<apex:page >
    <c:myComponent />
</apex:page>

From the apex:component documentation on allowDML:

If this attribute is set to “true”, you can include DML within the
component. The default is “false”. Allowing DML can cause side-effects
that could be problematic for consumers using the component with
partial page updates. When allowing DML within a component, you should
include rerender attributes so the consumer can appropriately refresh
their page. In addition, you should detail, in the description of the
component, what data is manipulated by the DML so that consumers of
the component are aware of potential side-effects.

It is also possible that the error is being caused because there is DML in a constructor which is not allowed.

It appears that the Exception is originating in your SignUpController (Class.SignUpController.: line 79, column 1) which is not the page’s controller. I’d start with looking at that.

Attribution
Source : Link , Question Author : upgoingstar , Answer Author : Peter Knolle

Leave a Comment