+ Start a Discussion
ckellieckellie 

Cloning Error: INVALID_FIELD_FOR_INSERT_UPDATE

I am working in a Custom Opportunity Clone and am trying to clone the Sales Team when I recieve the following error when I click the clone button:

System.DmlException: Insert failed. First exception on row 0 with id 00q50000002eoQZAAY; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]



Class.OpportunityCloneWithItemsController.MyClone: line 94, column 14 External entry point 

 

Here is the code in question to clone the sales team:

             // copy over the Opportunity Sales Team - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             List<OpportunityTeamMember> team= new List<OpportunityTeamMember>();
             for (OpportunityTeamMember otm : [Select id, TeamMemberRole,OpportunityAccessLevel, 
                  Userid, opportunity.id From OpportunityTeamMember where Opportunity.id = :o.id]) {
                  OpportunityTeamMember newotm = otm.clone(true);
                  newotm.opportunity = otm.Opportunity;
                  team.add(newOtm);
             }
             insert team;
      

 

Here is the entire code:

 

public with sharing class OpportunityCloneWithItemsController {

    //added an instance varaible for the standard controller
    private ApexPages.StandardController controller {get; set;}
     // add the instance for the variables being passed by id on the url
    public Opportunity o {get;set;}
    // set the id of the record that is created -- ONLY USED BY THE TEST CLASS
    public ID newRecordId {get;set;}


    public OpportunityCloneWithItemsController(ApexPages.StandardController controller) {

        //this.controller = controller;

        // load the current record
        o = (opportunity) controller.getRecord();
        system.debug('********** ' + o);
    }
    
    // method called from the VF's action attribute to clone the Opportunity
    public PageReference MyClone() {
 
         // setup the save point for rollback
         Savepoint sp = Database.setSavepoint();
         Opportunity o;
         Opportunity newO;
 

              //copy the Opportunity - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             o = [select 
                     Name, 
                     Estimated_Cost__c, 
                     Margin__c, 
                     Competitor_Primary__c,
                     Competitor_Secondary__c, 
                     Account_Shipping_Location__c, 
                     Amount, 
                     INCO_Terms__c,             
                     Related_Location__c, 
                     Language_to_prepare_docs_in__c, 
                     Campaignid, 
                     Equipment__c,             
                     Parent__c, 
                     Equip_Services_Options_to_be_quoted__c, 
                     Lead_Time_Ex_Works__c, 
                     Op_Market_Position__c,             
                     Product_1__c, 
                     Product1_condition__c, 
                     Product1_Line_Capacity__c, 
                     Product1_Bulk_density__c,
                     Product1_defect_description__c, 
                     Product_2__c, 
                     Product_2_Condition__c, 
                     Product_2_Line_Capacity__c,
                     Product_2_Bulk_Density__c, 
                     Product_2_Defect_Description__c, 
                     Description, 
                     PS__c, 
                     Farmco__c,
                     Service_Contract__c, 
                     Potato__c, 
                     Processed_Fruit_Veg__c, 
                     AIS__c, 
                     Project_System_Eng__c, 
                     Freshline__c,
                     Spare_Parts__c, 
                     Pharma__c, 
                     Misc_Other__c, 
                     CloseDate, 
                     StageName, 
                     AccountId,
                     OwnerID from Opportunity where ID = :this.o.ID];
             newO = o.clone(false);

    newO.Name = 'Cloned - ' + o.Name;
    newO.StageName = 'Prospecting';
    newO.CloseDate = o.CloseDate + 700;
    
             insert newO;
         System.debug('******* ID: ' + newO.ID);
    
    //Reset the Opp ID and resset fields to default values

    

             // copy over the Opportunity Sales Team - ONLY INCLUDE THE FIELDS YOU WANT TO CLONE
             List<OpportunityTeamMember> team= new List<OpportunityTeamMember>();
             for (OpportunityTeamMember otm : [Select id, TeamMemberRole,OpportunityAccessLevel, 
                  Userid, opportunity.id From OpportunityTeamMember where Opportunity.id = :o.id]) {
                  OpportunityTeamMember newotm = otm.clone(true);
                  newotm.opportunity = otm.Opportunity;
                  team.add(newOtm);
             }
             insert team;
      


     return new PageReference('/'+newO.id+'/e?retURL=%2F'+newO.id);
   return new PageReference('/'+  newO.Id);

    }

}

 

How do I solve this error?

 

Thank you

prageethprageeth

Hello;

It seems you don't need to use the clone method with the argument 'true' as below.

 

OpportunityTeamMember newotm = otm.clone(true);

 If you use clone(false), you will not get this error. However child object lists are not be cloned in this case. For an example if you are cloning an Opportunity, OpportunityLineItems are not be cloned. As I think in your case you can use clone(false) as below.

 

OpportunityTeamMember newotm = otm.clone(false);
ckellieckellie

Hello,

 

Thank you for your help. You are correct on both counts; changing the true to false corerected the error, but it also does not clone the related list items, which is what I want.

 

Is there another way to clone related list items, which part of the requirements of the product?

 

Thank you

ckellieckellie

I have been able to clone one item on the opportunity related list.

    Map<Id, OpportunityTeamMember> ot = new Map<Id, OpportunityTeamMember>();
    List<OpportunityTeamMember> otm;
              //copy the OpportunityTeamMember- ONLY INCLUDE THE FIELDS YOU WANT TO CLONE    
    If (ot.size()>0) {
            otm = [Select 
                    id, 
                    TeamMemberRole,
                    OpportunityAccessLevel, 
                    Userid 
                    From OpportunityTeamMember where Opportunity.id = :this.o.id];
           otm = otm.clone();
                
                insert otm;
    }

 

How do I iterate through the entire related list to clone all of the sales team members?

prageethprageeth

Hello following is an example to clone an account with its Opportunities(Related List). Sometimes you will get an Idea by looking at it.

 

public void doClone() {
      Account accOriginal = [SELECT Name FROM Account WHERE Id = '00190000003pyjL'];
      Account clonedAcc = accOriginal.clone(false);
      clonedAcc.name = 'My Cloned Account';
      insert clonedAcc;
      Opportunity[] oppList1 = [SELECT name, AccountId, stageName, closeDate FROM 
                                Opportunity WHERE accountId =: accOriginal.id];
      Opportunity[] oppList2 = new Opportunity[0];
      for (Opportunity o : oppList1) {
        Opportunity o1 = o.clone(false);
        o1.accountId = clonedAcc.Id;
        oppList2.add(o1);
      }
      insert oppList2;
}

 

 

foodrunnerfoodrunner

Thanky ou for the example. the example is only cloning one item on the related list at a time. How do I clone all items on the particular related list?

 

Thanks,