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
Syed F RazaSyed F Raza 

Error In Visual Force

Hi All,

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!!

 
Best Answer chosen by Syed F Raza
{tushar-sharma}{tushar-sharma}
After insert just clear the account variable, just like you have cleared the string variable.
insert acc;
acc = new Account()
aname=Null;
aphone=Null;
If this answer helps you, please mark it as accepted.

Regards,
Tushar Sharma
https://newstechnologystuff.com/
 

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Syed,

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
{tushar-sharma}{tushar-sharma}
After insert just clear the account variable, just like you have cleared the string variable.
insert acc;
acc = new Account()
aname=Null;
aphone=Null;
If this answer helps you, please mark it as accepted.

Regards,
Tushar Sharma
https://newstechnologystuff.com/
 
This was selected as the best answer
Syed F RazaSyed F Raza
Hi Tushar,

Thanks!! Yes it did help.