+ Start a Discussion
mikebr59mikebr59 

Can't get a simple DML operation to work in controller extension

I'm going to risk embarrassing myself here because I know this is something really basic. But I'm just not seeing it.

 

In the following code, when I click Save, I get a DML exception saying that I did not include required fields Account__c and Invoice_Date__c, even though I entered them on the form. I've even tried initializing them in the constructor. But when it gets to the Save method all the properties in the "inv" variable are null.

 

I've also tried this with the format shown in the VF reference guide: "private final Invoice__c inv", using invoice__c in the markup. I've also tried doing this with a public getter method that returns inv. Nothing works.

 

WHY?!

 

//*** Page Markup ***

<apex:page standardController="Invoice__c" extensions="brokenInvoiceController" >
  <apex:form >

    <apex:pageBlock >
      <apex:pageMessages />
      <apex:pageBlockButtons >
        <apex:commandButton action="{!save}" value="Save" immediate="true" />
        <apex:commandButton action="{!cancel}" value="Cancel"/>
      </apex:pageBlockButtons>
      <apex:pageBlockSection >
        <apex:inputField value="{!inv.Account__c}" label="Account"/>
        <apex:inputField value="{!inv.Invoice_Date__c}" label="Invoice Date"/>
        <apex:inputField value="{!inv.Memo__c}" label="Memo" />
      </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>
</apex:page>

 

//*** Controller Extension ***

public with sharing class brokenInvoiceController {

public Invoice__c inv {get;set;}

public brokenInvoiceController(ApexPages.StandardController controller) {
  inv = (Invoice__c)controller.getRecord();
}

 

public PageReference Save() {
  upsert inv;
  return null;
}

public PageReference Cancel() {
  return null;
}
}

Best Answer chosen by Admin (Salesforce Developers) 
Andrew WilkinsonAndrew Wilkinson

Take the immediate out of your Save commandbutton.

 

Is there more to your custom controller extension? If not, you only need to use the standardController and not an extension.

All Answers

Andrew WilkinsonAndrew Wilkinson

Take the immediate out of your Save commandbutton.

 

Is there more to your custom controller extension? If not, you only need to use the standardController and not an extension.

This was selected as the best answer
mikebr59mikebr59

See, I was right - I am embarrassed.

 

I pasted that code from another page and didn't even see the "immediate". One of those things where you get so frustrated you can't see the forest for the trees. Or trees for the forest. Whatever. And yes, there is more to the page, but I wanted to create a minimal example that would fail so I could post it here.

 

Thanks!