Script ID:customscript_absi_set_fields
Environment: PRODUCTION
This script is written in suitescript 1.0.
The script is used to update the Part no field with the Over ride part number value. The script will trigger before submitting the sales order record. The script will not consider the sales orders that having the subsidiary value “Studco Australia”. Get the text value from the custom transaction line field “Override part#” from every line lines of items. If this field do not contain any values or if it contains null value get the item id from the corresponding item record and and set this value as the transaction line custom field “Part No”.
Script:
/**
* Module Description
*
* Version Date Author Remarks
* 1.00 31 May 2016 Reza Seedin
*
*/
var AU_SUBSIDIARY = StudAuUtils.getAuSubsidiary(); // STUDCO AUS
var AU_CONSIGN_LOC = StudAuUtils.getAuConsignmentLocation();
// var AU_JOURNAL_FORM = '254'; // 254 AU Journal Entry
var AU_ACCOUNT_PPV = StudAuUtils.getAuAccPPV(); // '775' // 906 NS- Purchase Price Variance
var AU_ACC_INV_REC_NT_BILL = StudAuUtils.getAuAccInvRcvNotBilled(); //'115' // 153 NS-Inventory Received Not Billed
var woForm = StudAuUtils.getAuWoForm(); // AU Work Order
var auPoSlittingForm = StudAuUtils.getAuPoSlittingForm(); // AU Purchase Order - Slitting
var slittingServiceId = StudAuUtils.getAuSlitCoilService(); // SLIT-COIL - Service for Purchase
var AU_PROD_DEPT = StudAuUtils.getAuDepartmentProduction();
var AU_BRANCH = StudAuUtils.getAuBranch();
var AU_INV_TRANS_FORM = StudAuUtils.getAuInventoryTranferForm();
//var location = '18'; // STUDCO MELBOURNE
var TIMEZONE = 'Australia/Melbourne'; // DEFAULT
var DATETIME_FORMAT = 'DD/MM/YYYY hh:mm:ss a';
var DATE_FORMAT = 'DD/MM/YYYY';
var TIME_FORMAT = 'h:mm a';
var AU_TRANSFER_ORD_FORM = StudAuUtils.getAuTransferOrderForm();
/**
* The recordType (internal id) corresponds to the "Applied To" record in your script deployment.
* @purchaseorder recordType
*
* @param {String} type Operation types: create, edit, delete, xedit,
* approve, cancel, reject (SO, ER, Time Bill, PO & RMA only)
* pack, ship (IF only)
* dropship, specialorder, orderitems (PO only)
* paybills (vendor payments)
* @returns {Void}
*/
function purchOrderAfterSubmit(type){
nlapiLogExecution('DEBUG', ' purchOrderAfterSubmit', 'START' );
try {
if (type=='create' || type=='edit') {
var recType = nlapiGetRecordType();
var recId = nlapiGetRecordId();
var rec = nlapiLoadRecord(recType, recId);
var tranOrdOldID = '';
var tranOrdNewID = '';
var transRec = '';
var lineRec = {};
var spclInst = '';
var isSlitItem = false;
var oldRecord = nlapiGetOldRecord();
var count = rec.getLineItemCount('item');
var formId = rec.getFieldValue('customform')
nlapiLogExecution('DEBUG', 'formId', formId );
// slitting PO
if (formId == auPoSlittingForm) {
var isCreateWO = rec.getFieldValue('custbody_au_create_wo_slit_coils');
nlapiLogExecution('DEBUG', 'isCreateWO', isCreateWO );
// var mcTotalQty = getMcTotalQty(recType, recId);
var vendorLocation = rec.getFieldValue('custbody_au_vendor_location');
var pricePerTonne = rec.getFieldValue('custbody_au_slit_cost_per_tonne');
// var pricePerKg = '';
// if (ObjectUtils.isNotEmpty(pricePerTonne)) {
// pricePerKg = parseFloat(pricePerTonne / 1000);
// }
var totMCWeight = rec.getFieldValue('custbody_au_total_mass');
var totMCWidth = rec.getFieldValue('custbody_au_mother_coil_width');
var poDate = rec.getFieldValue('trandate');
for (var x = 1; x <= count; x++) {
nlapiLogExecution('DEBUG', 'Line', x );
lineRec.item = rec.getLineItemValue('item', 'item', x);
lineRec.consigned = rec.getLineItemValue('item','custcol_au_consigment', x);
lineRec.mcLocation = rec.getLineItemValue('item','custcol_au_mcnum_location', x);
lineRec.scLinExptQty = rec.getLineItemValue('item','custcol_au_expected_quantity', x);
lineRec.scLinWidth = rec.getLineItemText('item', 'custcol_au_width', x);
if (lineRec.item == slittingServiceId) {
isSlitItem = true;
}
lineRec.woId = rec.getLineItemValue('item', 'custcol_au_work_order', x);
lineRec.McCoilId = rec.getLineItemValue('item', 'custcol_au_mc_coil_number', x);
lineRec.McCoilLot = rec.getLineItemText('item', 'custcol_au_mc_coil_number', x);
lineRec.coilType = nlapiLookupField('item', lineRec.item, 'custitem_au_item_coil_type', true);
if (lineRec.coilType == 'Mother Coil' && isCreateWO == 'T' && ObjectUtils.isNotEmpty(lineRec.McCoilLot)) {
lineRec.McLocation = LotUtils.getLocation(lineRec.McCoilLot);
if (lineRec.McLocation == AU_CONSIGN_LOC) {
lineRec.mcTransferId = moveConsignedCoilToLocation(lineRec.McCoilLot, vendorLocation);
lineRec.LotId = LotUtils.getLotId(lineRec.McCoilLot);
if (ObjectUtils.isNotEmpty(lineRec.mcTransferId)) {
//update Lot details
nlapiSubmitField('inventorynumber', lineRec.LotId,
['custitemnumber_au_date_unconsigned','custitemnumber_au_consignment'],
[poDate,'F']);
mcTransferJournalEntry(lineRec.mcTransferId);
}
}
}
if (lineRec.coilType == 'Daughter Coil') {
if (ObjectUtils.isEmpty(spclInst)) {
nlapiLogExecution('DEBUG', 'FIRST TIME spclInst', spclInst);
spclInst = 'SLIT: ' + lineRec.scLinExptQty + ' @ ' + lineRec.scLinWidth;
} else {
nlapiLogExecution('DEBUG', 'spclInst', spclInst);
spclInst = spclInst + ', ' + lineRec.scLinExptQty + ' @ ' + lineRec.scLinWidth;
}
rec.setLineItemValue('item', 'quantity', x, '0');
nlapiLogExecution('DEBUG', 'lineRec', JSON.stringify(lineRec) );
var totMCWeightKG = totMCWeight * 1000;
lineRec.qty = Math.round((lineRec.scLinWidth / totMCWidth) * totMCWeightKG * lineRec.scLinExptQty);
nlapiLogExecution('DEBUG', 'lineRec.qty', lineRec.qty );
rec.setLineItemValue('item', 'custcol_coil_quantity_expected', x, lineRec.qty);
}
// nlapiLogExecution('DEBUG', 'lineRec', JSON.stringify(lineRec) );
if (isCreateWO == 'T' && lineRec.coilType == 'Daughter Coil' && ObjectUtils.isEmpty(lineRec.woId)) {
lineRec.woId = createWorkOrder(recId, lineRec.item, lineRec.qty , vendorLocation);
nlapiLogExecution('DEBUG', 'Work Order Created woId: ', lineRec.woId + ' - Line :' + x);
rec.setLineItemValue('item', 'custcol_au_work_order', x, lineRec.woId);
}else if (isCreateWO == 'T' && lineRec.coilType == 'Mother Coil' && ObjectUtils.isNotEmpty(lineRec.McCoilId)){
nlapiLogExecution('DEBUG', 'UPDATE Mother Coil REC as used in PO', 'UPDATE Mother Coil REC as used in PO' );
try {
nlapiSubmitField('customrecord_au_mcnum', lineRec.McCoilId, 'custrecord_au_mcnum_booked', recId);
} catch (e) {
var errorMessage = 'Error updating MC coil with PO number';
nlapiLogExecution('DEBUG','purchOrderAfterSubmit - ' + errorMessage, e);
}
} // end IF is createWO
} // end LOOP
rec.setFieldValue('custbody_au_special_instructions', spclInst);
if (ObjectUtils.isNotEmpty(totMCWeight) && ObjectUtils.isNotEmpty(pricePerTonne)) {
// var slitServiceRate = (totMCWeight / 1000) * pricePerTonne;
// nlapiLogExecution('DEBUG', 'rec.slitServiceRate', slitServiceRate );
if (!isSlitItem) {
nlapiLogExecution('DEBUG', 'AND SLITTING SERVICE' );
// nlapiLogExecution('DEBUG', 'SLIT COIL ExITSS');
rec.selectNewLineItem('item');
rec.setCurrentLineItemValue('item', 'item', slittingServiceId);
rec.setCurrentLineItemValue('item', 'quantity', totMCWeight );
rec.setCurrentLineItemValue('item', 'rate', pricePerTonne);
rec.commitLineItem('item');
// nlapiLogExecution('DEBUG', 'pricePerTonne', pricePerTonne );
}
}
try {
nlapiSubmitRecord(rec);
} catch (e) {
var errorMessage = 'Error updating PO record ';
nlapiLogExecution('DEBUG', 'purchOrderAfterSubmit', errorMessage + e);
}
}//end iF
// transfer order form
if (formId == AU_TRANSFER_ORD_FORM) {
if (count > 0) {
for (var x = 1; x <= count; x++) {
tranOrdOldID = oldRecord.getLineItemValue('item', 'custcol_au_transfer_order', x);
tranOrdNewID = rec.getLineItemValue('item', 'custcol_au_transfer_order', x);
if (tranOrdOldID != tranOrdNewID) {
nlapiLogExecution('DEBUG', ' tranOrdOld', tranOrdOldID );
nlapiLogExecution('DEBUG', ' tranOrdNew', tranOrdNewID );
updateTransferOrder(tranOrdOldID, tranOrdNewID, recId );
} // END IF
} // end forloop
} // end if count > 0
} // end if
} // end if create or edit
} catch (e) {
var errorMessage = 'Error ';
nlapiLogExecution('DEBUG','purchOrderAfterSubmit - ' + errorMessage, e);
}
}
function updateTransferOrder(tranOrdOldID, tranOrdNewID, poId )
{
try {
nlapiLogExecution('DEBUG', 'updateTransferOrder', 'START' );
var updateNewRec = '';
var updateOldRec = '';
var addNewOrder = false;
var removeOldOrder = false;
var transRec = '';
if (ObjectUtils.isNotEmpty(tranOrdNewID) && ObjectUtils.isEmpty(tranOrdOldID)) {
// New Transfer no added and previous was blank
addNewOrder = true;
}else{
//removed transfer order
removeOldOrder = true;
}
// Transfer order number changed
if (ObjectUtils.isNotEmpty(tranOrdNewID) && ObjectUtils.isNotEmpty(tranOrdOldID)) {
addNewOrder = true;
removeOldOrder = true;
}
if (addNewOrder) {
transRec = nlapiLoadRecord('transferorder', tranOrdNewID);
transRec.setFieldValue('custbody_au_paired_purchase_order', poId);
updateNewRec = nlapiSubmitRecord(transRec);
nlapiLogExecution('DEBUG', 'set PO updateNewRec', updateNewRec );
}
if (removeOldOrder) {
transRec = nlapiLoadRecord('transferorder', tranOrdOldID);
transRec.setFieldValue('custbody_au_paired_purchase_order', '');
updateOldRec = nlapiSubmitRecord(transRec);
nlapiLogExecution('DEBUG', 'REMOVE PO updateOldRec', updateOldRec );
}
} catch (e) {
var errorMessage = 'Error updateTransferOrder';
nlapiLogExecution('DEBUG', errorMessage, e);
}
}
function createWorkOrder(recId, assemblyItem, qty ,location)
{
try {
nlapiLogExecution('DEBUG', 'createWorkOrder', 'START' );
var woRec = nlapiCreateRecord('workorder',
{
'customform': woForm,
'subsidiary': AU_SUBSIDIARY
});
// woRec.setFieldValue('customform' , woForm);
// woRec.setFieldValue('subsidiary' , AU_SUBSIDIARY);
woRec.setFieldValue('location' , location);
woRec.setFieldValue('assemblyitem' , assemblyItem);
woRec.setFieldValue('quantity' , qty);
woRec.setFieldValue('custbody_au_created_from', recId);
// woRec.setFieldValue('custbody_cseg_au_branch' , AU_BRANCH);
try {
var woId = nlapiSubmitRecord(woRec, true);
// nlapiLogExecution('DEBUG', 'woId', woId );
return woId;
} catch (e) {
var errorMessage = 'Error creating WO';
nlapiLogExecution('DEBUG','createWorkOrder - ' + errorMessage, e);
}
} catch (e) {
var errorMessage = 'Error ';
nlapiLogExecution('DEBUG', createWorkOrder, e);
}
}
function moveConsignedCoilToLocation(lotNo, toLocation) {
try {
nlapiLogExecution('DEBUG', 'moveConsignedCoilToLocation', 'START');
var filters = [];
filters.push(new nlobjSearchFilter('inventorynumber', null, 'is', lotNo));
filters.push(new nlobjSearchFilter('quantityavailable', null, 'greaterthan', '0'));
var columns = [];
columns.push(new nlobjSearchColumn('item'));
columns.push(new nlobjSearchColumn('inventorynumber'));
columns.push(new nlobjSearchColumn('location'));
columns.push(new nlobjSearchColumn('quantityavailable'));
var searchRec = nlapiSearchRecord('inventorynumber', null, filters, columns);
// nlapiLogExecution('DEBUG', 'searchRec', JSON.stringify(searchRec));
var currlocation = null;
var qty = null;
var item = null;
var lotId = null;
if (ObjectUtils.isNotEmpty(searchRec)) {
currlocation = searchRec[0].getValue('location');
qty = searchRec[0].getValue('quantityavailable');
item = searchRec[0].getValue('item');
lotId = searchRec[0].getId();
// nlapiLogExecution('DEBUG', 'qty', qty);
var rec = nlapiCreateRecord('inventorytransfer');
rec.setFieldValue('customform' , AU_INV_TRANS_FORM);
rec.setFieldValue('subsidiary' , AU_SUBSIDIARY);
rec.setFieldValue('location' , currlocation); // FROM LOCATION
rec.setFieldValue('transferlocation', toLocation); // TO LOCATION
rec.setFieldValue('custbody_cseg_au_branch' , AU_BRANCH);
rec.selectNewLineItem('inventory');
rec.setCurrentLineItemValue('inventory', 'item', item);
rec.setCurrentLineItemValue('inventory', 'adjustqtyby', qty);
invDetail = rec.createCurrentLineItemSubrecord('inventory', 'inventorydetail');
invDetail.selectNewLineItem("inventoryassignment");
invDetail.setCurrentLineItemValue("inventoryassignment", "issueinventorynumber", lotId);
invDetail.setCurrentLineItemValue("inventoryassignment", "quantity", qty);
invDetail.commitLineItem("inventoryassignment");
invDetail.commit();
rec.commitLineItem('inventory');
try {
// ---- Post Inv Transfer
var invTransferId = nlapiSubmitRecord(rec);
nlapiLogExecution('DEBUG', 'invTransferId', invTransferId);
var dateAu = moment().tz(TIMEZONE).format(DATE_FORMAT);
return invTransferId;
} catch (e) {
var errorMessage = 'Error submitting INV transfer ';
nlapiLogExecution('DEBUG', 'onAfterSubmit - ' + errorMessage, e);
}
}
} catch (e) {
var errorMessage = 'Error in moveConsignedCoilToLocation';
nlapiLogExecution('DEBUG', errorMessage, e);
}
}
function mcTransferJournalEntry(transferId )
{
try {
nlapiLogExecution('DEBUG', 'mcTransferJournalEntry', 'START' );
var journal = nlapiCreateRecord('journalentry', { recordmode: 'dynamic'});
var journalType = '1';
// var transferId = '1140283';
var amount = getTransactionGLAmount(transferId);
journal.setFieldValue('subsidiary', AU_SUBSIDIARY);
journal.setFieldValue('custbody_au_journal_type', '1');
journal.setFieldValue('custbody_au_it_transaction', transferId);
journal.setFieldValue('custbody_cseg_au_branch' , AU_BRANCH);
journal.selectNewLineItem('line');
journal.setCurrentLineItemValue('line', 'account', AU_ACCOUNT_PPV);
journal.setCurrentLineItemValue('line', 'debit', amount );
journal.commitLineItem('line');
journal.selectNewLineItem('line');
journal.setCurrentLineItemValue('line', 'account', AU_ACC_INV_REC_NT_BILL);
journal.setCurrentLineItemValue('line', 'credit', amount );
journal.commitLineItem('line');
return nlapiSubmitRecord(journal);
} catch (e) {
var errorMessage = 'Error in mcTransferJournalEntry';
nlapiLogExecution('DEBUG', errorMessage, e);
}
}
function getTransactionGLAmount(transId)
{
try {
nlapiLogExecution('DEBUG', 'getTransactionGLAmount', 'START' );
var filters = [];
filters.push(new nlobjSearchFilter('internalid', null ,'is', transId));
filters.push(new nlobjSearchFilter('amount', null ,'greaterthan', '0'));
var columns = [];
columns.push(new nlobjSearchColumn('fxamount'));
var searchRec = nlapiSearchRecord('transaction', null, filters,columns);
nlapiLogExecution('DEBUG', 'searchRec', JSON.stringify(searchRec) );
if (ObjectUtils.isNotEmpty(searchRec)) {
return searchRec[0].getValue('fxamount');
}
} catch (e) {
var errorMessage = 'Error in getTransactionGLAmount';
nlapiLogExecution('DEBUG', errorMessage, e);
}
}