I’m editing the billing and shipping address subre...
# suitescript
m
I’m editing the billing and shipping address subrecord on sales orders. I’m trying to create a custom address and put specific values into each field. Regardless of the order I enter the fields, the city is populated automatically if the zip code is set. Is there a way to prevent this from happening? The scenario. We have to change the customer on about 1500 sales orders. This will immediately put the new customer's default billTo/shiptTo on the order. This script is attempting to put the addresses back to their original values. In some cases, the user has put the City in the
address2
field. So when this script attempts to update the address, we'll now have the city in both
address2
and
city
. If I load a sales order, open the subrecord and set
city
to
null
, the city field is emptied as expected. If I try setting all of the address fields and set
city
to
null
, the
city
field is still populated. This code takes an object with properties that match the address subrecord. I've tried changing the order so that country and zip are populated first, I've tried loading the record in standard and dynamic mode. Any thoughts as to how I can set all address fields but have the
city
field remain empty?
Copy code
const addressFieldIds = ['country', 'zip', 'addressee', 'attention', 'addr1', 'addr2', 'city', 'state', 'phone'];

const addressRecord = transaction.getSubrecord({ fieldId: subRecordFieldId });

addressFieldIds.forEach(function(fieldId) {
    let value = fieldId === 'country'
        ? getISOCountryCode(address[fieldId]) // Convert "United States" to "US"
        : address[fieldId];

   addressRecord.setValue({ fieldId, value: value || null,  });
});
a
In my mind city is the right field to be populated with the city and not vice versa. One way you can go about this is to create a search to get all addresses with the city in the address 2 field and update those to remove the city and have it in the proper field, this can be done via a search and then CSV import. Then you run your script and you are all good.
a
yeah I agree the approach here should be to null
address2
rather than
city
can you not just test if address2 === city addresss2 = null?
m
No doubt the data is bad, but I can't guarantee that
address2
isn't valid is most cases. It might contain 'Suite123' which is a perfectly valid value. The goal is to make the custom address match the original address (before changing the customer) exactly.
a
right, I understood that - you only change the address2 to null if it is equal to the city after you've populated zip
m
ooooh. That makes sense. Sometimes you're too close to it. Thanks!
👍 1