You need to sign in to do that
Don't have an account?
Mohammadasif 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
All Answers
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.
hosp = new Hospital__c();
Note: Please mark it as best answer if it helps you to resolve the issue.
Select Name, Account__c from Hospital__c where Id = :insertedrecordid order by CreatedDate Desc Limit 1
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?
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
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
You need to change your VF page at line number 19. Rerender must call mainSection as well which will refresh the table