Jira Code: Prof -30
A script to update the AR balances of customers. The balance is calculated with previous month income and current monthly income. A scheduled script is used to compare the fields and update the AR balance field.
/**
* @NApiVersion 2.x
* @NScriptType ScheduledScript
* @NModuleScope SameAccount
*/
define(['N/email', 'N/http', 'N/https', 'N/record', 'N/runtime', 'N/search', 'N/url', 'N/task'],
/**
* @param {email} email
* @param {http} http
* @param {https} https
* @param {record} record
* @param {runtime} runtime
* @param {search} search
* @param {url} url
*/
function(email, http, https, record, runtime, search, url, task) {
/**
* Definition of the Scheduled script trigger point.
*
* @param {Object} scriptContext
* @param {string} scriptContext.type - The context in which the script is executed. It is one of the values from the scriptContext.InvocationType enum.
* @Since 2015.2
*/
var main = {
addCommasToNumber: function(n) {
return n.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
},
checkforusage: function(type) {
var scriptObj = runtime.getCurrentScript();
var remainingTime = scriptObj.getRemainingUsage();
log.debug("Remaining governance units@ " + type, remainingTime);
},
scheduleanother: function(position) {
var scheduleScrptTask = task.create({
taskType: task.TaskType.SCHEDULED_SCRIPT,
scriptId: "customscript_prof_30_ss_ar_balance",
deploymentId: 'customdeploy2',
params: {
custscript_jj_position: position
}
});
var schedulescript = scheduleScrptTask.submit();
log.debug("schedulescript", schedulescript);
},
execute: function(scriptContext) {
var currentposition = runtime.getCurrentScript().getParameter("custscript_jj_position");
var currentposition = parseInt(currentposition);
var customers = main.createsearch(currentposition);
log.debug("customers", customers.currentdataobj);
main.setthehighest(customers.currentdataobj);
if (currentposition + 1 < customers.searchlength) {
main.scheduleanother(parseInt(currentposition) + 1)
}
},
createsearch: function(currentposition) {
log.debug("currentposition", currentposition);
var customerSearchObj = search.create({
type: "customer",
filters: [],
columns: [
search.createColumn({ name: "internalid", label: "internalid" }),
search.createColumn({ name: "custentity_jj_highest_ar_balance", label: "AR" }),
search.createColumn({ name: "balance", label: "Balance" })
]
});
var searchPageRanges = customerSearchObj.runPaged({
pageSize: 50
});
var currentdataobj = [];
var counter = 0;
searchPageRanges.fetch({
index: currentposition
}).data.forEach(function(result) {
var tempobj = {};
tempobj.internalid = result.getValue({
name: "internalid"
});
tempobj.balance = result.getValue({
name: "balance"
});
tempobj.arbalance = result.getValue({
name: "custentity_jj_highest_ar_balance"
});
currentdataobj.push(tempobj);
counter++
return true;
});
return {
currentdataobj: currentdataobj,
searchlength: searchPageRanges.pageRanges.length
};
},
setthehighest: function(customers) {
for (var i = 0; i < customers.length; i++) {
try {
var changed = false;
var customerobj = record.load({
type: "customer",
id: customers[i].internalid,
isDynamic: true,
});
var currentbalance = customerobj.getValue("balance");
var highestarbalance = customerobj.getValue("custentity_jj_highest_ar_balance");
/* var highestarbalance = parseFloat(highestarbalance.replace(",", ""));*/
if (highestarbalance == null || highestarbalance == undefined || highestarbalance == "" || highestarbalance == NaN) {
changed = true;
customerobj.setValue({
fieldId: 'custentity_jj_highest_ar_balance',
value: currentbalance,
ignoreFieldChange: true
});
} else if (currentbalance >= highestarbalance) {
changed = true;
customerobj.setValue({
fieldId: 'custentity_jj_highest_ar_balance',
value: currentbalance,
ignoreFieldChange: true
});
}
var recid = customerobj.save();
log.debug(recid, "currentbalance =" + currentbalance + " highestarbalance = " + highestarbalance + " changed = " + changed);
} catch (e) {
log.debug("e in " + customers[i].internalid, e);
}
}
}
}
for (var key in main) {
if (typeof main[key] === 'function') {
main[key] = trycatch(main[key], key);
}
}
function trycatch(myfunction, key) {
return function() {
try {
return myfunction.apply(this, arguments);
} catch (e) {
log.debug("e in " + key, e);
}
}
};
return main;
});