+ Start a Discussion
Ben Wild 8Ben Wild 8 

Error: Id not specified in an update call

I have a custom object called Opportunity_Forecast__c that sits as a child object below Opportunity. I created a custom visualforce page for editing and creating new Opportunity_Forecast__c records. The reason for this was the navigation butotns on the standard edit page took me back to the wrong page. Editing is working as expected. I am however having issues with creating new forecasts, I get the error Error:
Id not specified in an update call
when saving. My code is as below;
VF Page:
<apex:page standardController="Opportunity_Forecast__c" extensions="createController">
<apex:form >
<apex:pageBlock title="Forecast" mode="Edit">
	  <apex:pageBlockButtons >
	  	<apex:commandButton action="{!saveAndReturn}" value="Save"/>
	  	<apex:commandButton action="{!cancelAndReturn}" value="Cancel"/>
	  </apex:pageBlockButtons>
	  <apex:pageMessages />
	  <apex:pageBlockSection title="Forecast Detail" columns="1">
      	<apex:inputField value="{!Opportunity_Forecast__c.Value__c}"/> 
      	<apex:inputField value="{!Opportunity_Forecast__c.Forecast_Category__c}"/> 
     	<apex:inputField value="{!Opportunity_Forecast__c.Forecast_Period__c}"/>
     	</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller Extension:
public with sharing class createController {
	Opportunity_Forecast__c oppf {get;set;}
	private ApexPages.StandardController stdController;
	
    public createController(ApexPages.StandardController stdController) {
    	this.stdController = stdController;
        Opportunity_Forecast__c oppf = (Opportunity_Forecast__c) stdController.getRecord();
        Map<String, String> m = ApexPages.currentPage().getParameters();
        oppf.Opportunity__c = (Id) m.get('oppId');
    }
	
	public PageReference cancelAndReturn()
    {
    	PageReference cancel = stdController.cancel();
        cancel.setRedirect(true);
 		return cancel;	
    }
	
	public PageReference saveAndReturn()
    {
        try{
        	insert oppf;
        	PageReference save = stdController.cancel();
        	stdController.save();
        	save.setRedirect(true);
        	return save;
        }
        catch(DMLException ex){
        	ApexPages.addMessages(ex);
        	return null;
        }
    }
    
}

Any ideas?
v varaprasadv varaprasad

Hi Ben,

Please let me know In try block, you are updating or inserting new record .
If you are updating record then use :

try{
insert oppf; // for inserting
update oppf // for updating the record you need record id.

And One more thing please put some debug logs for below code and check expected values or coming or not.

Map<String, String> m = ApexPages.currentPage().getParameters();
system.debug('mmmmm'+m);
oppf.Opportunity__c = (Id) m.get('oppId');

system.debug('oppf.Opportunity__c : '+oppf.Opportunity__c);


Hope it helps you.


Thanks
Varaprasad



 

srlawr uksrlawr uk
That is a curious issue.. I have examined (and recreated) your setup in a developer org, but can't recreate the issue just yet...

How are you launching your Visualforce page to create these new Opp Forecasts? I see you are grabbing a URL parameter "OppId" - from somewhere; how is that coming about? -- I don't nesessarily think it is connected to the error message, but it would be useful to know.

You are definitly making an Insert call at line 22... so all I can think is that inserting oppf is causing a chained event (such as a Trigger or Process Builder flow) that is then failing to complete an update of it's own - and the error message is bubbling all the way back up to your try/catch.

Are there any other automations on insert of Opportunity_Forecast you can examine? If OppId was coming in blank (which I might suspect) that could be causing an attempted trigger updating/upserting the associated Opportunity to fail with that message.


 
Ben Wild 8Ben Wild 8
Thanks, yes it is an insert, looking at it again I think I may be getting the Opportunity ID wrong. The visualforce page opens when you click on the new button on the Opportunity_Forecast__c button on the Opportunity page. I think my issue is I assumed it was pulling it through with the standardcontroller. 

I do have other automations running, these Opportunity_Forecast__c objects are also created automatically when the opportunity reaches a certain percentage. The edit/create screen I am working on is for whena  user wants to manually override what the system has created (I have a manual override checkbox).
Ben Wild 8Ben Wild 8
I've noticed something else odd, when I press the cancel button, it comes up asking me to add values to the boxes and wont let me leave the page until I do. 
srlawr uksrlawr uk
For the cancel issue - does it help if you add the  immediate="true" attribute to the button as so:
 
<apex:commandButton action="{!cancelAndReturn}" value="Cancel" immediate="true" />

this should bypass page level validation and fire your method
Ben Wild 8Ben Wild 8
Just igven that a try but it didn't seem to help, I have an edit version of this page and it uses the same cancel button and the code in the externsion for that is identical. It has the same issue, if I delete anything and then press cancel it asks me to enter a value, its as if the cancel button is trying to save.