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
RAJU_DEEPRAJU_DEEP 

System.LimitException while dml operation through custom component

Hello,

          Here I am creating a custom component to perform the dml operation but whenever I click on Save button is show the following error:

System.LimitException: demoPackageTest:DML currently not allowed

 

showHere is the Custom Component code:

 

<apex:component controller="Customer">
  <fieldset>
      <apex:panelgrid columns="2">
            <p>Account Name:</p>
                <apex:inputText value="{!aName}"/>
            <p>No of Employees:</p>
                <apex:inputText value="{!aEmp}"/>
            <p>Phone:</p>
                <apex:inputText value="{!aPhone}"/> 
     </apex:panelGrid>
     <apex:commandButton action="{!saveNew}" value="Save"/>
  </fieldset>
</apex:component>

 

 

Here is the Controller code:

 

public class Customer {
    public String aName {get; set;}
    public Integer aEmp {get; set;}
    public String aPhone {get; set;}
    public void saveNew(){
        Account aa = new Account(
            Name = aName,
            NumberOfEmployees = aEmp,
            Phone = aPhone
        );
        insert aa;
    }
}

 

 

Here is visualforce page code:

 

<apex:page controller="Customer">
    <c:test />
</apex:page>

 

Guide me where I am going wrong and is it possible to perform dml operation through custom component.

 

Thanks in advance

Raju.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

It is possible to perform DML via a custom component, but the default behaviour is to stop it.

 

To enable this, you specify the allowDML attribute as true in your controller declaration:

 

 

<apex:component controller="Customer" allowDML="true">

 

 

All Answers

_Prasu__Prasu_

Have you created Managedpackage of this code and deployed in the another instance? 

bob_buzzardbob_buzzard

It is possible to perform DML via a custom component, but the default behaviour is to stop it.

 

To enable this, you specify the allowDML attribute as true in your controller declaration:

 

 

<apex:component controller="Customer" allowDML="true">

 

 

This was selected as the best answer
RAJU_DEEPRAJU_DEEP

Hi,

Thanks for your suggestion it really works.

 

Regards

Raju.

KiranRangaKiranRanga

Hi All, 

I am new to salesforce force.com, even I am facing the same issue when inserting a record into the database. I am getting an error saying "System.LimitException - DML is currently not allowed . I have used the allowDML="true" in apex component, but still i am getting same error.  Could you please anyone help me in this , thank you so much in advanced.

 

Please find the below apex class code:

 

public with sharing class InsertResponse{
 
 Response__c resp = new Response__c();
 List<Response__c> respList = new List<Response__c>();
 //getUserInfoResult result = connection.getUserInfo();

    public Answers__c ans {get; set;}
    public Question__c ques{get;set;}
    public boolean selected {get; set;}
    public String ansId{get;set;}
    public String userid{get; set;}
    public String questionId {get; set;}
    public String chosenAnswer {get; set;}
    public String answerId {get; set;}
    public String questionName{get; set;}
    public String currentQuestion {get; set;}
   
 public InsertResponse(TestController controller) {
 

 getAccountDetails(controller);
 }
  
  public InsertResponse(Answers__c answ) {
        this.ans = answ;
        selected = false; 
         userid = UserInfo.getUserId();
    }
   
     public  Response__c getinsertResponse(){
   

     resp.Name='TestResponse1';
     resp.Question_Id__c=currentQuestion;
     resp.Answers_Id__c = chosenAnswer;
     resp.User_Id__c = UserInfo.getUserId();
      insert resp;   // here i am getting System.LimitException - DML is currently not allowed
  
     return null;
     
     }
     public void getAccountDetails(TestController controller){
     try{
     this.ques = (Question__c)controller.getAccounts();
     currentQuestion = ques.Id;
     }
     catch(DmlException ex){
         ApexPages.addMessages(ex);
      }
     }
  
      }