function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mallikammallikam 

Record ID: cannot Specify ID in an Insert Call

I have the following function which is being called on page submit for my visual force page. I am getting the following error when the user hits submit

 

Record ID: cannot Specify ID in an Insert Call

 

And the code is below

public PageReference confirm() {

case ticket = new case();

if (this.request != null) {

request.Title__c = this.empTitle;request.Department__c =

this.empDept; request.Reports_to__c = this.repToEmail;

 

try {

insert request; } catch (system.DMLException e){

ApexPages.addMessages(e);

return null;

}

this.caseId1 = generateNewTicket(request.employee_name__c, request.Grant_Email_access_to__c,

request.Outlook_out_of_office_message__c, request.Blackberry_user__c );

if (this.caseId1 == NULL)

return null;

}

 

list<Case> a = [Select c.CaseNumber from Case c where c.Id = :this.caseId1 limit 1];

if(a.size() != 0) {ticket = a[0];}

this.confNumber = ticket.CaseNumber;

 

try{ sendEmail(this.confNumber);

sendEmail();

} catch (EmailException e){

ApexPages.addMessages(e);

return null;

}

PageReference p = Page.offbsubmit;

p.setRedirect(true);

return p;

}

 

It says problem with insert but I see the new request was created! But I did not get any emails, so may be it was

choking at that part but I dont understand why. Any help please?

Best Answer chosen by Admin (Salesforce Developers) 
ShamSham

The next likely reason that comes to my mind is that the users are likely hitting the save button twice.

Because 

 

this.caseId1 = generateNewTicket(request.employee_name__c, request.Grant_Email_access_to__c,

request.Outlook_out_of_office_message__c, request.Blackberry_user__c );

if (this.caseId1 == NULL)

return null;

}

 

here null is being returned, the user remains on the same page is hitting the save button again.

 

All Answers

ShamSham

Is this a standardController Extension, in that if the request id is passed via the Url, SF will automatically set the RequestId

 

Try putting a  System.debug for Request.Id before doing the insert.

mallikammallikam

Thing is I never got this error when I tested it from my production instance. The request Id is being passed from

sites to visualforce pages to controller extension where I have this code. So I deployed the code on production and only clients are seeing this error. I am unable to reproduce the error!

ShamSham

The next likely reason that comes to my mind is that the users are likely hitting the save button twice.

Because 

 

this.caseId1 = generateNewTicket(request.employee_name__c, request.Grant_Email_access_to__c,

request.Outlook_out_of_office_message__c, request.Blackberry_user__c );

if (this.caseId1 == NULL)

return null;

}

 

here null is being returned, the user remains on the same page is hitting the save button again.

 

This was selected as the best answer