+ Start a Discussion
Matt1000Matt1000 

Delete OpportunityLineItem results in error DML currently not allowed

The following code is supposed to delete all products from the current Opportunity:

 

delete [ Select Id from OpportunityLineItem WHERE OpportunityID = :ApexPages.currentPage().getParameters().get('id') ];

 

...but it results in this error:

 

"DML currently not allowed"

 

The email generated to me from Salesforce about the error says:

System.LimitException: DML currently not allowed

 

Note: I am using Developer Edition.

 

Can anyone explain why this code is failing?

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
Ankit AroraAnkit Arora

Hi Matt,

 

There are several reasons why you facing this error:

 

1) You are operating DML in constructor, which is not allowed.

2) DML operation within a component controller.

 

If you facing the problem due to first scenario then use:

 

 

<apex:page action="{!YourDMLOperatingMethod}"/>

 

YouDMLOpertaingMethod will delete the records, this method will be called after constructor is loaded. 

 

 

And if you are facing the the problem due to second scenario then try this :

 

 

<apex:component allowDml="true"/>

 

Hope this will help you out.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

 

All Answers

MandyKoolMandyKool

Hi,

 

Check out the exception. It shows that you have crossed your governor limits.

That is why you are getting this kind of error. Try to optimize your code so that you will cross the governor limits.

Also Using Delete [Query] is also not good practice.

 

Hope this will help you!! For any help let me know!!

Ankit AroraAnkit Arora

Hi Matt,

 

There are several reasons why you facing this error:

 

1) You are operating DML in constructor, which is not allowed.

2) DML operation within a component controller.

 

If you facing the problem due to first scenario then use:

 

 

<apex:page action="{!YourDMLOperatingMethod}"/>

 

YouDMLOpertaingMethod will delete the records, this method will be called after constructor is loaded. 

 

 

And if you are facing the the problem due to second scenario then try this :

 

 

<apex:component allowDml="true"/>

 

Hope this will help you out.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

 

This was selected as the best answer
Matt1000Matt1000

Thanks Ankit_Arora!

 

I was trying to execute this DML statement in the constructor, so your 1st suggestion solved my issue.

 

P.S. I had started with a safer form for the delete statement, escaping the id parameter, etc., but I pared the delete statement down to the simple form shown for discussion in this forum.

 

Thanks again!