User Event Script To Split Vendor Prepayment In Vendor Prepayment Application Record

Scenario:

The client needs to apply a fixed percentage of the prepayment amount for the line item in the Bill record. When the user is creating a vendor prepayment application record and the vendor prepayment amount will be applied to a purchase order record. The prepayment amount need not be fully applied to the bill but only as a fixed percentage of the vendor prepayment at the item line level. 

It is based on the percentage defined in the custom field in the purchase order item line level.

/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define(['N/record', 'N/search'],
/**
* @param{record} record
* @param{search} search
*/
(record, search) => {

function searchDetails(payment){
try {
let vendorprepaymentSearchObj = search.create({
type: "vendorprepayment",
filters:
[
["type", "anyof", "VPrep"],
"AND",
["mainline", "is", "T"],
"AND",
["internalid", "anyof", payment]
],
columns:
[
search.createColumn({name: "createdfrom", label: "Created From"}),
search.createColumn({
name: "custbody_jj_required_prepayment_per_",
join: "createdFrom",
label: "Required prepayment Percentage"
})
]
});
let obj={}
let searchResultCount = vendorprepaymentSearchObj.runPaged().count;
log.debug("vendorprepaymentSearchObj result count", searchResultCount);
vendorprepaymentSearchObj.run().each(function (result) {
obj.purchase = result.getValue({
name: "createdfrom", label: "Created From"
});
obj.percentage = result.getValue({
name: "custbody_jj_required_prepayment_per_",
join: "createdFrom",
label: "Required prepayment Percentage"
})
return true;
});
return obj;
}catch (e) {
log.debug("error @purchaseSearch",e)
}
}

function purchaseItemDetails(purchase){
try {
let purchaseorderSearchObj = search.create({
type: "purchaseorder",
filters:
[
["type", "anyof", "PurchOrd"],
"AND",
["internalid", "anyof", purchase],
"AND",
["mainline", "is", "F"]
],
columns:
[
search.createColumn({name: "item", label: "Item"})
]
});
let searchResultCount = purchaseorderSearchObj.runPaged().count;
let itemArray = []
log.debug("purchaseorderSearchObj result count", searchResultCount);
purchaseorderSearchObj.run().each(function (result) {
itemArray.push(result.getValue({
name: "item", label: "Item"
}))
return true;
});
return itemArray;
}catch (e) {
log.debug("error @ purchaseItemSearch",e)
}
}

function billItemDetails(bill){
try {
let vendorbillSearchObj = search.create({
type: "vendorbill",
filters:
[
["type", "anyof", "VendBill"],
"AND",
["internalid", "anyof", "21841"],
"AND",
["mainline", "is", "F"]
],
columns:
[
search.createColumn({name: "item", label: "Item"}),
search.createColumn({name: "amount", label: "Amount"})
]
});
let searchResultCount = vendorbillSearchObj.runPaged().count;
let itemArray = []
log.debug("vendorbillSearchObj result count", searchResultCount);
vendorbillSearchObj.run().each(function (result) {
let obj = {}
obj.item = result.getValue({
name: "item", label: "Item"
})
obj.amount = result.getValue({
name: "amount", label: "Amount"
})
itemArray.push(obj)
return true;
});
return itemArray;
}catch (e) {
log.debug("error@billItemDetails",e)
}
}
/**
* Defines the function definition that is executed after record is submitted.
* @param {Object} scriptContext
* @param {Record} scriptContext.newRecord - New record
* @param {Record} scriptContext.oldRecord - Old record
* @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
* @since 2015.2
*/
const afterSubmit = (scriptContext) => {
try {
let currentRec = scriptContext.newRecord
log.debug("Current Record", currentRec)
let id = currentRec.id
log.debug("Id", id);
let payment = currentRec.getValue({
fieldId: "vendorprepayment"
});
log.debug("Payment", payment);

let purchaseDetails = searchDetails(payment);
log.debug("Purchase Details", purchaseDetails);
if(purchaseDetails.purchase && purchaseDetails.purchase!==""){
let purchaseItems=purchaseItemDetails(purchaseDetails.purchase)
log.debug("purchaseItems",purchaseItems)
let billCount=currentRec.getLineCount({
sublistId: "bill"
});
log.debug("Bill Line Count",billCount)
let billID="",line
for(let i=0;i<billCount;i++){
let isApply=currentRec.getSublistValue({
sublistId: "bill",
fieldId: "apply",
line: i
});
log.debug("IS Apply",isApply)
if(isApply===true || isApply==='true'){
billID=currentRec.getSublistValue({
sublistId: "bill",
fieldId: "doc",
line: i
})
line =i
}
log.debug("billID",billID);
}
if(billID && billID!=="") {
let billItems = billItemDetails(billID)
log.debug("Bill Items",billItems)
let amount=0;
for(let i=0;i<purchaseItems.length;i++){
for(let j=0;j<billItems.length;j++){
if(purchaseItems[i]===billItems[j]["item"]){
log.debug(" Inside iF")
amount=parseInt(amount)+parseInt(billItems[j]["amount"])
}
}
}
log.debug("Amount",amount)
let amountAfterPercentage=((amount*parseInt(purchaseDetails.percentage))/100).toFixed(2);
log.debug("Amount After Percentage",amountAfterPercentage)
let record_load=record.load({
type:record.Type.VENDOR_PREPAYMENT_APPLICATION,
id: id,
isDynamic: false
});
//log.debug("record_load",record_load)
record_load.setSublistValue({
sublistId: "bill",
fieldId: "amount",
line: line,
value: amountAfterPercentage
})
record_load.save()

}



}
}catch (e) {
log.debug("error @afterSubmit",e)
}

}

return {afterSubmit}

});

Leave a comment

Your email address will not be published. Required fields are marked *