DML Operation in getter or setter method in Salesforce

DML Operation in getter or setter method in Salesforce

You cannot use DML (Data Manipulation Language) operations in getter or setter methods in Salesforce.

The getter and setter are invoked either by your code, or if you are writing visualforce controllers, the getters are executed when the page is rendered and the setters are executed when a form is posted back to the controller. We cannot consider getter or setter method for doing DML operations in the code.

The “get” method is used to pass data from your Apex code to your Visualforce page. The “set” method is used to pass values from your Visualforce page to the controller.

Check the below link for more information

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_considerations.htm?search_text=dml

Scenarios that may cause “DML currently not allowed” are

1. Venturing a DML operation within a component controller where allowDML=false.

2. DML operation within a class constructor.

3. DML operation within a getter or setter.

Leave a Reply