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
mamatha devarajmamatha devaraj 

exception2Insert failed. First exception on row 0 with id a7o3K000000DwRrQAK; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]

How can i fix this exception


     public static string billSpecSoInsert(Customer_Billing_Spec_Template__c billSpecins, List<Customer_Billing_Spec_SOMap__c> SoMapList, List<Cust_Billing_Spec_Extra_Parameter__c> billingExtParamList){
     
        System.debug('In ECOB_CloneBillingSpecsAccessor class for billSpecSoInsert  billSpecins:'+billSpecins);
        System.debug('In ECOB_CloneBillingSpecsAccessor class for billSpecSoInsert SoMapList:'+SoMapList);
        
        
        System.debug('In ECOB_CloneBillingSpecsAccessor class for billSpecSoInsert SoMapList size:'+SoMapList.size());
        string status='success';
        try{
            
            insert billSpecins; 
            //NAD-4567(SF-8490): Order Extra Parameters Phase 2
            if(!billingExtParamList.isEmpty()){
                for(Cust_Billing_Spec_Extra_Parameter__c ep : billingExtParamList){
                    ep.Customer_Billing_Spec_Template__c = billSpecins.id;
                }
                upsert billingExtParamList;
            }
            
            if(!SoMapList.isEmpty()){
            
                for(Customer_Billing_Spec_SOMap__c billspecId : SoMapList){
                    
                    billspecId.Customer_Billing_Spec__c = billSpecins.id;
                }
            
                upsert SoMapList;                
            }
        }catch(DMLexception ex) {
            ECOB_LogUtility.createDMLExceptionLog(ex); 
            system.debug('exception1 '+ex.getCause());
            system.debug('exception2'+ex.getMessage());
            status = ex.getMessage();
        }
      return status;
    }
    
    Thanks in advance
SwethaSwetha (Salesforce Developers) 
HI Mamatha,
Based on https://salesforce.stackexchange.com/questions/87901/invalid-field-for-insert-update-cannot-specify-id-in-an-insert-call

It seems that you are trying to insert a record with provided ID, that is impossible because record is created already.

Probably you need to update it - use update statement instead of insert.
In case if you are not sure if record exists - use upsert (it works as update for record with given id and as insert for record with missing id in general).

Here you can read more about upsert: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_database.htm#apex_System_Database_upsert_2

Thanks