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>