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
Technossus DeveloperTechnossus Developer 

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

Best Answer chosen by Admin (Salesforce Developers) 
souvik9086souvik9086

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

Devender MDevender M
No problem in code.
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.
Technossus DeveloperTechnossus Developer

Thanks for reply 

 

I guess after click on save page got referesghed Automatically ???? If not what is the solution for this ??????

 

Regards

Ambivert

souvik9086souvik9086

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

This was selected as the best answer
Technossus DeveloperTechnossus Developer

 

 

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

souvik9086souvik9086

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