+ Start a Discussion
Mohammadasif SiddiquiMohammadasif Siddiqui 

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

<apex:page controller="Hospital" >
<apex:form >
<apex:pageBlock >
  <apex:pageMessages id="showmsg"></apex:pageMessages>  
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
  Hospital Name : 
<apex:inputField value="{!hosp.Name}"/>  
</apex:pageBlockSectionItem>
    
 <apex:pageBlockSectionItem >
Account Name :  
 <apex:inputField value="{!hosp.Account__c}"/>
</apex:pageBlockSectionItem>  
</apex:pageBlockSection>   
  
    <apex:pageBlockButtons >
    
<apex:commandButton action="{!insertNewItem}" value="Save" rerender="showmsg"/>   

</apex:pageBlockButtons>
    
<apex:pageBlockTable value="{! insertedrecord }" var="ct" id="mainSection">
    <apex:column headerValue="Hospital Name">"{! ct.Name }"</apex:column>
    <apex:column headerValue="Account Name">"{! ct.Account__c }"</apex:column>
</apex:pageBlockTable>    

</apex:pageBlock>
</apex:form>
</apex:page>
 
public class Hospital{

    public Hospital__c hosp{get;set;}
    public Hospital__c insertedrecord{get;set;}
 public Hospital()
    {
        hosp = new Hospital__c();
    }
    
  public void insertNewItem() {
if(hosp.Name == NULL || hosp.Name == '' )
{
   ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Please enter'));   
}
 else
 {     
      insert hosp;
      id insertedrecordid = hosp.Id;
      insertedrecord = [Select Name, Account__c  from Hospital__c where Id = :insertedrecordid Limit 1];
     
 }   
  }
}



Insert failed. First exception on row 0 with id a0B2w000000hYugEAE; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Error is in expression '{!insertNewItem}' in component <apex:commandButton> in page task1hospital: Class.Hospital.insertNewItem: line 17, column 1
 

I am getting above error, Please help me

Thanks in Advance

Best Answer chosen by Mohammadasif Siddiqui
Ankush AgarwalAnkush Agarwal
public class Hospital{

    public Hospital__c hosp{get;set;}
    public Hospital__c insertedrecord{get;set;}
 public Hospital()
    {
        hosp = new Hospital__c();
    }
    
  public void insertNewItem() {
if(hosp.Name == NULL || hosp.Name == '' )
{
   ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Please enter'));   
}
 else
 {     
      insert hosp;
      id insertedrecordid = hosp.Id;
      insertedrecord = [Select Name, Account__c  from Hospital__c where Id = :insertedrecordid Limit 1];
        hosp = new Hospital__c();

     
 }   
  }
}
End of insertNewItem method as shown in above code
 

All Answers

Ankush AgarwalAnkush Agarwal
Hi Asif,

You need to initialize hosp valirable once record is inserted.

      insert hosp;
      id insertedrecordid = hosp.Id;
      insertedrecord = [Select Name, Account__c  from Hospital__c where Id = :insertedrecordid Limit 1];
     hosp = null;

You might be getting this error when storing 2nd or 3rd record. Since after inserting first record, Hosp.Id is having value of 1st record and when it will try to insert another record it will give an DML error.
 
Mohammadasif SiddiquiMohammadasif Siddiqui
how to to initialize hosp valirable once record is inserted?
Ankush AgarwalAnkush Agarwal
Please use below line of code:

hosp = new Hospital__c();

Note:
 Please mark it as best answer if it helps you to resolve the issue.
Mohammadasif SiddiquiMohammadasif Siddiqui
where to use this line of code?
 
Mohammadasif SiddiquiMohammadasif Siddiqui
I have already initialized in constructor line 5
Ankush AgarwalAnkush Agarwal
public class Hospital{

    public Hospital__c hosp{get;set;}
    public Hospital__c insertedrecord{get;set;}
 public Hospital()
    {
        hosp = new Hospital__c();
    }
    
  public void insertNewItem() {
if(hosp.Name == NULL || hosp.Name == '' )
{
   ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Please enter'));   
}
 else
 {     
      insert hosp;
      id insertedrecordid = hosp.Id;
      insertedrecord = [Select Name, Account__c  from Hospital__c where Id = :insertedrecordid Limit 1];
        hosp = new Hospital__c();

     
 }   
  }
}
End of insertNewItem method as shown in above code
 
This was selected as the best answer
Mohammadasif SiddiquiMohammadasif Siddiqui
Error is gone but my data is not reocrded in hospital object when i save
Ankush AgarwalAnkush Agarwal
You may need to change below query at line 19 :

Select Name, Account__c  from Hospital__c where Id = :insertedrecordid order by CreatedDate Desc Limit 1
Mohammadasif SiddiquiMohammadasif Siddiqui

not working

maybe i think i need to  set value in apex class variable from vf page, can you help me how should i do that?
 

sachinarorasfsachinarorasf
Hi Mohammadasif,

It looks like you are trying to insert a record with the ID field. This will cause an error because the record is already created. You could probably need to do an Update.
In case you are not sure about record exists or not, use UPSERT. It performs like Insert or Update. If there is an id field it will update the
record else it inserts a new record. Replace insert with upsert.

Or you can use the below line in your code to remove the error.

<apex:commandButton action="{!insertNewItem}" value="Save" rerender="showmsg" oncomplete="window.top.location.reload()" />  

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com
Mohammadasif SiddiquiMohammadasif Siddiqui

how to pass value to apex class from vf page

 

https://developer.salesforce.com/forums/ForumsMain#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Apex_Code_Development&criteria=OPENQUESTIONS&id=9062I000000XwGQQA0

 

Ankush AgarwalAnkush Agarwal
<apex:page controller="Hospital" >
<apex:form >
<apex:pageBlock >
  <apex:pageMessages id="showmsg"></apex:pageMessages>  
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
  Hospital Name : 
<apex:inputField value="{!hosp.Name}"/>  
</apex:pageBlockSectionItem>
    
 <apex:pageBlockSectionItem >
Account Name :  
 <apex:inputField value="{!hosp.Account__c}"/>
</apex:pageBlockSectionItem>  
</apex:pageBlockSection>   
  
    <apex:pageBlockButtons >
    
<apex:commandButton action="{!insertNewItem}" value="Save" rerender="showmsg, mainSection"/>   

</apex:pageBlockButtons>
    
<apex:pageBlockTable value="{! insertedrecord }" var="ct" id="mainSection">
    <apex:column headerValue="Hospital Name">"{! ct.Name }"</apex:column>
    <apex:column headerValue="Account Name">"{! ct.Account__c }"</apex:column>
</apex:pageBlockTable>    

</apex:pageBlock>
</apex:form>
</apex:page>

You need to change your VF page at line number 19. Rerender must call mainSection as well which will refresh the table
lambingan teleseryelambingan teleserye
thnx for awesome post sir, can you plz help me out with the Pinoy Lambingan (https://lambinganteleserye.su/)  and how to use it?  Pinoy Tv Replay and the most papular Pinoy Teleserye Replay .... Lambingan