How can we create a copy of a record which is yet ...
# suitescript
r
How can we create a copy of a record which is yet to be created(
record.save())
in SuiteScript? I have created a record using
record.create
and set some fields on it. I need to create a copy of everything and change few things over it, then save them on demand if needed.
v
record.copy
r
Record is not yet saved in Netsuite.
record.copy needs an internal id of saved record.
n
There may be a better way, but you could store all the fields you want to copy from one record to the next in an array. Then when you create the copy you just run through the array real quick to move the fields over.
Copy code
const parentRecord = record.create({type, isDynamic: true});

const fieldsToCopy = ['field1', 'field2', 'field3'];

const childRecord = record.create({type, isDynamic: true});

fieldsToCopy.forEach(field => {
  childRecord.setValue({
    fieldId: field,
    value: parentRecord.getValue(field)
  });
});
r
Thanks. That's what I am doing for now