Is it possible to set the shipping address in a cu...
# suitescript
l
Is it possible to set the shipping address in a customer invoice (sourced from the location address) on beforeSubmit? For some reason, I can’t make it work. It works for me only when I trigger it on afterSubmit.
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
* @NModuleScope SameAccount
*/
define(['N/record', 'N/runtime', 'N/search', 'N/log'],
(record, runtime, search, log) => {
const afterSubmit = (scriptContext) => {
if (scriptContext.type !== scriptContext.UserEventType.CREATE && scriptContext.type !== scriptContext.UserEventType.EDIT) {
`log.debug('Debug',
Script did not run for event type: ${scriptContext.type}. Exiting.
);`
return;
}
const newRecord = scriptContext.newRecord;
const recordId = newRecord.id;
const subsidiaryId = newRecord.getValue({
fieldId: 'subsidiary'
});
if (!subsidiaryId) {
log.debug('Debug', 'No subsidiary found on the record. Exiting.');
return;
}
try {
const subsidiaryFields = search.lookupFields({
type: record.Type.SUBSIDIARY,
id: subsidiaryId,
columns: ['country']
});
const subsidiaryCountryValue = subsidiaryFields.country[0].value;
`log.debug('Debug',
Subsidiary country is: ${subsidiaryCountryValue}
);`
if (subsidiaryCountryValue !== 'US') {
log.debug('Debug', 'Subsidiary country is not USA. Exiting.');
return;
}
const locationId = newRecord.getValue({
fieldId: 'location'
});
if (!locationId) {
log.debug('Debug', 'No location found on the record. Exiting.');
return;
}
const locationFields = search.lookupFields({
type: record.Type.LOCATION,
id: locationId,
columns: [
'address1', 'address2', 'city', 'state', 'zip', 'country'
]
});
log.debug('locationFields', JSON.stringify(locationFields));
const loadedRecord = record.load({
type: newRecord.type,
id: recordId,
isDynamic: true
});
const shippingAddressSubrecord = loadedRecord.getSubrecord({
fieldId: 'shippingaddress'
});
shippingAddressSubrecord.setValue({
fieldId: 'country',
value: locationFields.country[0].value
});
shippingAddressSubrecord.setValue({
fieldId: 'city',
value: locationFields.city
});
shippingAddressSubrecord.setValue({
fieldId: 'state',
value: locationFields.state
});
shippingAddressSubrecord.setValue({
fieldId: 'zip',
value: locationFields.zip
});
shippingAddressSubrecord.setValue({
fieldId: 'addr1',
value: locationFields.address1
});
if (locationFields.address2) {
shippingAddressSubrecord.setValue({
fieldId: 'addr2',
value: locationFields.address2
});
}
const updatedRecordId = loadedRecord.save({
ignoreMandatoryFields: true
});
`log.audit('Success',
Ship-to address updated from location record for Record ID: ${updatedRecordId}
);`
} catch (e) {
log.error({
title: 'Error copying location address',
details: e.toString()
});
}
};
return {
afterSubmit: afterSubmit
};
});
m
We have resorted to setting the shipping address on newly created invoices in
afterSubmit
as well. No error, it just isn't updated.
l
Ohh interesting. Thanks, Mike