I am trying to build a process where the user selects a clone action from a list of records along with a number. When they do that, they are cloning that records x number of times. I am using a parent component to clone the record selected and then passing the list to a child component to iterate over. The issue I am having is I am trying to set an id when I clone the record. The id for each record is ending up the same. If I populate a 3 in the clonevalue field, all three records end up with an ID of 2. In the code below, both the value of i and the value of gs.id are correct (as they loop, the values change from 0, 1 and 2. However the last log with the value of the clonedGroupStructures has a 2 for all three. Any help would be greatly appreciated!!!

The method for cloning is below. I have declared the following variable outside of the method as part of export: clonedGroupStructures = [];

    // From Row Action Dropdown - Clone record
cloneCurrentRecord(currentRow) {

    // clear out var first in case we run multiple times
    this.clonedGroupStructures = []

  let i;
  let gs = {};
    // open modal box
    this.bShowModal = true;
    this.isEditForm = false;
    this.isNewForm = false
    this.isReadForm = false;
    this.isCloneForm = true;

      //loop through based on number of clones selected
   for (i=0; i<this.cloneValue; i++){
       // c = this.groupstructurelist[i];
      //set variables to clone field over
    gs.Status__c = 'Draft';
    gs.Funding_Type__c = currentRow.Funding_Type__c;
    gs.Group_Number__c = currentRow.Group_Number__c;
    gs.Section_Code__c = currentRow.Section_Code__c;
    gs.Package_Code__c = currentRow.Package_Code__c;
    gs.Effective_Date__c = currentRow.Effective_Date__c;
    gs.End_date__c = currentRow.End_Date__c;
    gs.Health_Product__c = currentRow.Health_Product__c;
    gs.prescription__c = currentRow.prescription__c;
    gs.Dental__c = currentRow.dental__c;
    gs.vision__c = currentRow.vision__c;
    gs.Rating_Category__c = currentRow.Rating_Category__c;
    gs.Coverage_Categories__c = currentRow.Coverage_Categories__c;
    gs.Prefix__c = currentRow.Prefix__c;
    gs.CDH_Status__c = currentRow.CDH_Status__c;
    gs.Description__c = currentRow.Description__c;
    gs.Account__c = currentRow.Account__c;
    gs.id = i;
    gs.Description__c = i;
    console.log('1 here is i' + i);  //this displays the iterated value correctly
    console.log('2 here is gs.id ' + gs.id);  //this displays the iterated value correctly
      this.clonedGroupStructures.push(gs);  //this get populated with the same (last) value for i

    } // end of loop


1 Answer 1


gs has been declared outside the for-loop, so you are not creating 3 different objectes, you're just changing N times the properties of the same one and you're adding always the same object to clonedGroupStructures.
That's why the last values are the ones you see in every element of clonedGroupStructures.

cloneCurrentRecord(currentRow) {
    // this.clonedGroupStructures = []; // uncomment this line if clonedGroupStructures must contains only the actual clones
    //loop through based on number of clones selected
    for (let i=0; i<this.cloneValue; i++) {
        //set variables to clone field over
        const gs = {};
        gs.Status__c = 'Draft';
        gs.Funding_Type__c = currentRow.Funding_Type__c;
        gs.id = i;
    } // end of loop
    console.log('3 Here is clonedGroupStructures' + JSON.stringify(this.clonedGroupStructures));

then passing the list to a child component to iterate over

If every time you call the cloneCurrentRecord the list clonedGroupStructures must contains only the actual clones and you don't need the old ones, please uncomment the first line of the method.

I would also suggest to declare variables using const where possibile, otherwise let instead of var, this way you'll declares a block-scoped local variable.

  • Thanks Ruben!!! When I tred to declare inside the for look I get an error that all vars need to be declared at the start.. I will try const. thanks!!
    – Frank
    Jan 11, 2021 at 21:31
  • I'm working on this this morning and I'm not getting it. When the button is clicked I need to clone over x number of records into the array. I tried to create a new array variable inside the method but its still uses the same value for i. Even though the console.log shows me the value of i advancing, when it gets assigned to the array, its all the last value.
    – Frank
    Jan 12, 2021 at 15:58
  • @Frank could you please update your question posting the update code. Please post the whole controller
    – RubenDG
    Jan 12, 2021 at 17:44
  • Thanks RubenDG... I updated the code in the original post. thanks again!!
    – Frank
    Jan 13, 2021 at 13:35
  • 1
    @Frank in your code gs is still initialized outside the for-loop. Look what happens if you declare it inside the loop. If you gets an error when you try to declare it inside the for-loop, please post it here.
    – RubenDG
    Jan 13, 2021 at 14:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .