Scripting newbie wants to step through and obtain ...
# suitescript
n
Scripting newbie wants to step through and obtain the value of all the custom fields on this custom record. The record that I load looks like this image and I want to load the highlighted fields with their list value using
for ( var key in newRecord ) {
if (newRecord.hasOwnProperty(key)) {
log.debug('key:'+key,' value:'+newRecord[key])
newRecord.getValue({
fieldId: key,
value: [key]
});
}
}
This steps through the keys of the objects like key:findMatrixSublistLineWithValue but I want to access my field. Thoughts appreciated.
b
The record logged by the debugger and logs is not the actual record
Its converted to its json representation first
If you want to access keys and values the same way, then work with the output of newRecord.toJSON()
alternatively use methods like Record.getFields to get your list of field ids
n
ok, loading now. thanks
s
AFAIK
toJSON()
for records is undocumented - I wouldn't dare write any code that relies on its output.
as battk mentioned, you can probably do something like
Copy code
const values = newRecord.getFields()
    .filter( name=> name.startsWith('custrecord'))
    .map( name=> newRecord.getValue({fieldId:name}))
n
I think @battk was suggesting I explore the object with. I ended up with
for ( var key in allFields ) {
if (allFields.hasOwnProperty(key)) {
if(JSON.stringify(allFields[key]).indexOf('custrecord')>-1){
var valField = newRecord.getValue({
fieldId: allFields[key]
});
log.debug('name:'+allFields[key]+' value : '+valField);
}
}
}
i'll try @stalbert code also.
s
I'm not sure what you're trying to accomplish with the nesting of
for..in
and use of
.hasOwnProperty
as that would be redundant. In other words, you should be able to skip the
if(allFields.hasOwnProperty()
part.
if you want to see the field names in the output make that last map call emit an object
Copy code
const values = newRecord.getFields()
    .filter( name=> name.startsWith('custrecord'))
    .map( name=> ({ name, value: newRecord.getValue({fieldId:name})}))
b
dont drag me into that abomination of code
s
I thought abomination was my word?
b
do something similar to stalbert if you want a functional style
n
working on it. thanks
s
I don't find I truly need such generic code like you're trying to do @Netsuite Tragic - are you sure you need to iterate so generally over all custom fields? Most often I find real world scripts would just use
record.getValue({ fieldId: 'custrecord_inspection_lights')
- i.e. just use the field names without getting fancy unless you really need to.
b
otherwise use a more normal loop
for (var i = 0; i < allFieldsl.length;i++)
there is no need to bring in a for in loop for an array
s
that's a good point - if you're now iterating over an array-ish thing and you insist on the newer syntax you want
for...of
not
for...in
. That distinction is one reason I tend to avoid
for
loops altogether and write things in a more functional style where the chained method names have actual meaning. 🙂
✅ 1
n
I did say scripting newbie and I'll work on it over weekend.
s
My advice is to try and keep things as simple as possible but no simpler. That has proven to be useful advice to me for many (many) years in programming.
b
if you didnt understand any of the things we suggested, you probably want to learn more javascript before doing suitescript