Description of ABSI SO ShipDate User Event Script in Studco

Script ID: customscript_absi_so_shipdate_ue

Environment : PRODUCTION

This script is written in suitescript 1.0.  

It will work in create, edit and delete contexts. The script will trigger when the sales order record submit. 

We will get the subsidiary of the current record. First we will check the subsidiary is not equal to “Studco Australia”.  

  1. When the context is delete, we will get the corresponding work order details from the sales order. Then get the ship date from the sales order and set this value to the work order production end date.  
  1. When the context is either Create/ Edit  get the subsidiary value from the sales order. If the subsidiary is either Studco Overarching or Studco- United States , get the sales order ID, document and the status values. If the sales order status is either Pending Approval and Pending Fulfillment get the line item counts from the sales order and update the  remaining quantity, remaining weight and total remaining weight line level fields. 

Script:

/**
* To update the ExpectedShipDate field on WO from SO, when SO not created in the UI
*
* Version Date Author Remarks
* 1.00 08 Jul 2016 Shanthi Vedulla
*
* Calculate and update Qty Remaining and Remaining Weight columns and Total Remaining Weight - 03/12/2020
* Exclude Script for UK as well along with AU - 3/30/2020
* Add code to fix the issue with 'Original Qty' field on SO. - 07/13/2022
*/
var absi_subsidiary = nlapiGetSubsidiary(); //Disregard AU subsidiary(7)
function userEventBeforeSubmit(type){

if (absi_subsidiary != 7)
{
try {
if (type != 'delete') {
var shipDate = nlapiGetFieldValue('shipdate');
var lineCount = nlapiGetLineItemCount('item');
if ((shipDate.length != 0) && (lineCount > 0)){
for (var i = 1; i <= lineCount; i++){
nlapiSelectLineItem('item', i);
nlapiSetLineItemValue('item', 'expectedshipdate', i, shipDate);// Only on UE and NEW records
nlapiCommitLineItem('item');
}
}
}
}
catch (e) {
var logMessage = 'Failed to update ExpectedShipDate in SalesOrder Line Items' + '\n' + processError(e);
nlapiLogExecution('ERROR','Failed to update ExpectedShipDate', logMessage);
}
}
}

function userEventAfterSubmit(type) {

var soID = nlapiGetRecordId();
nlapiLogExecution('debug','soID', soID);
if (absi_subsidiary != 7) {
if (type != 'delete') {
var woID, prodEndDate;
//var soID = nlapiGetRecordId();
try {
var searchResults = nlapiSearchRecord('workorder', null, new nlobjSearchFilter('createdfrom', null, 'anyof', soID));
if (searchResults){
var len = searchResults.length;
nlapiLogExecution('debug','len', len);
if (len > 0){
for(var i=0; i<=len; i++) {
woID = searchResults[i].getId();
prodEndDate = nlapiLookupField('salesorder', soID, 'shipdate');
nlapiSubmitField('workorder', woID, 'enddate', prodEndDate);
}
}
}
}
catch (e) {
var logMessage = 'Failed to update ProductionEndDate on the Work Order: ' + woID + '\n' + processError(e);
nlapiLogExecution('ERROR','Failed to update ProductionEndDate', logMessage);
}
}
}

/*Calculate and update Qty Remaining and Remaining Weight columns and Total Remaining Weight
* 03/12/2020
*/

if (type == 'create' || type == 'edit') {

try {
var subsid = nlapiLookupField('salesorder', soID, 'subsidiary');
nlapiLogExecution('debug','subsid', subsid);

if (subsid == '1' || subsid == '2') {

var recSO = nlapiLoadRecord('salesorder',soID);
var docNum = recSO.getFieldValue('tranid');
var orderStatus = recSO.getFieldValue('orderstatus');
nlapiLogExecution('debug','docNum', docNum);
nlapiLogExecution('debug','orderStatus', orderStatus);

if (orderStatus=='A' || orderStatus=='B') {
var lineCnt = recSO.getLineItemCount('item');
var totRemWeight = 0;
for (j=1; j<=lineCnt; j++) {
recSO.setLineItemValue('item', 'custcol_absi_qty_remaining', j, parseInt(Math.round(recSO.getLineItemValue('item', 'custcol_no_of_pieces', j))));
recSO.setLineItemValue('item', 'custcol_absi_remaining_weight', j, parseInt(Math.round(recSO.getLineItemValue('item', 'custcol_total_tran_line_weight', j))));
totRemWeight+=parseInt(Math.round(recSO.getLineItemValue('item', 'custcol_total_tran_line_weight', j)));
recSO.setLineItemValue('item', 'custcol_original_qty_ordered', j, recSO.getLineItemValue('item', 'custcol_no_of_pieces', j));
//nlapiLogExecution('debug','j',j);
//nlapiLogExecution('debug','lineWeight', parseInt(Math.round(recSO.getLineItemValue('item', 'custcol_total_tran_line_weight', j))));
}
nlapiLogExecution('debug','totRemWeight', totRemWeight);
recSO.setFieldValue('custbody_absi_total_remain_weight', totRemWeight);
nlapiSubmitRecord(recSO);
}
}
}
catch (e) {
var logMessage = 'Sales Order. Id: ' + soID + '\n' + processError(e);
nlapiLogExecution('ERROR','Failed to update Remaining Qty, Remaining Weight and Total Remaining Weight', logMessage);
}
}
}

function processError(error){
if(error instanceof nlobjError){
return error.getCode() + '\n' + error.getDetails();
}else{
return error.toString();
}
}

Leave a comment

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