My Question is am displaying the records in Table format and there is a NEW button when it's pressed want to create a new row at the top. With all the fields empty.

Here doc contains the record values and DataFieldValue contains fieldNames.

Column are built dynamically. So copying the fieldNames from First Record and Making doc Object to null - (say Account)

And making a boolean to true for New record and once the script Runs the New record is inserted and lookup, picklist, input box is created based on fieldName.

But the boolean which was Changed also affects the Second Row(Record) which is RowItemList[1] now, after Javascript unshift

  newRecord :function(component,event,helper){ 

    var RowItemList = component.get("v.currentList");
    var newRow = {"DataFieldValue": RowItemList[0].DataFieldValue, "doc":{}};

    for (var i in newRow.DataFieldValue) {
        for(var j in newRow.DataFieldValue[i].multiFields){
            if(newRow.DataFieldValue[i].multiFields[j].fieldType == "search")
                newRow.DataFieldValue[i].multiFields[j].isrecordineditmode = true;
    component.set("v.currentList", RowItemList);

Suggest the Best way to bypass this issue.


Complex object values are assigned by reference in most languages, including JavaScript. When you do

var newRow = {"DataFieldValue": RowItemList[0].DataFieldValue, "doc":{}};

newRow.DataFieldValue and RowItemList[0].DataFieldValue refer to the same object instance. Hence, when you mutate that instance with

            newRow.DataFieldValue[i].multiFields[j].isrecordineditmode = true;

You are affecting both rows, because their underlying data store is the same object.

Your question doesn't include the actual data type of DataFieldValue or its contents, but what you need to do is make a copy of that object - a new object, containing the same data -, rather than just assigning it. You may find this answer on Stack Overflow about cloning JavaScript objects helpful.

  • Got it still the same Reference - Saved my day, Thank you.
    – SFDC
    Feb 18 '19 at 5:14

If you do not use functions within your object, a very simple one-liner can be the following:

Stringifying and parsing the object helped to clone the object with a totally new instance

var newRow = JSON.parse(JSON.stringify(InsernewRow));

Reference - https://stackoverflow.com/questions/728360/how-do-i-correctly-clone-a-javascript-object#answer-10869248

Answer link credit to @David Reed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.