This script is to apply a filter to a standard ‘List/Record’ field so that only the required results are shown in the list. This is accomplished by creating a (virtual) field by scripting with the same name as the original field. The scripted field is visible only in the edit mode. Use a workflow to hide the original field in the edit mode. Note: As the scripted field cannot be fetched in saved search and is not available after save, the data in that field has to be copied to the original field before saving the record.
Script to Create a new field
function beforeLoad(scriptContext) {
try {
var form = scriptContext.form;
var rec = scriptContext.newRecord;
if (scriptContext.type != 'view') {
//adding extra columns to record
var currency_field = form.addField({
id: 'custpage_billingcurrencyfiltered',
type: serverWidget.FieldType.SELECT,
label: 'Billing Currency'
});
//edit mode
if (scriptContext.type == 'edit') {
//fetching data from orignal field
var currency_body = rec.getValue({
fieldId: 'custentity_jj_billing_currency'
});
log.debug('currency_body', currency_body)
if (currency_body) {
currency_field.defaultValue = currency_body
}
}
//fetching the necessary data for the list
var currency_list = searchCurrency()
//to set the initial value as 'null' so that blank selection is possible(optional)
currency_field.addSelectOption({
value: '',
text: ''
});
var type = 'currency'
setSelectvalues(currency_list, currency_field, type)
}
} catch (er) {
log.debug('error @ beforeLoad', er)
}
}
Setting the data from the scripted field to the original field
function beforeSubmit(scriptContext) {
var rec = scriptContext.newRecord;
if (scriptContext.type != 'xedit') {
//for billing currency
var currency_body = rec.getValue({
fieldId: 'custpage_billingcurrencyfiltered'
});// considered as new currency
//FETCH THE OLD CURRENCY (before setting the virtual field value. Hence old)
var oldCurrency = scriptContext.newRecord.getValue({
fieldId: 'custentity_jj_billing_currency'
});
log.debug('oldCurrency', oldCurrency)
rec.setValue({
fieldId: 'custentity_jj_billing_currency',
value: currency_body,
ignoreFieldChange: true
});
}
}