I’m running script that will automatically change the Memo field of the Amortization Journal based from the Memo of its Bill. However, I’m unable to do the change right after it is created/generated. The script only works when editing then saving it. Tried to Make Copy a record to test the Context Type Create and it works fine as well except when generating the Memo field.
I found some article and the reason is because schedulenum is blank yet when the script runs.
Just wondering if you have any ideas for a workwound?
/**
*
* @NApiVersion 2.x
* @NScriptType UserEventScript
*/
define([
'N/record',
'N/search',
'N/log',
'N/redirect'
],
function (record, search,log,redirect) {
function afterSubmit(context) {
var currentRecord = context.newRecord;
currentRecord = record.load({ type: currentRecord.type, id: currentRecord.id });
log.debug('currentRecord',currentRecord);
log.debug('context.type', context.type);
var isAmortization = currentRecord.getValue({ fieldId: "isfromamortization" });
var lineCount = currentRecord.getLineCount({sublistId: 'line'});
var newMemo;
var billMemo;
if(context.request && 'T' == context.request.parameters.custparam_fresh_status) return;
// if (context.type == context.UserEventType.CREATE && isAmortization == 'T') {
if (isAmortization == 'T') {
for (var i = 0; i < lineCount; i++) {
var amortSched = currentRecord.getSublistValue('line', 'schedulenum', i);
var oldMemo = currentRecord.getSublistValue('line', 'memo', i);
log.debug(amortSched, 'amortSched');
var vendorName = currentRecord.getSublistValue('line', 'entity_display', i);
log.debug(vendorName, 'vendorName');
var billRecord = search.create({
type: "transaction",
filters:
[
["amortizationschedule.internalid","anyof",amortSched],
"AND",
["type","anyof","VendBill"]
],
columns:
[
search.createColumn({name: "memo"})
]
});
billRecord.run().each(function(result) {
billMemo = result.getValue('memo');
});
log.debug(billMemo, 'billMemo');
if(oldMemo == 'Amortization Destination' || oldMemo == 'Amortization Source') {
newMemo = currentRecord.setSublistValue({
sublistId: 'line',
fieldId: 'memo',
line: i,
value: 'Amortization ' + billMemo + ' ' + vendorName
});
}
}
currentRecord.save({ ignoreMandatoryFields: true });
}
}
return {
afterSubmit: afterSubmit
};
});```
Tried making it as BeforeSubmit, Client Script PageInit and Workflow action but all didn't work.