Requirement
We need to display the external Id to the custom field in Department record. Usually External Ids are not displayed in any transactions/ records.
Solution
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define(['N/currentRecord', 'N/record', 'N/search', 'N/redirect', 'N/ui/serverWidget'],
/**
* @param{currentRecord} currentRecord
* @param{record} record
* @param{search} search
* @param{redirect} redirect
* @param{serverWidget} serverWidget
*/
(currentRecord, record, search, redirect, serverWidget) => {
const beforeLoad = (scriptContext) => {
try {
log.debug('intId',intId)
if(scriptContext.type==="view" || scriptContext.type==="edit" ) {
var currentRecord = scriptContext.newRecord
var intId = currentRecord.id;
var externalId = currentRecord.getValue({
fieldId: 'externalid'
})
log.debug('externalId', externalId)
var custExtId = currentRecord.getValue({
fieldId: 'custrecord_jj_external_id'
})
log.debug('custExtId', custExtId)
if ((custExtId == '') && (externalId != '')) {
log.debug('empty')
record.submitFields({
type: record.Type.DEPARTMENT,
id: intId,
values: {
custrecord_jj_external_id: externalId
},
options: {
enableSourcing: false,
ignoreMandatoryFields: true
}
});
}
var checkValue = currentRecord.getValue({
fieldId: 'custrecord_jj_check'
})
log.debug('Check value before',checkValue)
if(!checkValue){
let setUrl = '<html><body><script type="text/javascript">window.location.reload();</script></body></html>'
var fieldValue = scriptContext.form.addField({
id: 'custpage_hiddenfield',
type: serverWidget.FieldType.INLINEHTML,
label: 'Text'
});
fieldValue.defaultValue = setUrl;
}
record.submitFields({
type: record.Type.DEPARTMENT,
id: intId,
values: {
custrecord_jj_check: true
},
options: {
enableSourcing: false,
ignoreMandatoryFields: true
}
});
var checkValue1 = currentRecord.getValue({
fieldId: 'custrecord_jj_check'
})
log.debug('Check value after',checkValue1)
}
} catch (e) {
log.debug('Error@BeforeLoad', e)
}
}
return {beforeLoad}
});