We have some global classes that have been exposed to the customer so they can utilize some code. If an error occurs in this code, we write out a log record into a custom object. The problem comes when one of our customers decides to utilize this code in a controller or a context where DML is not allowed. If this happens SF generates a Limit Exception and crashes hard.

Salesforce gives you the ability to test for most limits to see how close you are using the limit class.

Limits Class

But there doesn't seem to be a way to tell if you can perform an insert and since Salesforce won't let you catch the exceptions, there seems to be no way around.

If I could do something like limit.getIsDMlAllowed(); I could avoid writing out the log messages and instead just return the errors to the user.

Does anyone know of a way to check if DML is allowed before performing DML?

By the way if anyone is looking for a work around for this issue. We decided to utilize the rest api and make an http Request to create the record. Just seems like an extreme workaround for the issue and I would rather only do the callout if we can't insert the record normally.

1 Answer 1


Basically, do not perform any DML in a constructor or getter. This should eliminate the possibility of these errors. If you need a DML that should execute when a page loads, write an action method instead. There is no way to determine of DML is not allowed, but if you simply avoid any DML in a constructor, you won't have to worry about the exception.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .