You need to sign in to do that
Don't have an account?
Syed F Raza
Error In Visual Force
Hi All,
Please find below my apex and visualforce code:
On the Visualforce page, when I input the values for Name and Phone, the value gets inserted and I can see the new record in Accounts. Also the values in the form become null.
However once I enter a different set of values, I get the following error:
System.DmlException: Insert failed. First exception on row 0 with id 001f400001MIzTMAA1; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Error is in expression '{!getName}' in component <apex:commandButton> in page learnvf1: Class.Learn1.getName: line 13, column 1
Class.Learn1.getName: line 13, column 1
Please advise. Thanks!!
Please find below my apex and visualforce code:
public class Learn1 { public string aname{get;set;} public string arating{get;set;} public string aphone{set;get;} Account acc=new Account(); public void getName(){ acc.Name=aname; acc.Rating=arating; acc.Phone=aPhone; insert acc; aname=Null; aphone=Null; } }
<apex:page controller="Learn1"> <apex:form> <apex:pageblock> <apex:pageblocksection> <apex:outputLabel value="Name of Account" style="width:100px;"/> <apex:inputText value="{!aname}"/> </apex:pageblocksection> <apex:pageblocksection> <apex:outputLabel value="Phone" /> <apex:inputText value="{!aphone}"/> </apex:pageblocksection> <apex:pageblockbuttons> <apex:commandButton value="Submit" action="{!getName}"/> </apex:pageblockbuttons> </apex:pageblock> </apex:form> </apex:page>
On the Visualforce page, when I input the values for Name and Phone, the value gets inserted and I can see the new record in Accounts. Also the values in the form become null.
However once I enter a different set of values, I get the following error:
System.DmlException: Insert failed. First exception on row 0 with id 001f400001MIzTMAA1; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Error is in expression '{!getName}' in component <apex:commandButton> in page learnvf1: Class.Learn1.getName: line 13, column 1
Class.Learn1.getName: line 13, column 1
Please advise. Thanks!!
Regards,
Tushar Sharma
https://newstechnologystuff.com/
All Answers
I found the below link which shows the same example to insert record into account object can you please have a look at it once.
>> https://developer.salesforce.com/forums/?id=906F000000092Z0IAI
In case if this helps can you please mark this as best answer so that it can be used by others in the future.
Regards,
Anutej
Regards,
Tushar Sharma
https://newstechnologystuff.com/
Thanks!! Yes it did help.