+ Start a Discussion
Mr SlingMr Sling 

"System.DmlException: Insert failed" when saving to custom object in Summer 09 release

We are getting the following error when saving a value in one of our custom objects (RMA), but only in our Sandbox which is running the new Summer 09 release. This error is not occurring in our production environment which is running Spring 09.

"System.DmlException: Insert failed. First exception on row 0 with id a0BR0000001XEhfMAG; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

Class.RMANumberController.saveRmaNumber: line 15, column 10 External entry point "

The code for both the "RMANumberController" Apex Class and the "AddRMANumber" Visualforce page haven't changed in 8 months, so we're unsure why this is happening now. We have played with the the code in our custom Case view (Visualforce page) but that only 'displays' the RMA value and doesn't modify it.

This issue did not occur as recently as 2-3 weeks ago, but I just noticed it today.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

The problem is as the error message says, you're calling insert with an sobject that already has an Id. possibly because you're calling insert twice with the same object ? i.e. your saveNumber function gets call multiple times per lifetime of the controller (remember the controller is not reinitialized from scratch for each http round trip, so after the first call to insert, the rmanumber object will have its id set, then something calls saveNumber again (perhaps in a subsequent request on the same page), and you get the error.

 

I believe the problem about the window navigation etc, is because you have the last lime commented out. 

 

If your still having issues then you should probably ask in the visualforce forum, as its more of a VF issue than apex itself. 

All Answers

gv007gv007

catch the exception it wo't apper.

 

 

try

{

insert a;

}

catch(Dmexception d)

 

{d.getMessage();

}

Mr SlingMr Sling

Thanks!  The good news is that I've added the catch to the code and the exception isn't appearing now, but the bad news is that the pop-up "Add RMA" Visualforce window now isn't going away.  Basically, clicking the 'Add RMA' button appears to do nothing, but if I add a new RMA value and manually closing the pop-up window and then refresh the product record, the new RMA value appears.  

 

It would be great to know why this behavior popped-up in our Sandbox with Summer 09, especially before Summer 09 goes live in our production environment.

 

Here's the whole code, with your suggestion shaded: 

 

 

public class RMANumberController{

final RMA_Number__c rmanumber = new RMA_Number__c();

final OurCompany__c product;

public String productId {set;get;}

public RMANumberController(ApexPages.StandardController controller) {

rmanumber.Product__c = ApexPages.currentPage().getParameters().get('id');

} public RMA_Number__c getRmanumber(){

return rmanumber;

} public void saveRmaNumber(){

System.debug('Value of : ' + rmanumber.RMANumber__c);

if(rmanumber.RMANumber__c !=null && rmanumber.RMANumber__c.trim().length()>0){

try

{

insert rmanumber;

}

catch(DmlException d)

{

d.getMessage();

}

}else{

ApexPages.Message msg = new ApexPages.Message(ApexPages.severity.ERROR,'Please Enter RMA Number'); ApexPages.addMessage(msg);

}

//return new Pagereference('/'+rmanumber.Id);

}

}

 

 

 

Message Edited by Mr Sling on 06-19-2009 02:58 PM
Message Edited by Mr Sling on 06-19-2009 02:58 PM
SuperfellSuperfell
catching an exception and ignoring it are terrible practice, and you haven't fixed anything, your object still isn't getting saved. Also all instances have already been upgraded to summer '09
Mr SlingMr Sling

SimonF wrote:
catching an exception and ignoring it are terrible practice, and you haven't fixed anything, your object still isn't getting saved. Also all instances have already been upgraded to summer '09

I agree, and thanks -- however, because this issue has cropped up unexpectedly in the sandbox, we are concerned it will appear in production before we understand the root of the issue, and we can't have our 150+ users cry foul.  Thankfully, it still hasn't appeared in production and the code above to ignore it was only tested in the Sandbox.  I was happy to hear a suggestion on how to have the exception not appear, but you're absolutely right -- nothing is fixed and this issue is still not 'solved'.  :smileyhappy:

 

I will glady take any feedback you may have on fixing this the right way!

 

Thanks again,

 

Mark

SuperfellSuperfell

The problem is as the error message says, you're calling insert with an sobject that already has an Id. possibly because you're calling insert twice with the same object ? i.e. your saveNumber function gets call multiple times per lifetime of the controller (remember the controller is not reinitialized from scratch for each http round trip, so after the first call to insert, the rmanumber object will have its id set, then something calls saveNumber again (perhaps in a subsequent request on the same page), and you get the error.

 

I believe the problem about the window navigation etc, is because you have the last lime commented out. 

 

If your still having issues then you should probably ask in the visualforce forum, as its more of a VF issue than apex itself. 

This was selected as the best answer
gv007gv007
I agree with simon commnets, if you are geting the same id filed second timeuse upsert it may slove yours problem.
Mr SlingMr Sling

Sorry for the late reply - I was traveling on business and finally able to update this thread.

 

I eventually had my case escalated to Tier 3 where they realized this occurred due to a combination of:

 

1) being in development mode

2) "calling the insert twice" as SimonF mentioned.  (the window wasn't going away, so I was hitting 'add' twice, but thinking that the first press wasn't 'connecting') :-)

 

From SF Support:

 
"...When you are in Development Mode, the page has to be iframed to use the dev mode.  Window.close does not work in iFrames.

To make it work only with dev mode, you can use window.parent.close().

You can also use window.top.close() which worked for our test both in dev mode and non-dev mode..."

 

 

Thanks for all the help!  Sorry for the false alarm...