Is there a way to tell if an address has changed o...
# suitescript
c
Is there a way to tell if an address has changed on the customer record with a user event script? I'm attempting to write a script that will update the address in a seperate system when the default address on the customer record has changed. I'm running into trouble however, and i'm assuming it's because the address is a subrecord, so loading with the oldRecord or newRecord context produces the same result. Is there a workaround for this? I could compare the default address to the address in the other system for changes, but would rather compare in netsuite to avoid unnecesary external API calls if that's possible. Function is in the comments.
Copy code
function addressesChanged(context) {
            const newRec = context.newRecord;
            const oldRec = context.oldRecord;

            const oldDefaultShippingLine = oldRec.findSublistLineWithValue({
                sublistId: 'addressbook',
                fieldId: 'defaultshipping',
                value: true
            });

            const newDefaultShippingLine = newRec.findSublistLineWithValue({
                sublistId: 'addressbook',
                fieldId: 'defaultshipping',
                value: true
            });

            let oldDefaultAddress = {}
            let newDefaultAddress = {}

            const oldAddressSubrecord = oldRec.getSublistSubrecord({
                sublistId: 'addressbook',
                fieldId: 'addressbookaddress',
                line: oldDefaultShippingLine
            });

            const newAddressSubrecord = newRec.getSublistSubrecord({
                sublistId: 'addressbook',
                fieldId: 'addressbookaddress',
                line: newDefaultShippingLine
            });

            const oldAddress1 = oldAddressSubrecord.getValue({
                fieldId: 'addr1'
            });
            const oldAddress2 = oldAddressSubrecord.getValue({
                fieldId: 'addr2'
            });
            const oldAddress3 = oldAddressSubrecord.getValue({
                fieldId: 'addr3'
            });
            const oldCity = oldAddressSubrecord.getValue({
                fieldId: 'city'
            });
            const oldState = oldAddressSubrecord.getValue({
                fieldId: 'state'
            });
            const oldZip = oldAddressSubrecord.getValue({
                fieldId: 'zip'
            });
            oldDefaultAddress.address = (oldAddress1+oldAddress2+oldAddress3).trim().toLowerCase().replace(/\s+/g, '');
            oldDefaultAddress.city = oldCity;
            oldDefaultAddress.state = oldState;
            oldDefaultAddress.zip = oldZip;

            const newAddress1 = newAddressSubrecord.getValue({
                fieldId: 'addr1'
            });
            const newAddress2 = newAddressSubrecord.getValue({
                fieldId: 'addr2'
            });
            const newAddress3 = newAddressSubrecord.getValue({
                fieldId: 'addr3'
            });
            const newCity = newAddressSubrecord.getValue({
                fieldId: 'city'
            });
            const newState = newAddressSubrecord.getValue({
                fieldId: 'state'
            });
            const newZip = newAddressSubrecord.getValue({
                fieldId: 'zip'
            });
            newDefaultAddress.address = (newAddress1+newAddress2+newAddress3).trim().toLowerCase().replace(/\s+/g, '');
            newDefaultAddress.city = newCity;
            newDefaultAddress.state = newState;
            newDefaultAddress.zip = newZip;

            log.debug({title: 'old default addres object', details: oldDefaultAddress})
            log.debug({title: 'new default addres object', details: newDefaultAddress})

            if (newDefaultAddress.address != oldDefaultAddress.address ||
                newDefaultAddress.city != oldDefaultAddress.city ||
                newDefaultAddress.state != oldDefaultAddress.state || 
                newDefaultAddress.zip != oldDefaultAddress.zip
            ) {
                return true;
            }
            return false;

        }