Hi all. When creating a record from another recor...
# suitescript
x
Hi all. When creating a record from another record (non-transactional) is there a more efficient way of doing it other than the following? (done in SS1 just for demonstration purposes)
Copy code
var val1 = originalRecord.getFieldValue('field1');
var val2 = originalRecord.getFieldValue('field2');
...etc
var newRecord = nlapiCreateRecord('whatever');
newRecord.setFieldValue('field1', val1);
newRecord.setFieldValue('field2, val2);
...etc
s
I assume these are not the same record type?
x
Correct.
s
Create object at top of fields to get, with their values set to null, then loop the object, getting original value and setting the property on the object
x
That's what I was thinking originally, but I just realized I was kinda going about it the wrong way, hahaha
s
Copy code
var fieldsToGet = {
    field1: null,
    field2: null,
    field3: null,
    field4: null,
    field5: null,
    field6: null,
    field7: null,
}
for (var fieldId in fieldsToGet){
    fieldsToGet[fieldId] = originalRec.getValue(fieldId);
}
for (var fieldId2 in fieldsToGet){
    newRec.setValue(fieldId2, fieldsToGet[fieldId2]);
}
something like that
x
Right, I just had a brain fart.
🧠 1
b
im a fan of arrays
Copy code
var newRecord = nlapiCreateRecord("whatever");
var fields = ["field1", "field2"];
fields.forEach(function(field) {
  newRecord.setFieldValue(field, originalRecord.getFieldValue(field));
});
s
I haven't had to do this recently, but with NFT this would be something like
Copy code
const newRec = new RecordType1()
const originalRecord = new RecordType2()
_.merge(newRec, originalRecord)
I'm a fan of objects, (well, when working with objects.)