1

I am getting a DML not allowed error from my visual force page.

The error is caused by an update and insert call in a method which seems to be the constructor.

Can someone give me a solution to how I can update and insert this list of records without hitting this error?

Code snippet:

public with sharing class SalesInvoiceHelper
{
public static List<CTransaction_Item__c> generateSalesInvoiceLineItems(Id salesInvoiceId, List<OpportunityLineItem> prodList)
{
    List<CTransaction_Item__c> lineItems = new List<CTransaction_Item__c>();
    List<OpportunityLineItem> oneOffPayments = new List<OpportunityLineItem>();
    List<OpportunityLineItem> renewals = new List<OpportunityLineItem>();
    RenewalValues__c rv = RenewalValues__c.getOrgDefaults();

    for(OpportunityLineItem oli : prodList)
    {  
      //I removed the code in here from the snippet to keep my post from being too long, as it is quite long and not causing any problems.           
    }

    **//This line is causing DML not allowed.**
    if(oneOffPayments.size() > 0)  {update oneOffPayments; oneOffPayments.clear();} 

    **//This line is causing DML not allowed.**
    if(renewals.size() > 0){ insert renewals; renewals.clear();}

    return lineItems; //Returns all of the items
}
1

You can perform a DML operation in an action method. apex:page's action method will be called after constructors but before the page renders:

<apex:page controller="SalesInvoiceHelper" action="{!generateLineItems}" ...>

or

<apex:page controller="Sales_Invoice__c" extensions="SalesInvoiceHelper" action="{!generateLineItems}" ...>
  • Hi, thanks for the reply. But my controller is not the SalesInvoiceHelper. The class is called later on in the code, but when I remove the update and insert lines, the page no longer throws the DML not allowed error. – Alexander Atkinsoon Feb 5 at 15:50
  • 1
    @AlexanderAtkinsoon Whatever class is the controller would need to have the method to be called. The point of my answer is that you simply need to call an action method. – sfdcfox Feb 5 at 15:51
  • The documentation discourages performing DML in the method specified by the action parameter of <apex:page>, but...eh. I just take that as a friendly suggestion. I suppose the behavior might not be well defined if, for example, you end up running into an uncatchable exception in executing the action method. Any input from you sfdcfox? – Derek F Feb 5 at 16:05
  • 2
    @DerekF The recommendation is based on the fact that it opens the possibility of CSRF-style attacks. Imagine someone embeds an image or iframe in an email with a URL to this page, and the user is already authenticated, it could result in unintended actions performed by the user. There's a very good reason why it should be avoided if possible. As far as undefined behavior, the action method on apex:page is the same as any other, uncaught exceptions result in the B+W VF error page. – sfdcfox Feb 5 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.