Luis
08/18/2025, 7:59 AM/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
* @NModuleScope SameAccount
*/
define(['N/record', 'N/runtime', 'N/search', 'N/log'],
(record, runtime, search, log) => {
const beforeSubmit = (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 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 shippingAddressSubrecord = newRecord.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
});
}
`log.audit('Success', Ship-to address updated from location record for Invoice ID: ${newRecord.id || 'New Record'}
);`
} catch (e) {
log.error({
title: 'Error copying location address',
details: e.toString()
});
}
};
return {
beforeSubmit: beforeSubmit
};
});
Celigo AI
08/18/2025, 7:59 AMAI generated answer (using internal Slack threads):Based on the conversations, there could be several reasons why the ship to address values are not being set in the invoice using the provided script: 1. Subsidiary or Location Missing: The script checks for the subsidiary and location fields in the record. If these fields are not found, the script will exit without setting the shipping address. 2. Subsidiary Country Not US: The script checks if the subsidiary's country is 'US'. If it's not, the script will exit without setting the shipping address. 3. Error in Address Fields: There might be an error in the address fields retrieved from the location record. Make sure the fields 'address1', 'address2', 'city', 'state', 'zip', 'country' are correctly set in the location record. 4. Error in Setting Address Values: There might be an error while setting the address values in the shipping address subrecord. Make sure the fieldIds 'country', 'city', 'state', 'zip', 'addr1', 'addr2' are correct and the values are valid. 5. Other Scripts or Workflows: There might be other scripts or workflows that are overriding the address values set by this script. Check if there are any other scripts or workflows that are triggered after this script and modify the shipping address. 6. Record Save Error: There might be an error while saving the record. Make sure the record is correctly saved after setting the shipping address. Remember to check the script logs for any error messages or debug information that might help identify the issue.
Celigo AI
08/18/2025, 8:00 AMSlack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.941992
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.890835047
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.882496953
Celigo AI
08/18/2025, 8:00 AM