When entering Weekly timesheet though the UI we ha...
# suitescript
n
When entering Weekly timesheet though the UI we have this following script which grabs the correct billing item for each customer. It's a field on their Customer record. This is recovered correctly. The issue I have is the the "Billable" tick box is not always ticked on the populated line. 95% of the time it is. 5% it will be unticked, it is not consistent. A new line entered immediately afterwards might be ticked for the same Customer.
/**
* @NApiVersion 2.x
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
define(['N/search'],
function(search) {
/**
* Function to be executed when field is changed.
*
* @param {Object} context
* @param {Record} context.currentRecord - Current form record
* @param {string} context.sublistId - Sublist name
* @param {string} context.fieldId - Field name
* @param {number} context.lineNum - Line number. Will be undefined if not a sublist or matrix field
* @param {number} context.columnNum - Line number. Will be undefined if not a matrix field
*
* @since 2015.2
*/
function fieldChanged(context) {
var rec 		= context.currentRecord;
var changeFieldid = context.fieldId;
//detect when the Customer entered on a time line
if (changeFieldid == 'customer'){
//log.debug(['recType','sublistid','changeFieldid'],[recType,sublistid,changeFieldid]);
var currCustomer = rec.getCurrentSublistValue({
sublistId: 'timeitem',
fieldId: 'customer'
});
//find the time item for this Customer record
var defaultItemId =	getCustomerItemDefault(currCustomer);
if (defaultItemId) {
//if there is an item, populate it to the line
rec.setCurrentSublistValue({
sublistId: 'timeitem',
fieldId: 'item',
value: defaultItemId
});
rec.setCurrentSublistValue({
sublistId: 'timeitem',
fieldId: 'isbillable',
value: true
});
//we need to commit the line so that is billable populates.
rec.commitLine({
sublistId: "timeitem"
});
}
}
}
//returns the service time item for this client.
function getCustomerItemDefault(custId){
var customerSearchObj = search.create({
type: "customer",
filters:
[
["internalidnumber","equalto",custId]
],
columns:
[
"custentity_default_time_item"
]
});
var searchResultCount = customerSearchObj.runPaged().count;
log.debug("customerSearchObj result count",searchResultCount);
customerSearchObj.run().each(function(result){
var columns = result.columns;
itemId = result.getValue(columns[0]);
//log.debug(['custId','itemId'],[custId,itemId])
});
return itemId;
}
return {
fieldChanged: fieldChanged,
};
});
b
setCurrentSublistValue is asynchronous
your code may be setting isbillable before its sourced from the item, making the sourced value overwrite
n
Ok. Will update this tomorrow.
g
HTH: the
/snippet
command in slack is great for sharing a bunch of code like this. better for both the op and the reader