Hi, I’m doing some testing in SB as I would like t...
# suitescript
l
Hi, I’m doing some testing in SB as I would like to override the shipping address on customer invoice on save. What am I missing in this script (see comments)?
/**
* @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
};
});
b
I assume you're asking the question because you don't get the expected outcome. What is happening? Is there an error?
l
It doesn't set the value at all. No value being set in the shipping address although the logs suggest that the values are retrieved from the location record.
b
How are you checking the address values after update? I've seen a bug where the address gets updated but the displayed Ship To address on the form does not reflect the change. Opening the address details on the form or using a search to view the individual address fields shows the correct values. Super annoying issue.
l
I was checking it in the UI. By opening the pop-up address form. Someone else from this group said that they could not make it work on beforeSubmit as well. Only afterSubmit which worked for me too. But it's just weird that it's not working on before Submit.
b
Agreed weird. beforeSubmit worked for me in a quick test today but isn't displaying correctly. Don't know why the transaction form and address pop up don't show the same thing.
l
Is Override ticked in your address pop up?
b
It was not. Checking that does update the address displayed which is helpful. Seems counterintuitive but has the right result.
l
Would you mind sharing a snippet of your beforeSubmit code?
b
I copied your code above. The only adjustments I made was turning it into a standalone function. That made it easy to add it to an existing script and call it when my user triggered the script. The rest was preferred formatting as I stepped through to review.
Copy code
function updateShippingAddress(scriptContext) {
	var newRecord = scriptContext.newRecord;
	
	var subsidiaryId = newRecord.getValue({	fieldId: 'subsidiary' });
	if (!subsidiaryId) {
		log.debug('Debug', 'No subsidiary found on the record. Exiting.');
		return;
	}
	
	try {
		// check subsidiary
		var subsidiaryFields = search.lookupFields({
			type: record.Type.SUBSIDIARY,
			id: subsidiaryId,
			columns: ['country']
		});
		var 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;
		}

		// check location
		var locationId = newRecord.getValue({ fieldId: 'location' });
		if (!locationId) {
			log.debug('Debug', 'No location found on the record. Exiting.');
			return;
		}
		
		var locationFields = search.lookupFields({
			type: record.Type.LOCATION,
			id: locationId,
			columns: ['address1', 'address2', 'city', 'state', 'zip', 'country']
		});
		log.debug('locationFields ', JSON.stringify(locationFields));

		var 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);
	} catch (e) {
		log.error({
			title: 'Error copying location address',
			details: e.toString()
		});
	}
 }
l
Thanks. Will take a look at it. Curious why mine didn't set the values at all on beforeSubmit unless I just missed it