Hi I am total novice at suitecripting but I wanted to do something simple like our finance wants to have 2 fields on Quotes, Sales Orders and Invoices that show a 3% fee of the total and a new total field that shows the total with the fee *also i ve limited it to just 1 subsidiary (internal code 2). So i figured simple to get my feet wet in suite scripting and since those fields do not touch anything then i wont be impacting much. I created this script and deployed to those 3 record types and the 2 custom fields (custbody_custom_totalwfee and custbody_custom_totalwfee) are displayed on the forms . It works for Quotes but not Sales orders or Invoices. I cant seem to figure out what ive missed? any suggestions? (I used code snippets in VS to start me off then added what I wanted and used ChatGPT 4o to help with cleaning up the my original code).
/**
* @NApiVersion 2.x
* @NScriptType ClientScript
*/
define(['N/record', 'N/log'], function(record, log) {
function calculateProcessingFee(context) {
var currentRecord = context.currentRecord;
var subsidiary = currentRecord.getValue({ fieldId: 'subsidiary' });
log.debug('calculateProcessingFee', 'Subsidiary: ' + subsidiary);
// Check if the subsidiary is Internal ID: 2
if (subsidiary == 2) {
var total = currentRecord.getValue({ fieldId: 'total' });
log.debug('calculateProcessingFee', 'Total: ' + total);
if (total) {
var fee = total * 0.03;
currentRecord.setValue({
fieldId: 'custbody_credit_card_fee',
value: fee
});
log.debug('calculateProcessingFee', 'Credit Card Fee: ' + fee);
var customTotal = total + fee;
currentRecord.setValue({
fieldId: 'custbody_custom_totalwfee',
value: customTotal
});
log.debug('calculateProcessingFee', 'Custom Total with Fee: ' + customTotal);
} else {
log.debug('calculateProcessingFee', 'Total is not defined');
}
} else {
log.debug('calculateProcessingFee', 'Subsidiary does not match');
}
}
function fieldChanged(context) {
var fieldId = context.fieldId;
log.debug('fieldChanged', 'Field ID: ' + fieldId);
alert('Field Changed: ' + fieldId); // Alert added for debugging
if (fieldId === 'total' || fieldId === 'subsidiary') {
calculateProcessingFee(context);
}
}
function saveRecord(context) {
log.debug('saveRecord', 'Save Record Triggered');
alert('Save Record Triggered'); // Alert added for debugging
calculateProcessingFee(context);
return true;
}
return {
fieldChanged: fieldChanged,
saveRecord: saveRecord
};
});