Requirement
To add a field to the WW Supplier Bill form, with a link to the Item Receipt that is associated with that bill, even though bills are processed from the purchase order, and not directly from the item receipt.
Prerequisites
Need to create the following field for Supplier
- Item Receipt
- Bill Body field
- Field Type: Multiple select.
- Mandatory: No
Description of the Solution
The requirement can be achieved by using a script. The script will list the item receipt links after saving the bill(creation). It will list the related purchase order’s all Item Receipt links in the supplier bill. If a bill has multiple purchase orders, it will also list the all-purchase order’s item receipts in the supplier bill.
If the bill has no related purchase order, then it will not list item receipt links in the supplier bill.
We can use search to find the related item receipts. It will compare the billed items and received items.
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define(['N/record', 'N/url','N/search'],
/**
* @param{record} record
* @param{url} url
* @param{search} search
*/
(record, url,search) =>
{
let fieldValue =[];
function irSearch(billId)
{
try
{
let transactionSearchObj = search.create({
type: "transaction",
filters:
[
["internalid","anyof",billId],
"AND",
["shipping","is","F"],
"AND",
["taxline","is","F"],
"AND",
["cogs","is","F"],
"AND",
["mainline","is","F"],
"AND",
["appliedtotransaction.fulfillingtransaction","noneof","@NONE@"]
],
columns:
[
search.createColumn({
name: "fulfillingtransaction",
join: "appliedToTransaction",
summary: "GROUP",
sort: search.Sort.ASC,
label: "Fulfilling/Receiving Transaction"
})
]
});
let searchResultCount = transactionSearchObj.runPaged().count;
if (searchResultCount>0)
{
transactionSearchObj.run().each(function (result) {
let itemReceipt = result.getValue(transactionSearchObj.columns[0]);
fieldValue.push(itemReceipt);
return true;
});
}
return fieldValue;
}
catch (e)
{
log.error({title:"error@search",details: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 billId = scriptContext.newRecord.id;
let irId = irSearch(billId);
record.submitFields({type:record.Type.VENDOR_BILL,id:billId,values:{custbody_jj_itemreceipts: irId}});
}
catch (e)
{
log.error({title: 'error@afterSubmit', details:e});
}
}
return {afterSubmit}
});