• lambingan teleserye
  • NEWBIE
  • 0 Points
  • Member since 2020
  • https://lambinganteleserye.su/

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
<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