0

I'm a beginner developer and I'm building a custom NEW contract button. I'm having issues getting the contract to insert/upsert and I think it has something to do with having a standardsetcontroller in my code. The problem is without it (and recordsetvar), it doesn't allow me to select the visualforce page when creating the list view button.

Error Message

Upsert failed. First exception on row 0 with id 000000000000000; first error: INVALID_ID_FIELD, record id cannot be empty key: [] Error is in expression '{!saveForm}' in component apex:commandButton in page societycontractrequestform: Class.SocietyContractRequest.saveForm: line 65, column 1

Controller Extension

public with sharing class SocietyContractRequest{
        
    public Contract Contract {get;set;}    
          
    public SocietyContractRequest(ApexPages.StandardController controller){
        this.contract = (contract)controller.getRecord();
    }
    
    public SocietyContractRequest(ApexPages.StandardSetController controller){
        this.contract = (contract)controller.getRecord();
    }
    
    //Save page
    public PageReference saveForm(){
        system.debug('Saving Contract...');
        upsert Contract;
        PageReference pageRef = new PageReference('/' + contract.Id);
        pageRef.setRedirect(true);
        return pageRef;
       }
       
    //Cancel and return to the Contract List View
    public PageReference cancelPage(){
        return new ApexPages.Action('{!List}').invoke();
    }
}   

Visualforce Page

<apex:page standardcontroller="Contract" extensions="SocietyContractRequest" recordSetVar="sobjects">
    <Script>
        function setFocusOnLoad() {}
    </Script>    
      <apex:form >        
        <apex:commandButton action="{!saveForm}" value="Save Contract" />        
        <apex:commandButton action="{!cancelPage}" value="Cancel" immediate="true" />                
    </apex:form> 
</apex:page>
  • Welcome to SFSE Justin -- there's too much code here that is irrelevant to your issue - you'll get more help if you use edit and strip away all the bits that don;t get at the root of the issue; e.g. we don;t need to see all these fields on Contract – cropredy Jun 26 at 5:40
  • Thanks for the advice @cropredy I've stripped away what I thought I could. I originally thought the whole picture is best, but simplifying makes sense. – Justin Guay Jun 26 at 14:50
  • do we need two SocietyContractRequest constructor? how about if you remove public SocietyContractRequest(ApexPages.StandardSetController controller)? StandardSetController is for multiple record – Shinya Koizumi Jun 26 at 15:01
  • The problem is if I remove the StandardSetController, I can no longer have recordsetvar on my Visualforce page which is a requirement when creating a list button. All I want is a custom new list button to create a single contract record at a time (without having to override the standard new button) – Justin Guay Jun 26 at 16:16
0

What worked for me was creating the list button with the required recordsetvar and StandardSetController... then removing the recordsetvar and changing to using a StandardController after the list button is created. Strange workaround, but it worked.

| improve this answer | |

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.