SuiteScript – Modifying Addresses

Understanding how to edit addresses under a customer turned out to be a bit tricky. Here is how I did it.

First, you can add custom fields, but to do that, you need to go to

Customization >> Forms >> Address Forms

Customize the “Standard Address Form.” Assign a country or countries to your form. This is important if you plan to use custom fields in an address. The form is where you add them. Custom fields will then be available to addresses in the assigned countries.

Next, it is important to understand that addresses are a subrecord. The sublist is called “addressbook” and is indexed by “addressbookaddress”. This will make more sense when you review the code below. You read the sublist, and then the associated subrecord under it. Here are the standard fields in the sublist for “addressbook”.

Label – not unique and if omitted becomes Address Line 1
defaultbilling – must be unique to one address
defaultshipping – must be unique to one address
isresidential – can apply to any or all addresses

Under each address is a subrecord that contains these fields:

Attention
Addressee
Address Line 1
Address Line 2
Address Line 3
City
State
Zip
Any custom fields that you’ve created

Here is a code example that demonstrates how to update addresses. The variable “line” corresponds with a zero-based index of the address as it appears in the Address tab under a customer. The variable “customer_id” is the internal ID of the customer and all assigned values are coming from variables which you’d set prior to running this code.

    /**
    * Load the record to gain access to the Address sublist 
    */
    var rec = record.load({
        type: record.Type.CUSTOMER,
        id: customer_id,
        isDynamic: false
    });

    if (/* you need to insert an address, do it here. You decide! */) {
        rec.insertLine({
            sublistId: 'addressbook',
            line: 0
        });
        line = 0;
    }

    /**
    * Modify those fields present in the Address sublist lines
    */
    rec.setSublistValue({
        sublistId: 'addressbook',
        fieldId: 'label',
        value: label,
        line: line
    });

    rec.setSublistValue({
        sublistId: 'addressbook',
        fieldId: 'defaultbilling',
        value: defaultbilling,
        line: line
    });

    rec.setSublistValue({
        sublistId: 'addressbook',
        fieldId: 'defaultshipping',
        value: defaultshipping,
        line: line
    });

    rec.setSublistValue({
        sublistId: 'addressbook',
        fieldId: 'isresidential',
        value: is_residential,
        line: line
    });


    /**
    * Load the sublist record, which holds all the actual address fields
    */
    var subrec2 = rec.getSublistSubrecord({
        sublistId: 'addressbook',
        fieldId: 'addressbookaddress',
        line: line
    });

    /**
        * Modify all subrecord fields
        */
    subrec2.setValue({
        fieldId: 'attention',
        value: attention
    });

    subrec2.setValue({
        fieldId: 'addressee',
        value: addressee
    })

    subrec2.setValue({
        fieldId: 'addr1',
        value: addr1
    });

    subrec2.setValue({
        fieldId: 'addr2',
        value: addr2
    });

    subrec2.setValue({
        fieldId: 'addr3',
        value: addr3
    })

    subrec2.setValue({
        fieldId: 'city',
        value: city
    });

    subrec2.setValue({
        fieldId: 'state',
        value: state
    });

    subrec2.setValue({
        fieldId: 'zip',
        value: zip
    });


    /**
    * Save the record
    */
    rec.save();

One thought on “SuiteScript – Modifying Addresses

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s