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
m@x~W0W~m@x~W0W~ 

INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

<apex:page standardStylesheets="true" tabStyle="Customer_Details__c" sidebar="false" showHeader="true" Controller="merchantService">
    <apex:form >
        <apex:pageMessages />
        <apex:pageBlock >
            <apex:pageBlockSection columns="1">
                <apex:pageBlockSection title="Customer Details" Collapsible="False">
                    <apex:inputField value="{!cusDet.First_Name__c}" required="true"/>
                    <apex:inputField value="{!cusDet.Last_Name__c}"/>
                    <apex:inputField value="{!cusDet.Email_ID__c}"/>    
                    <apex:inputField value="{!cusDet.Contact_No__c}"/>   
                </apex:pageBlockSection>
                
                <apex:pageBlockSection title="Product Details" collapsible="false" columns="3">
                    <apex:inputField style="width : 225px" value="{!proDet.Name}" required="true"/>
                    <apex:inputField style="width : 300px" value="{!proDet.Unit_Price__c}"/>
                    <apex:inputField style="width : 375px" Value="{!proDet.Quantity__c}"/>
                </apex:pageBlockSection>
                <apex:pageBlockSection >
                    <apex:commandButton style="float:right;" value="Add Row"/>
                    <apex:commandButton style="float:left;" value="Delete Row"/>
                </apex:pageBlockSection>  
            </apex:pageBlockSection>  
        </apex:pageBlock>
           <center>
              <apex:commandButton value="Save" action="{!save}"/>
              <apex:commandButton value="Generate Receipt"/>
              <input class="btn" type="reset" value="Reset"/>
           </center>
    </apex:form>
</apex:page>

 

public class merchantService
{
         
   public Customer_Details__c cusDet{ get; set;}
   public Product_Detail__c proDet{get; set;} 
    
    public merchantService()
    {
    cusDet = new Customer_Details__c();
    proDet = new Product_Detail__c();
    }                        
    public PageReference save() 
    {
        //try {
             insert cusDet;
             proDet.Customer_Name__c = cusDet.First_Name__c;
             upsert proDet;
             ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.INFO,'Save Successful.');
             ApexPages.addMessage(myMsg);
        //    } 
       // catch(System.DMLException e) 
       //    {
       //     ApexPages.addMessages(e);
        //    return null;
         //   }
            // After Save, navigate to the default view page:
            //return (new ApexPages.StandardController(pinfo)).view();
            return null;
    }  
}Here ,
First_Name__c is not unique.
Email_ID__c is unique.
 
 
Whenever I'm changing just the email id (First name unchanged) it gives the below error. But if I change the name too then it works fine.
 
Please suggest .
 
23:43:37:131 EXCEPTION_THROWN [4]|System.DmlException: Insert failed. First exception on row 0 with id 0019000000H6g5WAAR; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]
digamber.prasaddigamber.prasad

Hi,

 

Is there any trigger written on Customer_Details__c? If yes, please share code of the trigger.

m@x~W0W~m@x~W0W~

There is no trigger written on Customer_Details__c.

digamber.prasaddigamber.prasad

Hi,

 

You said when you want to change firstname & email,but by looking at your page & controller, looks like there is no scope of updating a record through this page. Seems gap in requirement. Could you please elaborate bit more about your exact requirement?

Praveen KimarPraveen Kimar

I think when you change the first name it is creating a new Customer details record and when you change the email change the same insert operation is happening and the already creating record is trying to insert again with the record id. 

 

Thank you,

Praveen K.