Jira Code : BGGN-152
Description
The User event script is used to create the Invoice automatically from the Item Fulfillment for New Zealand
Solution
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
/*******************************************************************************
* CLIENTNAME:Bloom & Grow Group
* BGG-152
* BGG Project
*************************************************************************
* Date : 19-05-2022
*
* Author: Jobin & Jismi IT Services LLP
* Script Description :
* The script is used to create the Invoice automatically from the Item Fulfillment for New Zealand
* Date created : 19-05-2022
* Created by: Jobin & Jismi
* REVISION HISTORY
* Revision 1.0 ${24-05-2022}
*
******************************************************************************/
define(['N/record', 'N/search'],
/**
* @param{record} record
* @param{search} search
*/
(record, search) =>
{
/**
* Function to return an object with item id, order line value and quantity
* @param {Record} soRec
* @param {Object} itemFulfillRecObject - Object referring to the item fulfillment record created to trigger the script
* @param {Number} line - Number value representing the current line value in IF record
* @returns {{itemQuantityValue: *, itemIdValue: *, itemOrderLine: *}}
*/
function itemListElement ( soRec, itemFulfillRecObject, line, )
{
try
{
//Find the amount for each item from SO
let amount = soRec.getSublistValue({
sublistId: 'item',
fieldId : 'amount',
line : line
});
log.debug("Amount -SO "+line, amount );
//Find the amount for each item from SO
let quantity = soRec.getSublistValue({
sublistId: 'item',
fieldId : 'quantity',
line : line
});
log.debug("Quantity -SO "+line, quantity );
//Returns null value for quantity if not present in item fulfillment
let itemId = itemFulfillRecObject.getSublistValue( {
sublistId: 'item',
fieldId: 'item',
line: line
} );
let itemQuantity = itemFulfillRecObject.getSublistValue( {
sublistId: 'item',
fieldId: 'quantity',
line: line
} );
let orderLineValue = itemFulfillRecObject.getSublistValue( {
sublistId: 'item',
fieldId: 'orderline',
line: line
} );
let itemObject1 = { itemIdValue : itemId,
itemOrderLine : orderLineValue,
itemQuantityValue : itemQuantity,
itemSOamount : amount,
itemSOQuantity : quantity
};
return itemObject1;
}
catch ( err )
{
log.debug( "Error at itemListElement() function", err );
}
}
/**
* Function to add quantity value to each line item in invoice record if it is present in fulfillment
* @param invoiceRecObject - object representing the new invoice generated by the script
* @param itemList - Array with item id, orderline value, quantity represented as objects
*/
function addQtyRemoveLinesOnInvoice ( invoiceRecObject, itemList )
{
try
{
//since invoice created from sales order it have all items in sales order
let invoiceLine = invoiceRecObject.getLineCount( { sublistId: 'item' } );
//loop through the invoice object
for ( let i = 0; i < invoiceLine; i++ )
{
let orderLineValue = invoiceRecObject.getSublistValue( {
sublistId: 'item',
fieldId: 'orderline',
line: i
} );
log.debug( "orderLineValue", orderLineValue );
//Find matching value from the itemlist (IF) based on the orderline
let obj = itemList.find( obj => obj.itemOrderLine == orderLineValue );
log.debug( " matching obj", obj )
//set qty if orderline matches or null
if ( obj )
{
//adding quantity obtained from itemList object
invoiceRecObject.setSublistValue( {
sublistId: 'item',
fieldId: 'quantity',
line: i,
value: obj.itemQuantityValue
} );
//adding unit price field to invoice
invoiceRecObject.setSublistValue( {
sublistId: 'item',
fieldId: 'rate',
line: i,
value: obj.itemSOamount / obj.itemSOQuantity
} );
}
else
{
//adding quantity obtained from itemList object
invoiceRecObject.setSublistValue( {
sublistId: 'item',
fieldId: 'quantity',
line: i,
value: ""
} );
}
}
//loops to remove item lines in invoice
for ( var i = invoiceLine - 1; i >= 0; i-- )
{
if ( invoiceRecObject.getSublistValue( { sublistId: 'item', fieldId: 'quantity', line: i } ) == "" )
{
let orderLineValue = invoiceRecObject.getSublistValue( {
sublistId: 'item',
fieldId: 'orderline',
line: i
} );
log.debug( "removed lines", orderLineValue )
invoiceRecObject.removeLine( {// Removing unwanted lines from item sublist
sublistId: 'item',
line: i
} );
}
}
} catch ( err )
{
log.debug( "Error in addQtyRemoveLinesOnInvoice() function", err );
}
}
const afterSubmit = ( scriptContext ) =>
{
try
{
if (scriptContext.type == scriptContext.UserEventType.DELETE)
return false;
var itemList = [];
let oldIFRecord = scriptContext.oldRecord;
let newIFRecord = scriptContext.newRecord;
let newItemFulfillmentStatus = newIFRecord.getValue( { fieldId: "shipstatus" } );
if ( oldIFRecord != null )
var oldItemFulfillmentStatus = oldIFRecord.getValue( { fieldId: "shipstatus" } );
if ((scriptContext.type == "create" && newItemFulfillmentStatus == "C") || (scriptContext.type == "edit" && (oldItemFulfillmentStatus != "C" && newItemFulfillmentStatus == "C")) || scriptContext.type == "ship")
//if ( ( newItemFulfillmentStatus != oldItemFulfillmentStatus && newItemFulfillmentStatus == 'C' ) || scriptContext.type == scriptContext.UserEventType.SHIP )
{
let salesOrderId = newIFRecord.getValue({fieldId: "createdfrom"});
log.debug("Related sales order id", salesOrderId);
let soRec = record.load({
type : record.Type.SALES_ORDER,
id : salesOrderId });
//Finding the reference number of item fulfillment
let itemFulfillId = newIFRecord.id;
log.debug("Item fulfillment id", itemFulfillId);
let customer = newIFRecord.getText({fieldId: "entity"});
log.debug("Customer", customer);
let subsidiary = newIFRecord.getValue({fieldId: 'custbody_fulfillment_subsidiary'});
log.debug("Subsidiary", subsidiary);
if (customer != "The Stork Nest NZ" && subsidiary == 26)
{
//creating a invoice record using record.transform() function
let newInvoiceRecord = record.transform({
fromType: record.Type.SALES_ORDER,
fromId: salesOrderId,
toType: record.Type.INVOICE,
});
log.debug("Invoice generated ", newInvoiceRecord);
//Finding the line items count from invoice record
let lineCountItemSublist = newInvoiceRecord.getLineCount({sublistId: 'item'});
log.debug("lineCountItemSublist", lineCountItemSublist);
//creating itemList object with item id and quantity using for loop from item fulfillment
for (let i = 0; i < lineCountItemSublist; i++)
{
try
{
//creating an array of objects to store IF contents
itemList.push(itemListElement( soRec, newIFRecord, i ));
}
catch (e)
{
log.debug('Error at lineCountItemSublist', e);
}
}
log.debug(" Item List Array - IF", itemList );
//Adding quantity values to sublist in invoice using itemList object
addQtyRemoveLinesOnInvoice(newInvoiceRecord, itemList);
// Saving the transformed Invoice record
let invSaveObjID = newInvoiceRecord.save({
enableSourcing: true,
ignoreMandatoryFields: true
});
log.debug("Saved invoice is", invSaveObjID);
}
else
{
log.debug(" ----Auto Invoicing condition is not satisfied ---");
}
}
else
{
log.debug( "---Fulfillment status is not shipped, no invoice created----" )
}
}
catch ( err )
{
log.debug( { title: "Error at afterSubmit function body", details: err } );
}
}
return { afterSubmit }
} );