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
Nagarjuna Reddy.PNagarjuna Reddy.P 

Override Standard Action of Account

Hi All,

I overriden standard new button of Account with lightning component.. since last 5 days it has been giving error message as "cannot specify id in an insert call"..
The problem is when a new record is created and try to create another record the previous record values remains (pre populated) in the input fields and not allowing to create another record.

Error Message : invalid_field_for_insert_update cannot specify id in an insert call


Here is my code
Apex class :
@AuraEnabled
     public static Account saveAccount(account acc){
              insert acc;
            return acc;
     }

Component : 

<aura:component implements="lightning:actionOverride" access="global" controller="apexHandler">
    <aura:attribute name="newAcc" type="Account" default="{'sobjectType': 'Account'}" />    
<aura:attribute name="accountType" type="String[]" default="Insurance,MSO,IPA,PCP,SNF,ALF,Hospital,Home Health,Other"/>
<lightning:input name="accountName" label="Account Name"         required="true" value="{!v.newAcc.Name}" class="slds-size--1-of-2 slds-p-horizontal_x-small"/>
<lightning:select aura:id="accType" name="accType" label="Account Type"  value="{!v.newAcc.Account_Type__c}" onchange=" {!c.conditionalDisplay}" class="slds-size--1-of-2 slds-p-horizontal_x-small" required="true" messageWhenValueMissing="Please Select a Value">
          <option value="">--None--</option>
           <aura:iteration items="{!v.accountType}" var="item">
                  <option value="{!item}">{!item}</option>
               </aura:iteration>
    </lightning:select>
       <lightning:button variant="neutral" label="Cancel" onclick="{!c.cancelDialog}" />
                <lightning:button variant="brand" label="Save" onclick="{!c.saveRecord}" />
</aura:component>

Controller : 
          
 saveRecord : function(component,event,helper){
var accRec = component.get("v.newAcc");
 var action = component.get("c.saveAccount");
         action.setParams({
                 "acc" : accRec
             });        
        action.setCallback(this,function(response){
             var state = response.getState();
             var resultsToast = $A.get("e.force:showToast");
             if(state == "SUCCESS"){
                resultsToast.setParams({
                    "title": "Saved",
                    "message": "The Account Record is saved.",
                    "type" : "success"
              });
              
                 component.set("v.newAcc",response.getReturnValue());
                 var recId = component.get("v.newAcc.Id");
                 console.log('Account Record Id== >> '+recId);
                 resultsToast.fire(); 
                 helper.navigateTo(component,recId);
                }else if(state == "ERROR"){
               console.log('Error: ' + JSON.stringify(response.getError()));
                 resultsToast.setParams({
                     "title": "Error",
                     "type" :"error",
                     "message": "Please Enter Account Name: " + JSON.stringify(response.getError())
                 });
                 resultsToast.fire();
                 console.log('Unknown problem, state: ' + response.getState() + ', error: ' + JSON.stringify(response.getError()));
            }
        });
         $A.enqueueAction(action);
       }
I'm not sure what's wrong with the code, Could you pls help me with this.
 
RituSharmaRituSharma
The account that you are trying to insert seems to be an existing account i.e. acc object instance has Id of the record. Instead of insert, update/upsert the account.
Nagarjuna Reddy.PNagarjuna Reddy.P
Hi Ritu,

Thanks for your Reply, also tried with upsert but it's giving same message. Why the form is pre populating previous record data in the input fields?
RituSharmaRituSharma
Replace below line:
<aura:attribute name="newAcc" type="Account" default="{'sobjectType': 'Account'}" />    
with:
<aura:attribute name="newAcc" type="Account"/>