+ Start a Discussion
Shruti Sharan GShruti Sharan G 

INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call:

Please help in resolving errors like :

INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call:
Best Answer chosen by Admin (Salesforce Developers) 
falfadlifalfadli

Hi discovered the problem.....I was not passing in a list properly to the insert call (creating a list object and passing the list to the insert call).

 

Code below works.....

 

    for (PG_Metric__c[] pgMetricArray: [Select p.Total_Fees__c, p.Standard_Non_Standard__c, p.Rationale_for_Request__c, p.Probability_of_Payout__c, p.Penalty_Type__c,
                                            p.PG_Template__c, p.Opportunity__c, p.Non_Standard_Reason__c, p.Non_Standard_Details__c, p.Metric_Status__c, p.HEDIS_Sites__c,
                                            p.HEDIS_Site_Quantity__c, p.Current_YTD_PG_Result__c, p.Current_PG_Metric_Level__c, p.Business_Owner__c, p.At_Risk__c,
                                            p.At_Risk_Percent__c, p.At_Risk_Base__c, p.At_Risk_Base_Type__c, p.Assessment_Period__c From PG_Metric__c p
                                            Where p.Metric_Status__c != 'Withdrawn' and p.PG_Case__c = :recordId Limit 1000 ]) {
                                           
                                            List <PG_Metric__c> metricList = new List<PG_Metric__c>();
                                             
                                            for (PG_Metric__c aPgMetric : pgMetricArray) {       
                                                //Create New PG_Metric Record
                                                metricList.add(new PG_Metric__c( Total_Fees__c=aPgMetric.Total_Fees__c, Standard_Non_Standard__c=aPgMetric.Standard_Non_Standard__c, Rationale_for_Request__c=aPgMetric.Rationale_for_Request__c,
                                                Probability_of_Payout__c=aPgMetric.Probability_of_Payout__c, Penalty_Type__c=aPgMetric.Penalty_Type__c, PG_Template__c=aPgMetric.PG_Template__c, Opportunity__c=aPgMetric.Opportunity__c,
                                                Non_Standard_Reason__c=aPgMetric.Non_Standard_Reason__c, Non_Standard_Details__c=aPgMetric.Non_Standard_Details__c,Metric_Status__c=aPgMetric.Metric_Status__c, HEDIS_Sites__c=aPgMetric.HEDIS_Sites__c,
                                                HEDIS_Site_Quantity__c=aPgMetric.HEDIS_Site_Quantity__c, Current_YTD_PG_Result__c=aPgMetric.Current_YTD_PG_Result__c, Current_PG_Metric_Level__c=aPgMetric.Current_PG_Metric_Level__c,
                                                Business_Owner__c=aPgMetric.Business_Owner__c, At_Risk__c=aPgMetric.At_Risk__c, At_Risk_Percent__c=aPgMetric.At_Risk_Percent__c, At_Risk_Base__c=aPgMetric.At_Risk_Base__c, At_Risk_Base_Type__c=aPgMetric.At_Risk_Base_Type__c,
                                                Assessment_Period__c=aPgMetric.Assessment_Period__c, PG_Case__c=newPgCaseRecordId, Do_Not_Validate__c=true));
                                            }
                                            insert metricList;  
                    } 

All Answers

aalbertaalbert
When making an INSERT DML statement in Apex, the Id property can not be set on the object.

Shruti Sharan GShruti Sharan G
Many Many thanks for the reply but I am not speciying ID in DML Insert , may be it is saving twise hence it is Taking ID, Nowhere i have specified Id for new to be inserted record.
Not able to figure out actual cause.
Actually i am inserting rows in a child object for each parent object record in a list.
SuperfellSuperfell
perhaps you could post your code, so we don't have to guess at what you're doing.
falfadlifalfadli

I am having a similar issue....Except error is

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

Code is pasted below. Code fails on the insert pgMetricArray; call. I don't think I am specifying an ID here. I need it to insert and generate a new PG_Metric__c record. Any advice is appreciated.

 

Thanks

 

>>>>>>>>>>>>>>>>>>.

 

 

