Search code examples
formscustomizationnetsuiteaddressbooksuitescript

Script access to custom address field through addressbook


I am having trouble setting a text value in a custom field I added to the address form.

function fieldChanged_form(type, name) {
    if (name == 'custentity_bsi_agycampus') {

        var lnSeq = nlapiFindLineItemValue('addressbook', 'defaultbilling', 'T');

        if (lnSeq > 0) {
            console.log("selected line " + lnSeq);
            nlapiSelectLineItem('addressbook', lnSeq);
            var agency_campus = nlapiGetFieldText('custentity_bsi_agycampus');
            nlapiSetCurrentLineItemValue('addressbook',
                    'custrecord_bsi_agy_div_bur_sd', agency_campus, true, true);
            console.log('agency' + ',' + agency_campus);
        }

        nlapiCommitLineItem('addressbook');
        console.log('after commit: '
                + nlapiGetCurrentLineItemValue('addressbook',
                        'custrecord_bsi_agy_div_bur_sd'));
    }
}

This script(applied to the Customer Form under the custom code tab) will not set custrecord_bsi_agy_div_bur_sd with the text value from custentity_bsi_agycampus (a custom field in the customer form). However, if I change custrecord_bsi_agy_div_bur_sd to addr1 (a field that is a default in the address form), it works just like I'd like.

This leads me to wonder whether or not I can access my custom field in the address form through 'addressbook' like you can for all of the other address fields. Does anyone know the answer to that question or have an idea of how I can troubleshoot this issue?


Solution

  • I believe you need to work with addresses as subrecords. Play around with something patterned after this:

    // {nlobjSubrecord} Get one of the addresses off the sublist
    var subrecord = {};
    
    nlapiSelectLineItem('addressbook', 1);
    subrecord = nlapiEditCurrentLineItemSubrecord('addressbook', 'addressbookaddress');
    
    // Set the data on the subrecord
    subrecord.setFieldValue('attention', 'Some Guy');
    subrecord.setFieldValue('addr1', '1234 5th St');
    subrecord.setFieldValue('addr2', 'Apt 234');
    subrecord.setFieldValue('addrphone', '5558675309');
    subrecord.setFieldValue('city', 'Scottsdale');
    subrecord.setFieldValue('state', 'AZ');
    subrecord.setFieldValue('country', 'US');
    subrecord.setFieldValue('zip', '85260');
    
    // Commit the subrecord to its parent before submitting the parent itself
    subrecord.commit();