Trying to set a field on the address subrecord of ...
# suitescript
n
Trying to set a field on the address subrecord of a customer in this UE script. What am I missing?
for(var line = 0; line < linecount; line++)
                        
{
                            
var defaultBilling = scriptContext.newRecord.getSublistValue({'sublistId': addrSublist, 'fieldId':'defaultbilling', 'line': line});
                            
if (defaultBilling == true) {
                                
var objSubRecord = scriptContext.newRecord.getSublistSubrecord({
                                    
sublistId: addrSublist,
                                    
fieldId: 'addressbookaddress',
                                    
line: line
                                
});
                                
objSubRecord.setValue({'fieldId':'custrecord_kes_invoiceemail', 'value': 'test'});
                            
}
                        
}
s
what's the value of your variable addrSublist?
n
addressbook
s
I'd check your field ids, I think your code is good
also try running it on the debugger, scriptContext.newRecord has a funny thing that not all values are there sometimes
when is this UE run btw, you might need to save the record?
n
Thanks @Sciuridae54696d i got it working. It was running in aftersubmit
s
ahh ok haha
Copy code
require(['N/record','N/runtime','N/search','N/format'], function(record,runtime,search,format) {
  try{
    var customerId = 2647;
    var rec = record.load({type:record.Type.CUSTOMER,id:customerId,isDynamic:false});
    var addrSublist = 'addressbook';
    var linecount = rec.getLineCount({sublistId:addrSublist});
    for(var line = 0; line < linecount; line++)
                            {
                                var defaultBilling = rec.getSublistValue({'sublistId': addrSublist, 'fieldId':'defaultbilling', 'line': line});
                                
                                if (defaultBilling == true) {
                                    var objSubRecord = rec.getSublistSubrecord({
                                        sublistId: addrSublist,
                                        fieldId: 'addressbookaddress',
                                        line: line
                                    });
                                    log.debug("success");
                                    
                                    
                                    objSubRecord.setValue({'fieldId':'addr2', 'value': 'test'});
                                    //objSubRecord.save();
                                    
                                }
                            }

                            rec.save();




  var abc = 1;

  } catch (e) {
    var scriptId = runtime.getCurrentScript().id;
    log.error('ERROR:'+scriptId+':fn:'+runtime.ContextType, JSON.stringify(e));
  }
});
was sure this ran fine for me on my debugger so your code was good, probably execution context problems!
s
another way to write this sort of code
🤯 2
👍 1
👀 2
n
cool, thanks @stalbert
w
@stalbert with NFT, that is?
s
alas, yes.
w
I've never got around to try it out... do you use it in every script you create?
s
yup. It generally results in about half the lines of code and easier to understand so it's hard not to use it.
the above code snippet is actually the more complex (aka more 'functional') version. You could also skip the
.filter
and use
.find
to get the default billing then operate on that.