You need to sign in to do that
Don't have an account?
first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
Please point out what I am doing wrong
<apex:page Controller="EmployeeDemo" >
<apex:form >
<h1>Congratulations</h1>
This is your new Page: ForDocument
<Apex:pageBlock >
<apex:inputField value="{!objEmployee.Employee_Name__c}"/>
<apex:pageBlockSection >
<apex:commandButton Action="{!Save}" Value="Save" />
</apex:pageBlockSection>
</Apex:pageBlock>
</apex:form>
</apex:page>
public with sharing class EmployeeDemo
{
public EmployeeDemo()
{
objEmployee= new Employee__c();
}
public Employee__c objEmployee {get;set;}
public PageReference Save()
{
try
{
Insert objEmployee;
}
catch(Exception ex)
{
System.debug('\n\nException ='+ex.getMessage()+'\n\n');
}
return null;
}
}
Regards
Ambivert
Try like this
public with sharing class EmployeeDemo
{
public EmployeeDemo()
{
objEmployee= new Employee__c();
}
public Employee__c objEmployee {get;set;}
public PageReference Save()
{
try
{
Insert objEmployee;
}
catch(Exception ex)
{
System.debug('\n\nException ='+ex.getMessage()+'\n\n');
}
PageReference pr = new PageReference('/apex/YourPageName');
pr.setRedirect(true);
return pr;
}
}
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
All Answers
For the first time you are saving, it saves properly.
For the next time it throws exception for every new save you should refresh the page.
Beacuse the controller is retaining the old values.
Thanks for reply Devender!!!
I guess after click on save page got referesghed Automatically ???? If not what is the solution for this ??????
Regards
Ambivert
Try like this
public with sharing class EmployeeDemo
{
public EmployeeDemo()
{
objEmployee= new Employee__c();
}
public Employee__c objEmployee {get;set;}
public PageReference Save()
{
try
{
Insert objEmployee;
}
catch(Exception ex)
{
System.debug('\n\nException ='+ex.getMessage()+'\n\n');
}
PageReference pr = new PageReference('/apex/YourPageName');
pr.setRedirect(true);
return pr;
}
}
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
PageReference pr = new PageReference('/apex/PageName');
pr.setRedirect(true);
return pr;
The pbm got solved but can you please tell me why you wrote this, I never wrote this code whenevr I save ???
Regards
Ambivert
PageReference pr = new PageReference('/apex/PageName');
pr.setRedirect(true);
return pr;
Below one is to navigate that particular page.
PageReference pr = new PageReference('/apex/PageName');
Below is to refresh the page after the record is saved.
pr.setRedirect(true);
So that when you save it again it will not be treated as update and it will not give error.
If the post helps you please throw KUDOS.
Thanks