An auto invoice creation exists in UBUY which will create an invoice when an item fulfillment is created.
For sales orders, a customer deposit is created so that when an invoice is created the amount in the customer deposit will be applied to the invoice automatically.
Ubuy needs new customization which will delete the related invoice and the deposit application when an item fulfillment is deleted.
Note:To link the item fulfillment and the invoice a custom field is created in the invoice record.
Solution:
User event script has been developed to delete the related invoice and deposit application when an IF is deleted and the script work for mass delete using inline editing
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define(['N/record','N/search','N/runtime'],
(record,search,runtime) => {
function salesOrderSearch(id){
try{
var salesorderSearchObj = search.create({
type: "salesorder",
filters:
[
["type","anyof","SalesOrd"],
"AND",
["internalid","anyof",id],
"AND",
["applyingtransaction.type","anyof","CustInvc"],
"AND",
["mainline","is","F"],
"AND",
["cogs","is","F"],
"AND",
["shipping","is","F"],
"AND",
["taxline","is","F"],
"AND",
["item.type","noneof","Discount"]
],
columns:
[
search.createColumn({
name: "ordertype",
sort: search.Sort.ASC,
label: "Order Type"
}),
search.createColumn({name: "applyingtransaction", label: "Applying Transaction"}),
search.createColumn({
name: "custbody_jj_related_if",
join: "applyingTransaction",
label: "Related IF"
}),
search.createColumn({name: "item", label: "Item"})
]
});
var srchArr = [];
var searchResultCount = salesorderSearchObj.runPaged().count;
log.debug("salesorderSearchObj result count",searchResultCount);
salesorderSearchObj.run().each(function(result){
// .run().each has a limit of 4,000 results
var srchObj = {};
srchObj.invoiceId = result.getValue({
name: "applyingtransaction",
label: "Applying Transaction"
});
srchObj.itemfulfillmentId = result.getValue({
name: "custbody_jj_related_if",
join: "applyingTransaction",
label: "Related IF"
});
srchArr.push(srchObj);
return true;
});
log.debug("srchArr############",srchArr);
return srchArr;
}
catch (e) {
log.error("error @salesOrderSearchfunction", e);
}
}
function invoiceSrchFunction(invId){
try{
var invoiceSearchObj = search.create({
type: "invoice",
filters:
[
["type","anyof","CustInvc"],
"AND",
["applyingtransaction.type","anyof","DepAppl"],
"AND",
["internalid","anyof",invId]
],
columns:
[
search.createColumn({name: "tranid", label: "Document Number"}),
search.createColumn({name: "applyingtransaction", label: "Applying Transaction"})
]
});
var depositAppId
var searchResultCount = invoiceSearchObj.runPaged().count;
log.debug("invoiceSearchObj result count",searchResultCount);
invoiceSearchObj.run().each(function(result){
// .run().each has a limit of 4,000 results
depositAppId = result.getValue({
name: "applyingtransaction",
label: "Applying Transaction"
});
return true;
});
log.debug("depositAppId",depositAppId);
return depositAppId;
}
catch (e) {
log.error("error @invoiceSrchFunction", e);
}
}
/**
* Defines the function definition that is executed before 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 beforeSubmit = (scriptContext) => {
try {
//Check to see the user event action is a delete action
if (scriptContext.type == 'delete') {
log.debug("inside delete context", scriptContext.type);
var rec = scriptContext.newRecord;
var ifId = rec.id;
log.debug("IF ID", ifId);
var ifSearchLookup = search.lookupFields({
type: search.Type.ITEM_FULFILLMENT,
id: ifId,
columns: ['createdfrom']
});
var soId = ifSearchLookup["createdfrom"][0].value;
// log.debug('state: ' + state);
log.debug("SoId", soId);
var soSearchResult = salesOrderSearch(soId);
log.debug("soSearchResult", soSearchResult);
var InvId;
var srchIFID;
for(var j = 0; j < soSearchResult.length; j++) {
InvId = soSearchResult[j].invoiceId;
srchIFID = soSearchResult[j].itemfulfillmentId;
log.debug("InvId", InvId);
log.debug("srchIFID", srchIFID);
if(ifId == srchIFID){
var depositID = invoiceSrchFunction(InvId)
log.debug("invoiceSrchResult", depositID);
if(depositID == null || depositID == '') {
log.debug("inside if condition","true")
var InvoiceRecord= record.delete({
type: record.Type.INVOICE,
id: InvId,
});
}
else{
log.debug("inside else condition","true")
var depositAppRecord = record.delete({
type: record.Type.DEPOSIT_APPLICATION,
id: depositID,
});
var InvoiceRecord= record.delete({
type: record.Type.INVOICE,
id: InvId,
});
}
}
}
}
}
catch (e) {
log.error("error @ beforesubmit", e);
}
}
return {beforeSubmit}
});