global class PGCase {
    //copy PGCase
        webService static String copyPgCase(id recordId){
       
       
        /* get case details */
       
                PG_Case__c aPgCase = [Select p.Well_Aware_Program__c, p.UW_Operation__c, p.Retirees_Included__c, p.Retirees_Covered__c, p.Primary_Sales_Rep__c,
                                        p.Pre_Post_65__c, p.Pharmacy_Carrier__c, p.PPO_OA_Subscribers__c, p.Opportunity__c, p.New_Renewal_PG__c,
                                        p.Network_POS_HMO_Subscribers__c, p.Medicare_Eligible__c, p.Medical_Enrolled_EE__c, p.Medical_Carrier__c,
                                        p.Length_of_Previous_Carrier__c, p.Length_of_Current_Carrier__c, p.Lead_UW__c, p.Health_Advisor__c, p.Funding_Type__c,
                                        p.Eligible_Employees__c, p.Effective_Start_Date__c, p.Effective_End_Date__c, p.Contact__c, p.Case_Type__c,
                                        p.Case_Description__c, p.UW_RD_Approver__c, p.UW_RVP_Approver__c From PG_Case__c p WHERE Id = :recordId Limit 1];
       
                        /*Get related metrics records for case */                       
           
           
           
                //Create New PG_Case Record using details from Case to be copied
                PG_Case__c  newPgCase = new PG_Case__c(Well_Aware_Program__c=aPgCase.Well_Aware_Program__c, UW_Operation__c=aPgCase.UW_Operation__c, Retirees_Included__c=aPgCase.Retirees_Included__c, Retirees_Covered__c=aPgCase.Retirees_Covered__c,
                                         Primary_Sales_Rep__c=aPgCase.Primary_Sales_Rep__c, Pre_Post_65__c=aPgCase.Pre_Post_65__c, Pharmacy_Carrier__c=aPgCase.Pharmacy_Carrier__c, PPO_OA_Subscribers__c=aPgCase.PPO_OA_Subscribers__c,
                                         Opportunity__c=aPgCase.Opportunity__c, New_Renewal_PG__c=aPgCase.New_Renewal_PG__c, Network_POS_HMO_Subscribers__c=aPgCase.Network_POS_HMO_Subscribers__c,
                                         Medicare_Eligible__c=aPgCase.Medicare_Eligible__c, Medical_Enrolled_EE__c=aPgCase.Medical_Enrolled_EE__c, Medical_Carrier__c=aPgCase.Medical_Carrier__c,
                                         Length_of_Previous_Carrier__c=aPgCase.Length_of_Previous_Carrier__c, Length_of_Current_Carrier__c=aPgCase.Length_of_Current_Carrier__c,
                                         Lead_UW__c=aPgCase.Lead_UW__c, Health_Advisor__c=aPgCase.Health_Advisor__c, Funding_Type__c=aPgCase.Funding_Type__c, Eligible_Employees__c=aPgCase.Eligible_Employees__c,
                                         Effective_Start_Date__c=aPgCase.Effective_Start_Date__c, Effective_End_Date__c=aPgCase.Effective_End_Date__c, Contact__c=aPgCase.Contact__c, Case_Type__c=aPgCase.Case_Type__c,
                                         Case_Description__c=aPgCase.Case_Description__c,  UW_RD_Approver__c=aPgCase.UW_RD_Approver__c, UW_RVP_Approver__c=aPgCase.UW_RVP_Approver__c );
                     
                  insert newPgCase;
           
               
                 //Get record Id associated with case
                  Id newPgCaseRecordId = newPgCase.Id; 
               
                for (PG_Metric__c[] pgMetricArray: [Select p.Total_Fees__c, p.Standard_Non_Standard__c, p.Rationale_for_Request__c, p.Probability_of_Payout__c, p.Penalty_Type__c,
                                            p.PG_Template__c, p.Opportunity__c, p.Non_Standard_Reason__c, p.Non_Standard_Details__c, p.Metric_Status__c, p.HEDIS_Sites__c,
                                            p.HEDIS_Site_Quantity__c, p.Current_YTD_PG_Result__c, p.Current_PG_Metric_Level__c, p.Business_Owner__c, p.At_Risk__c,
                                            p.At_Risk_Percent__c, p.At_Risk_Base__c, p.At_Risk_Base_Type__c, p.Assessment_Period__c From PG_Metric__c p
                                            Where p.Metric_Status__c != 'Withdrawn' and p.PG_Case__c = :recordId  ]) {
                                           
                                            for (PG_Metric__c aPgMetric : pgMetricArray) {                   
                                                //Create New PG_Metric Record
                                               
                                                PG_Metric__c newPgMetric = new PG_Metric__c( Total_Fees__c=aPgMetric.Total_Fees__c, Standard_Non_Standard__c=aPgMetric.Standard_Non_Standard__c, Rationale_for_Request__c=aPgMetric.Rationale_for_Request__c,
                                                Probability_of_Payout__c=aPgMetric.Probability_of_Payout__c, Penalty_Type__c=aPgMetric.Penalty_Type__c, PG_Template__c=aPgMetric.PG_Template__c, Opportunity__c=aPgMetric.Opportunity__c,
                                                Non_Standard_Reason__c=aPgMetric.Non_Standard_Reason__c, Non_Standard_Details__c=aPgMetric.Non_Standard_Details__c,Metric_Status__c=aPgMetric.Metric_Status__c, HEDIS_Sites__c=aPgMetric.HEDIS_Sites__c,
                                                HEDIS_Site_Quantity__c=aPgMetric.HEDIS_Site_Quantity__c, Current_YTD_PG_Result__c=aPgMetric.Current_YTD_PG_Result__c, Current_PG_Metric_Level__c=aPgMetric.Current_PG_Metric_Level__c,
                                                Business_Owner__c=aPgMetric.Business_Owner__c, At_Risk__c=aPgMetric.At_Risk__c, At_Risk_Percent__c=aPgMetric.At_Risk_Percent__c, At_Risk_Base__c=aPgMetric.At_Risk_Base__c, At_Risk_Base_Type__c=aPgMetric.At_Risk_Base_Type__c,
                                                Assessment_Period__c=aPgMetric.Assessment_Period__c, PG_Case__c=newPgCaseRecordId);
                                               
                                            }
                                                                       
                                            insert pgMetricArray;  
                                   
                    }
               
            return newPgCaseRecordId;
     
    }

}

