Hi all! When an invoice is copied it brings along ...
# suitescript
g
Hi all! When an invoice is copied it brings along the original invoice's billing address even if the customer's billing address has changed. What is the correct way to do it? I tried in beforeSubmit but this didn't do anything. Any advice?
Copy code
// Set billing address from the customer's default billing address
const customerId = newRecord.getValue({fieldId: 'entity'});

if (customerId) {

    const customerRecord = record.load({type: record.Type.CUSTOMER, id: customerId });
    const addressCount = customerRecord.getLineCount({sublistId: 'addressbook'});

    for (var i = 0; i < addressCount; i++) {

        var defaultBilling = customerRecord.getSublistValue({sublistId: 'addressbook', fieldId: 'defaultbilling', line: i});

        if (defaultBilling) {

            const addressId = customerRecord.getSublistValue({sublistId: 'addressbook', fieldId: 'internalid', line: i});
            
            newRecord.setValue({fieldId: 'billaddresslist', value: addressId});
            break;

        }
    }
}
b
have you tried just hardcoding the
billaddresslist
without all the other code so that you can see the behaviour of setting the field
an extra common mistake is just looking at the
billaddress
area text and not looking at the actual
billingaddress
subrecord. Its not unusual for user event scripts to not actually update the
billaddress
after changing the billing address
g
I'm trying to get the billing address ID but getting it as a line in the addressbook sublist doesn't seem to be working. I seem to recall this needing to be referenced a record machine subrecord but I don't remember. I haven't touched this much since SuiteScript 1.0 😅
b
are you trying to get the internal id of the address subrecord? Or the id of the addressbook line?
They are different things
g
address book record, so I can set the value in the Bill to address
b
that is incorrect
be very specific about your terms
g
image.png
b
do you want to address book line id or the address subrecord internal id
g
Don't I need the ID here?
b
do not try mixing terms and ask for address book id
g
to setValue?
the text field is read only
image.png
in the addressbook sublist I see this ID, so I think that's what I need to set the value of the select field to
not the line number
b
there is also a difference between the line id and the line number
g
I'm so confused, sorry, I just need to set the customer's bill to select to the value of the default billing address, whatever that is
b
for example, in the screenshot you shared, the line number is 1 and the line id is 544852
g
because on invoice copy it keeps using the old address unless a new one is selected
b
the
billaddressfield
uses address book line ids as the values for select fields
so just set yours to 544852
g
ok but how do I get that ID in the first place because the code above doesn't get it
I figured it out, two problems. 1. The correct field is id not internalid 2. The block is inside the copy block but when I log context type at the beginning it says it's create, even though it's being created via the make a copy action from an existing invoice. This block is never firing therefore it is not updating the address.
That doesn't fix it either. The actual update of billaddress happens during fieldChanged. Changing the billaddresslist value in the user event does NOT change the actual value of the billaddress field. 😆
b
pay closer attention to my first responses
g
I see it now, sorry, had to do it the hard way first I guess
thank you for your advice!
Actual solution ended up being: During beforeLoad enable the billaddress field Get the default billing address from the customer record Set the value of the billaddress text field Disable the billaddress text field to preserve native functionality
setting
billaddresslist
to the ID of the addressbook subrecord did update the select field but because it fires on fieldChanged it didn't do anything on save.
live and learn 🙂
b
there are various things you can do in a beforeSubmit to force the billaddress text area to update. Ranging from removing the bill address subrecord to force an update to also setting the billaddress in code