Problem:
Proposal summary
When an Item fulfillment of a customer of status shipped is edited or status changed to shipped is saved, we can give suggestion of the trolley number of the item fulfillments which are of the same customer and same ship date.
Requirement
While performing an Item fulfillment of a customer ,we have to check whether this customer has any other item fulfillments that are of status shipped and on the same ship date that of current item fulfillment. If there is any Item fulfillments(IF) that meet this we have to lookup the trolley Number of that item fulfillment, So that we can perform multiple fulfilments for the same customer to be placed on the same trolley.
Solution:
- While we edit the Item Fulfillment record, our script will show a suggestion of item fulfillments and trolly number as a pop up message. Provided if the edited IF has status =shipped, and On-Demand Ship Date is not empty.
● We will show the same pop up suggestion on the field change of On-Demand Ship Date ,provided If the status field is shipped.
● We will show the same pop up suggestion on the IF field change of status to shipped, provided if we have data in On-Demand Ship Date.
/**
* @NApiVersion 2.x
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
/************************************************************************************************
BLL - 223 Trolley Number Lookup Script
*********************************************************************************************
*
* Author: Jobin & Jismi IT Services LLP
*
* Date Created : 25-February-2022
*
* Description : Trolley Number Lookup Script.
*
* REVISION HISTORY
*
***********************************************************************************************/
define(['N/currentRecord', 'N/record', 'N/search', 'N/ui/dialog','N/runtime'],
/**
* @param{currentRecord} currentRecord
* @param{record} record
* @param{search} search
* @param{dialog} dialog
*/
function(currentRecord, record, search, dialog,runtime) {
/**
* Function to be executed when field is changed.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.sublistId - Sublist name
* @param {string} scriptContext.fieldId - Field name
* @param {number} scriptContext.lineNum - Line number. Will be undefined if not a sublist or matrix field
* @param {number} scriptContext.columnNum - Line number. Will be undefined if not a matrix field
*
* @since 2015.2
*/
var sc;
function ifDetails(custName, shippingDate, status) {
try {
var itemfulfillmentSearchObj = search.create({
type: "itemfulfillment",
filters:
[
["type", "anyof", "ItemShip"],
"AND",
["customermain.internalid", "anyof", custName],
"AND",
["mainline", "is", "T"],
"AND",
["custbody_rpod_shipdate", "on", shippingDate],
"AND",
["status", "anyof", status],
"AND",
["custbody_trolleynumber", "isnotempty", ""]
],
columns:
[
search.createColumn({
name: "ordertype",
sort: search.Sort.ASC,
label: "Order Type"
}),
search.createColumn({name: "mainline", label: "*"}),
search.createColumn({name: "trandate", label: "Date"}),
search.createColumn({name: "tranid", label: "Document Number"}),
search.createColumn({name: "entity", label: "Name"}),
search.createColumn({name: "custbody_trolleynumber", label: "Trolley Number"}),
search.createColumn({name: "custbody_rpod_shipdate", label: "On-Demand Ship Date"}),
search.createColumn({name: "custbody_rpod_delivery_date", label: "Delivery Date"}),
search.createColumn({
name: "internalid",
join: "customerMain",
label: "Internal ID"
}),
search.createColumn({name: "statusref", label: "Status"})
]
});
var searchResultCount = itemfulfillmentSearchObj.runPaged().count;
console.log("itemfulfillmentSearchObj result count", searchResultCount);
var arr = [];
var customer, onDemandShipDate, trolleyNo, docNumber;
itemfulfillmentSearchObj.run().each(function (result) {
// .run().each has a limit of 4,000 results
console.log("search data", result)
var obj = {};
obj.customer = result.getValue({
name: "internalid",
join: "customerMain"
})
console.log("cust", obj.customer)
obj.onDemandShipDate = result.getValue({
name: "custbody_rpod_shipdate"
})
console.log("On Demand Ship date", obj.onDemandShipDate)
obj.trolleyNo = result.getValue({
name: "custbody_trolleynumber"
})
console.log("Trolley Number", obj.trolleyNo)
obj.docNumber = result.getValue({
name: "tranid"
})
console.log("Doc Number", obj.docNumber)
arr.push(obj);
return true;
});
return arr;
}
catch(err)
{
console.log(err)
}
}
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = '' + d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [day, month, year].join('/');
}
function fieldChanged(scriptContext) {
try{
// if(runtime.executionContext === "USERINTERFACE" ) {
console.log("Sc Value in Field ",sc)
if(sc === 'edit') {
//console.log("Context",scriptContext.mode);
var currentRec = scriptContext.currentRecord;
var currentRecId = currentRec.id;
console.log("current Record iD", currentRecId);
var ifNumber = currentRec.getValue({
fieldId: 'tranid'
})
console.log( "IF Number", ifNumber);
var status = "ItemShip:" + currentRec.getValue({
fieldId: 'shipstatus'
});
console.log("status", status)
var shipDate = currentRec.getValue({
fieldId: 'custbody_rpod_shipdate'
});
console.log("shipDate", shipDate);
var shippingDate = formatDate(shipDate)
console.log("shipping Date", shippingDate);
var custName = currentRec.getValue({
fieldId: 'entity'
});
console.log("custName", custName)
if((scriptContext.fieldId==='custbody_rpod_shipdate' && status === 'ItemShip:C') || (scriptContext.fieldId==='custbody_trolleynumber' && status === 'ItemShip:C')){
var arrayData = ifDetails(custName, shippingDate, status);
var content1='Please see the below Item fulfillment with its corresponding Trolley Number,'+'<br>';
var content2=''
for(var i=0;i<arrayData.length;i++) {
if (ifNumber != arrayData[i].docNumber) {
content2 = content2 + '<br>' + " Item Fulfillment: " + arrayData[i].docNumber + " ,Trolley Number: " + arrayData[i].trolleyNo
}
}
console.log("content3",content2)
if(content2!==''&& content2!==undefined) {
var options = {
title: "Trolley Number Suggestions",
message: content1 + content2
};
dialog.alert(options);
}
}
else if(scriptContext.fieldId==='shipstatus' && shippingDate !== '' && shippingDate !== null && shippingDate !== undefined && status === 'ItemShip:C' ) {
var arrayData = ifDetails(custName, shippingDate, status);
var content1='Please see the below Item fulfillment with its corresponding Trolley Number,'+'<br>';
var content2 =''
// if(arrayData.length > 0){
for(var i=0;i<arrayData.length;i++) {
if (ifNumber != arrayData[i].docNumber) {
content2 = content2 + '<br>' + " Item Fulfillment: " + arrayData[i].docNumber + " ,Trolley Number: " + arrayData[i].trolleyNo
}
}
console.log("array data ****",arrayData);
console.log("content3",content2)
if(content2!==''&& content2!==undefined) {
var options = {
title: "Trolley Number Suggestions",
message: content1 + content2
};
dialog.alert(options);
}
// }
}
}
}
catch (err) {
console.log(err)
}
}
function pageInit(scriptContext) {
sc=scriptContext.mode;
console.log("context values",sc)
try {
if (sc === 'edit') {
var currentRec = scriptContext.currentRecord;
var currentRecId = currentRec.id;
console.log("current Record", currentRec)
console.log("currentRecId", currentRecId)
var status = "ItemShip:" + currentRec.getValue({
fieldId: 'shipstatus'
});
var shipDate = currentRec.getValue({
fieldId: 'custbody_rpod_shipdate'
});
console.log("shipDate", shipDate);
var shippingDate = formatDate(shipDate)
var custName = currentRec.getValue({
fieldId: 'entity'
});
var ifNumber = currentRec.getValue({
fieldId: 'tranid'
})
var trolleyNumber = currentRec.getValue({fieldId:'custbody_trolleynumber'});
if (status === 'ItemShip:C' && shippingDate !== '' && shippingDate !== null && shippingDate !== undefined) {
var arrayData = ifDetails(custName, shippingDate, status);
console.log("Array Data", arrayData);
var content1='Please see the below Item fulfillment with its corresponding Trolley Number,'+'<br>';
var content2 ='';
for(var i=0;i<arrayData.length;i++) {
if (ifNumber != arrayData[i].docNumber) {
content2 = content2 + '<br>' + " Item Fulfillment: " + arrayData[i].docNumber + " ,Trolley Number: " + arrayData[i].trolleyNo
}
}
// console.log("content",content2)
if(content2!=='' && content2!==undefined) {
var options = {
title: "Trolley Number Suggestions",
message: content1 + content2
};
dialog.alert(options);
}
}
}
}
catch(err){
console.log(err)
}
return true
}
/**
* Validation function to be executed when record is saved.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @returns {boolean} Return true if record is valid
*
* @since 2015.2
*/
function saveRecord(scriptContext) {
return true;
}
return {
pageInit: pageInit,
fieldChanged: fieldChanged,
saveRecord: saveRecord
};
});