Scenario: One the Landed Costs tab of Item Receipts, the field value of Cost Allocation Method drop down option to be set as Quantity, the second option, rather than weight.
Solution:
Normally this can be achieved by using workflow. In this condition we are not able to fetch the field “Cost Allocation Method” in workflow. So we use client script to fix this issue
/**
* @NApiVersion 2.1
* @NScriptType ClientScript
*/
define(['N/currentRecord'],
function(currentRecord){
function pageInit(scriptContext) {
var myRecord = currentRecord.get();
myRecord.setValue({
fieldId: 'landedcostmethod',
value: 'QUANTITY'
});
}
return {
pageInit: pageInit
};
});
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
/*******************************************************************************
* CLIENTNAME:Cleerline Technology Group
* CTG-47
* CTG Project
*************************************************************************
* Date : 07-03-2022
*
* Author: Jobin & Jismi IT Services LLP
* Script Description :
* The script is used to automatically add Cost allocation method as Quantity in create mode
* Date created :07-03-2022
* Created by: Vibina James, Jobin & Jismi IT Services LLP
* REVISION HISTORY
* Revision 1.0 ${07-03-2022}
*
******************************************************************************/
define(['N/record','N/ui/dialog', 'N/ui/serverWidget'],
/**
* @param{record} record
* @param{render} render
* @param{dialog} dialog
* @param{serverWidget} serverWidget
*/
(record, dialog, serverWidget) => {
/**
* Defines the function definition that is executed before record is loaded.
* @param {Object} scriptContext
* @param {Record} scriptContext.newRecord - New record
* @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
* @param {Form} scriptContext.form - Current form
* @param {ServletRequest} scriptContext.request - HTTP request information sent from the browser for a client action only.
* @since 2015.2
*/
function beforeLoad(scriptContext) {
try {
var currentrec = scriptContext.newRecord;
var method = currentrec.setValue({
fieldId: 'landedcostmethod',
value:'QUANTITY'
});
} catch (error) {
log.error('beforeLoad', error.message);
}
}
return {beforeLoad:beforeLoad};
});
Note: UserEvent script is not applicable here, since the code should be working in both Create and Edit mode. Client script will works in UI, for perform CSV imports we have to use UserEvent script