falfadlifalfadli

Hi discovered the problem.....I was not passing in a list properly to the insert call (creating a list object and passing the list to the insert call).

 

Code below works.....

 

    for (PG_Metric__c[] pgMetricArray: [Select p.Total_Fees__c, p.Standard_Non_Standard__c, p.Rationale_for_Request__c, p.Probability_of_Payout__c, p.Penalty_Type__c,
                                            p.PG_Template__c, p.Opportunity__c, p.Non_Standard_Reason__c, p.Non_Standard_Details__c, p.Metric_Status__c, p.HEDIS_Sites__c,
                                            p.HEDIS_Site_Quantity__c, p.Current_YTD_PG_Result__c, p.Current_PG_Metric_Level__c, p.Business_Owner__c, p.At_Risk__c,
                                            p.At_Risk_Percent__c, p.At_Risk_Base__c, p.At_Risk_Base_Type__c, p.Assessment_Period__c From PG_Metric__c p
                                            Where p.Metric_Status__c != 'Withdrawn' and p.PG_Case__c = :recordId Limit 1000 ]) {
                                           
                                            List <PG_Metric__c> metricList = new List<PG_Metric__c>();
                                             
                                            for (PG_Metric__c aPgMetric : pgMetricArray) {       
                                                //Create New PG_Metric Record
                                                metricList.add(new PG_Metric__c( Total_Fees__c=aPgMetric.Total_Fees__c, Standard_Non_Standard__c=aPgMetric.Standard_Non_Standard__c, Rationale_for_Request__c=aPgMetric.Rationale_for_Request__c,
                                                Probability_of_Payout__c=aPgMetric.Probability_of_Payout__c, Penalty_Type__c=aPgMetric.Penalty_Type__c, PG_Template__c=aPgMetric.PG_Template__c, Opportunity__c=aPgMetric.Opportunity__c,
                                                Non_Standard_Reason__c=aPgMetric.Non_Standard_Reason__c, Non_Standard_Details__c=aPgMetric.Non_Standard_Details__c,Metric_Status__c=aPgMetric.Metric_Status__c, HEDIS_Sites__c=aPgMetric.HEDIS_Sites__c,
                                                HEDIS_Site_Quantity__c=aPgMetric.HEDIS_Site_Quantity__c, Current_YTD_PG_Result__c=aPgMetric.Current_YTD_PG_Result__c, Current_PG_Metric_Level__c=aPgMetric.Current_PG_Metric_Level__c,
                                                Business_Owner__c=aPgMetric.Business_Owner__c, At_Risk__c=aPgMetric.At_Risk__c, At_Risk_Percent__c=aPgMetric.At_Risk_Percent__c, At_Risk_Base__c=aPgMetric.At_Risk_Base__c, At_Risk_Base_Type__c=aPgMetric.At_Risk_Base_Type__c,
                                                Assessment_Period__c=aPgMetric.Assessment_Period__c, PG_Case__c=newPgCaseRecordId, Do_Not_Validate__c=true));
                                            }
                                            insert metricList;  
                    } 

This was selected as the best answer