0
votes

Context: A RESTlet written in SuiteScript v1, but I'll use v2 if I must.

Important: Solution must work serverside.

The location record has a field titled Main Address. The field id is mainaddress_text.

When I edit the Main Address field using the NetSuite website it presents me with the normal address editing form with the normal address fields, so it appears that NetSuite stores the address like it does the addresses on, for example, the customer record, in fields called Country, Attention, Addressee, Phone etc.

I want to get the values of these fields, but I can't work out how.

I have loaded the location using

location = nlapiLoadRecord('location', locationid)

The schema browser (https://system.netsuite.com/help/helpcenter/en_US/srbrowser/Browser2016_1/schema/record/location.html) suggests there is a field called mainAddress of type Address.

I have attempted to access the mainAddress field using statements like these:

location.getField('mainaddress') //returns null
location.getSubList('mainaddress') //returns null
location.getLineItemCount('mainaddress') //returns -1
location.getFieldValue('mainaddress') //returns null
location.getFieldText('mainaddress') //returns null

But no success.

I also tried location.getLineItemCount('addressbook') in case there was a field named like in the customer record but that returned -1.

The records browser (https://system.netsuite.com/help/helpcenter/en_US/srbrowser/Browser2016_1/script/record/location.html) suggests there is a field called addrtext of type Address.

I have attempted to access the addrtext field using statements like these:

location.getField('addrtext') //returns null
location.getSubList('addrtext') //returns null
location.getLineItemCount('addrtext') //returns -1
location.getFieldValue('addrtext') //returns null
location.getFieldText('addrtext') //returns null

But no success.

The records browser also describes fields called addr1, addr2, addr3, addressee, attention, city and so on.

I have attempted to access those fields using statements like these:

location.getFieldValue('attention') //returns null
location.getFieldValue('addressee') //returns null
location.getFieldValue('addr1') //returns null
location.getFieldValue('addr2') //returns null
location.getFieldValue('addr3') //returns null
location.getFieldValue('city') //returns null
location.getFieldValue('state') //returns null
location.getFieldValue('country') //returns null

But no success.

The only way I've found to get the address data is using location.getFieldValue('mainaddress_text') but that returns the address as one string with the parts of the address delimited by <br>-tokens.

How can I get the individual address fields?

2

2 Answers

3
votes

I suggest stringifying the record to see what's in it with JSON.stringify(location). The address fields are normally stored in addr1, etc...

It is possible that the address fields weren't set from the UI. I have seen some scripts that just set the mainaddress_text field, but neglect to change the individual fields as well which causes issues in the long run cause mainaddress_text won't match the other address fields (addr1, addr2, etc...).

You may want to try searching for the address fields, here's how to do it in SS 1.0:

var locationSearch = nlapiSearchRecord("location", null,
    [
       ["internalid", "anyof", "2"] //Change internal id
    ], 
    [
        new nlobjSearchColumn("attention", 'address', null),
        new nlobjSearchColumn("addressee", 'address', null),
        new nlobjSearchColumn("address1", null, null),
        new nlobjSearchColumn("address2", null, null),
        new nlobjSearchColumn("address3", null, null),
        new nlobjSearchColumn("city", null, null),
        new nlobjSearchColumn("state", null, null),
        new nlobjSearchColumn("country", null, null),
        new nlobjSearchColumn("zip", null, null),
        new nlobjSearchColumn("phone", null, null)
    ]
);

if (results)
{
    var result = results[0];

    return {
        attention: result.getValue('attention', 'address'),
        addressee: result.getValue('addressee', 'address'),
        address1: result.getValue('address1'),
        address2: result.getValue('address2'),
        address3: result.getValue('address3'),
        city: result.getValue('city'),
        state: result.getValue('state'),
        country: result.getValue('country'),
        zip: result.getValue('zip'),
        phone: result.getValue('phone')
    };
}
2
votes

Give this a try:

nlapiLoadRecord("location", locationID, {recordmode: "dynamic"})
.viewSubrecord("mainaddress")
.getFieldValue('addrtext');

I opened a Location record in a browser, and opened a console from there to load the nlobjRecord. Looking at the fields I see there is a mainaddress2_set field which is set by a nlapiViewSubrecord("mainaddress")

I couldn't make a call to the viewSubrecord("mainaddress") from the client side console, but I did create a simple suitelet to load the Location record and print the address.

function GET(request, response){
    var r = nlapiLoadRecord("location", "123456", {recordmode: "dynamic"});
    response.write(r.viewSubrecord("mainaddress").getFieldValue('addrtext'));
    response.write(r.viewSubrecord("mainaddress").getFieldValue('addr1'));
    // etc.
}