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

DML Currently not allowed

Hey all,


I'm trying to make a component for adding inline notes to any record.


But each time I call the {!addNote} action, I get "System.Exception: DML currently not allowed"


I don't know why that is, some explanation would be hot.



Here is my controller:



public class GenericInlineNoteComponentController { // External variables public List<Note> n {get; set;} public String myObjectId {get; set;} public GenericInlineNoteComponentController() { Id thisId = myObjectId; n = new List<Note>(); Note newNote = new Note(); newNote.ParentId = thisId; //Add the new line right off the bat n.add(newNote); } //Save public PageReference addNote() { insert n; return null; } }


Everything displays correctly till addNote is called.








Hello JNic;

I dont think that the problem is with your "addNote()" method.The problem is may be with the place where you call the "addNote()" method. In some places it is not allowed to do DML operations. For an example in your constructor you cant do a DML operation(So you can't call the "addNote()" method in constructor). Could you please tell us how/where do you call the "addNote()" method.  


If you want to use dml statements in a component you have to use the allowDML attribute in the visualforce component tag:


<apex:component allowDML="true"> ...


I have Visualforce as a Quick Action on Case and got the same error. I have try catch block in constructor and creating an Error log if there is any error. There is an exception in the code and that caused to create Error Log in constructor. Since VF components won't allow DML either you have to add allowDML="true" or fix the errors before reaching any DML statements. I commented try catch block, tested and fixed errors and then added try catch block.

Is this Visualforce Component? If yes you can add allowDML="true"
Refer this: