RE: How to set value to payment field on apply subtab’s invoice sublist on payment record using afterSubmit user event script 2.0?
I want to set a invoice dueAmount value to payment field on apply subtab’s invoice sublist on payment record using afterSubmit user event script 2.0.
I have get dueAmount value on invoice, I am creating payment record now want to set on payment record.
Please help!
/**
*@NApiVersion 2.1
*@NScriptType UserEventScript
*/
define([‘N/record’,’N/search’], function (record,search) {
function afterSubmit(context) {
try {
if (context.type == context.UserEventType.CREATE || context.type == context.UserEventType.EDIT) {
var inv_Rec = context.newRecord;
var inv_id = inv_Rec.id;
log.debug(‘Invoice Id’, inv_id);
var loadrec = record.load({
type: record.Type.INVOICE,
id: inv_id
});
var card_Val = loadrec.getValue({ fieldId: ‘custbody_celigo_shopify_giftcard_id’ }); // shopify_giftcard_id
log.debug(“card_Val”, card_Val);
if (card_Val != null && card_Val != ” && card_Val != ‘null’ && card_Val != undefined && card_Val != ‘undefined’ && card_Val != ‘@NONE@’ && card_Val != ‘NaN’) {
var DueAmount = loadrec.getValue({ fieldId: ‘amountremainingtotalbox’ }); //Amount Due
log.debug(“DueAmount”, DueAmount);
var Shopify_Dt = loadrec.getValue({ fieldId: ‘custbody9’ });
log.debug(“Shopify date”, Shopify_Dt);
if (DueAmount != null && DueAmount != ” && DueAmount != ‘null’ && DueAmount != undefined && DueAmount != ‘undefined’ && DueAmount != ‘@NONE@’ && DueAmount != ‘NaN’ || DueAmount == 0) {
var salesOrders = search.create({
type: “salesorder”,
filters:
[
[“type”, “anyof”, “SalesOrd”],
“AND”,
[“applyingtransaction.internalid”, “anyof”, inv_id],
“AND”,
[“mainline”, “is”, “F”]
],
columns:
[
search.createColumn({
name: “internalid”,
summary: “GROUP”,
label: “Internal ID”
}),
search.createColumn({
name: “shipcountry”,
summary: “GROUP”,
label: “Shipping Country”
})
]
});
log.debug(“salesOrders”, salesOrders);
let searchResult = salesOrders.run().getRange({ start: 0, end: 10 });
if (searchResult.length > 0) {
var so_id = searchResult[0].getValue({
name: “internalid”,
summary: “GROUP”,
label: “Internal ID”
});
var shipCountry = searchResult[0].getValue({
name: “shipcountry”,
summary: “GROUP”,
label: “Shipping Country”
});
log.debug(“so_id”, so_id);
log.debug(“shipCountry”, shipCountry);
}
//if (shipCountry == ‘CA’ || shipCountry == ‘AU’) {
var trRecord = record.transform({
fromType: record.Type.INVOICE,
fromId: inv_id,
toType: record.Type.CUSTOMER_PAYMENT,
isDynamic: true,
});
log.debug(“Transfer Record”, trRecord);
trRecord.setValue({
fieldId: ‘paymentoption’,
value: 18
});
trRecord.setValue({
fieldId: ‘custbody_salesord_id’,
value: so_id
});
trRecord.setValue({
fieldId: ‘trandate’,
value: Shopify_Dt
});
trRecord.setValue({
fieldId: ‘account’,
value: 959
});
trRecord.setValue({
fieldId: ‘paymentoption’,
value: 15
});
/* trRecord.setValue({
fieldId: ‘applied’,
value: DueAmount
});
trRecord.setValue({
fieldId: ‘autoapply’,
value: true
});*/
var lineCount = trRecord.getLineCount({sublistId: ‘apply’});
log.debug(“lineCount”,lineCount);
// Set the Payment field value for the current invoice record
trRecord.setCurrentSublistValue({
sublistId: ‘apply’,
fieldId: ‘amount’,
line: i,
value: DueAmount // Replace 100 with the desired Payment field value
});
var rec_id = trRecord.save();
log.debug(“save record”, rec_id);
//}
}
}
}
}
catch (e) {
log.error({
title: ‘Error ‘,
details: e.message
});
}
}
return {
afterSubmit: afterSubmit
};
});
// NFT const payment = new CustomerPayment(record.transform(...)) // optionally clear the 'apply' checkbox if auto-apply is happening _.foreach(payment.apply, line => line.apply = false) const applyLine = _.find( payment.apply, line => line.custbody_salesord_id == so_id) applyLine.apply = true applyLine.account = 959 ... etc. payment.save()