How to auto select trans line field sourced from vendor and item - netsuite

I need to add a check in purchase order whether the item has any minimum order qty (moq) set by the vendor. If yes make sure order qty input is more than OR equal to moq if not prompt error to user.
I created a custom record "Item Vendor Setting" to store vendor item moq with below fields
Vendor
Item
Min order qty
And I created a custom transaction line field "Item Vendor id" which is list record from "item vendor setting" with filter Vendor = trans Vendor and item = trans item.
When I create a PO, after enter the Vendor and item fields, the matched ID is not auto selected in the "item vendor setting" field. Do I need any scripting to achieve this ? Can anyone advise any sample code because I can read code but cannot code from scratch :( Thanks !
I have added the client script but not sure what's wrong there is no error prompt and I can't add any line. Can anyone advise ?
/**
*#NApiVersion 2.1
*#NScriptType ClientScript
*#NModuleScope Public
*/
define(['N/currentRecord', 'N/search'],
function (currentRecord, search) {
function validateLine(context) {
var currentRecord = context.currentRecord;
var sublistName = context.sublistId;
if(sublistName === 'item') {
var recsub = currentRecord.getField({ fieldId: 'subsidiary' });
var reclineitem = currentRecord.getCurrentSublistValue({ sublistId: sublistName, fieldId: 'item'});
if (!recsub && !reclineitem) {
var subitemlinksearch = search.create({
type: "customrecord_fc_item_subsi_fields",
filters: [["custrecord_fc_isf_subsidiary", "is", recsub],'and',
["custrecord_fc_isf_item","is",reclineitem]]
});
subitemlinksearch.run().each(function (result) {
var subitemlink = result.getValue({ name: 'id' });
if (subitemlink) {
currentRecord.setCurrentSublistValue({ sublistId: sublistName, fieldId: 'custcol_fc_ir_isf_link', value: subitemlink });
};
return true;
});
};
};
}
return {
validateLine: validateLine
};
});

Your (!) negations if (!recsub && !reclineitem) {...} are incorrect. It should be the opposite.
Here you've a sample of how I would handle it (without the help of the custom field). The code hasn't been tested, so there could be some bugs.
/**
*#NApiVersion 2.1
*#NScriptType ClientScript
*#NModuleScope Public
*/
define(["N/search", "N/ui/message", "N/ui/dialog"], function (search, message, dialog) {
function validateLine(context) {
var currentRecord = context.currentRecord;
var sublistId = context.sublistId;
if (sublistId != 'item') return true;
var subsidiary = currentRecord.getValue({ fieldId: 'subsidiary' });
var vendor = currentRecord.getValue({ fieldId: 'entity' });
var item = currentRecord.getCurrentSublistValue({ sublistId: sublistId, fieldId: 'item' });
var quantity = currentRecord.getCurrentSublistValue({ sublistId: sublistId, fieldId: 'quantity' });
if (!subsidiary) {
message.create({
title: 'Missing Field',
type: message.Type.WARNING,
message: 'Subsidiary is missing...',
}).show({ duration: 5000 });
return false;
}
if (!vendor) {
message.create({
title: 'Missing Field',
type: message.Type.WARNING,
message: 'Vendor is missing...',
}).show({ duration: 5000 });
return false;
}
if (!item) {
message.create({
title: 'Missing Field',
type: message.Type.WARNING,
message: 'Item is missing...',
}).show({ duration: 5000 });
return false;
}
if (!quantity) {
message.create({
title: 'Missing Field',
type: message.Type.WARNING,
message: 'Quantity is missing...',
}).show({ duration: 5000 });
return false;
}
var subitemlinksearch = search.create({
type: "customrecord_fc_item_subsi_fields",
filters: [
["custrecord_fc_isf_subsidiary", "is", subsidiary],
'and',
["custrecord_fc_isf_vendor", "is", vendor], // I don't know how you called this field..
'and',
["custrecord_fc_isf_item", "is", item]
],
columns: ["custrecord_fc_isf_minimun_order_quantity"]
});
var canProceeed = false
var results = 0
subitemlinksearch.run().each(function (result) {
results++;
var moq = result.getValue({ name: 'custrecord_fc_isf_minimun_order_quantity' }); // I don't know how you called this field..
if (+quantity < +moq) {
dialog.confirm({
title: 'Minimum Order Quantity',
message: "The miniminum order quantity is " + moq + ", while you only order " + quantity + ". Do you want to update the quantity?"
}).then(function (confirmed) {
if (!confirmed) return false;
currentRecord.setCurrentSublistValue({ fieldId: 'quantity', sublistId: sublistId, value: moq })
currentRecord.commitLine({ sublistId: sublistId });
});
return false;
}
canProceeed = true
return true;
});
if (!results) return true;
return canProceeed;
}
return {
validateLine: validateLine
};
});

Related

Is there any way to show the records in view mode when clicking the edit when it has fully billed status in Netsuite Suitescript 2.0

function beforeLoad(context){
var Type = context.type;if(Type== context.UserEventType.EDIT && Status=='fullyBilled'){
var form = context.form;
return redirectUrl = url.resolveRecord({
recordType: 'purchaseorder',
isEditMode: false
});
}
}
You should be able to use redirect.toRecord(options) from the N/redirect module.
define(['N/redirect'], function(redirect) {
function beforeLoad(context) {
var newRecord = context.newRecord
var type = context.type
if (type != context.UserEventType.EDIT) return
var status = newRecord.getValue({ fieldId: 'status' })
if (status != "fullyBilled") return
redirect.toRecord({
type: newRecord.type,
id: newRecord.id,
isEditMode: false
})
}
return {
beforeLoad: beforeLoad
}
})

How can I disable line item field without disabling entire column using Client Script (SuiteScript 2.0)?

I'm trying to disable sublist field, in order to fillment of another sublist field. However, it seems to me it's not working.
I used lineInit(cuz it looks like what i need at the moment).
function lineInit(context) {
var cr = context.currentRecord;
var selectedLine = cr.getCurrentSublistIndex({sublistId: "item"});
var iskontoOran = cr.getCurrentSublistValue({sublistId: "item", fieldId : "custcol_delta_iskonto_oran"});
if (iskontoOran != "0.00"){
cr.getCurrentSublistValue({sublistId : "item", fieldId: "custcol_delta_iskonto_tutar", line: 0
}).isDisabled = true;
}
else {
cr.getCurrentSublistValue({sublistId : "item", fieldId: "custcol_delta_iskonto_tutar", line: 0
}).isDisabled = false;
}
function lineInit(context) {
var cr = context.currentRecord;
var iskontoOran = cr.getCurrentSublistValue({sublistId: "item", fieldId : "custcol_delta_iskonto_oran"});
cr.getSublistField({sublistId : "item", fieldId: "custcol_delta_iskonto_tutar"}).isDisabled = iskontoOran != 0;
}
function fieldChanged(context) {
var cr = context.currentRecord;
var sublistName = context.sublistId;
var sublistFieldName = context.fieldId;
if (sublistName === 'item' && sublistFieldName === 'custcol_delta_iskonto_oran') {
var iskontoOran = cr.getCurrentSublistValue({sublistId: "item", fieldId : "custcol_delta_iskonto_oran"});
cr.getSublistField({sublistId : "item", fieldId: "custcol_delta_iskonto_tutar"}).isDisabled = iskontoOran != 0;
}
}
function fieldChanged(context) {
var myRec = context.currentRecord;
var itemCount = myRec.getLineCount("item");
var anlıkLine = myRec.getCurrentSublistIndex({ sublistId: "item" });
var sublistName = myRec.getSublist({sublistId: "item"});
var oranColumn = sublistName.getColumn({ fieldId: "custcol_delta_iskonto_oran" });
var tutarColumn = sublistName.getColumn({ fieldId: "custcol_delta_iskonto_tutar" });
var currOran = myRec.getCurrentSublistValue({ sublistId: "item", fieldId: "custcol_delta_iskonto_oran" });
var currTutar = myRec.getCurrentSublistValue({ sublistId: "item", fieldId: "custcol_delta_iskonto_tutar" });
if (currTutar) {
myRec.setCurrentSublistValue({
sublistId: "item",
fieldId: "custcol_delta_iskonto_oran",
value: "",
ignoreFieldChange: true
});
oranColumn.isDisabled = true;
} else {
oranColumn.isDisabled = false;
}
if (currOran) {
myRec.setCurrentSublistValue({
sublistId: "item",
fieldId: "custcol_delta_iskonto_tutar",
value: "",
ignoreFieldChange: true
});
tutarColumn.isDisabled = true;
} else {
tutarColumn.isDisabled = false;
}
}
function lineInit(context) {
var myRec = context.currentRecord;
var itemCount = myRec.getLineCount("item");
var anlıkLine = myRec.getCurrentSublistIndex({ sublistId: "item" });
var quantity = myRec.getCurrentSublistValue({ sublistId: "item", fieldId: "quantity" });
log.debug({ title: "AnlıkLine, Quantity ve itemCount-LineInit", details: "anlıkLine = " + anlıkLine + ", quantity = " + quantity + ", itemcount = " + itemCount });
for (var j = 0; j < itemCount; j++) {
if (quantity){
var oranField = myRec.getSublistField({ sublistId: "item", fieldId: "custcol_delta_iskonto_oran", line: j });
var tutarField = myRec.getSublistField({ sublistId: "item", fieldId: "custcol_delta_iskonto_tutar", line: j });
var currOran = myRec.getCurrentSublistValue({ sublistId: "item", fieldId: "custcol_delta_iskonto_oran" });
var currTutar = myRec.getCurrentSublistValue({ sublistId: "item", fieldId: "custcol_delta_iskonto_tutar" });
if (currTutar) {
myRec.setCurrentSublistValue({
sublistId: "item",
fieldId: "custcol_delta_iskonto_oran",
value: "",
ignoreFieldChange: true
});
oranField.isDisabled = true;
} else {
oranField.isDisabled = false;
}
if (currOran) {
myRec.setCurrentSublistValue({
sublistId: "item",
fieldId: "custcol_delta_iskonto_tutar",
value: "",
ignoreFieldChange: true
});
tutarField.isDisabled = true;
log.debug({ title: "Tutar Alanı
} else {
tutarField.isDisabled = false;
}
}
}
}
So this is my aproach and it is working fine at the moment. Can it be more nice? Yeah, probably but it does do the job for now :)

Inventory Details Subrecord from Client script

Is there any way to get and set serials to the inventory details in client script?
My logic is that I have a text area field, I want to set there serials and then on field change I want to set the serials into the inventory details
/**
*#NApiVersion 2.x
*#NScriptType ClientScript
*/
define([], function () {
function fieldChanged(context) {
var currRec = context.currentRecord;
var sublistName = context.sublistId;
var sublistFieldName = context.fieldId;
var line = context.line;
if (sublistName === 'item' && sublistFieldName === 'custcol3') {
var testSerials = currRec.getSublistValue('item', 'custcol3', line);
testSerials = testSerials.split('\u0005');
console.log(testSerials);
var qty = testSerials.length;
currRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity',
value: qty,
ignoreFieldChange: true,
forceSyncSourcing: true
});
var invDet = currRec.getSublistSubrecord('item', 'inventorydetail', line);
for (var i = 0; i < testSerials.length; i++) {
invDet.setSublistValue('inventoryassignment', 'receiptinventorynumber', i, testSerials[i]);
invDet.setSublistValue('inventoryassignment', 'quantity', i, 1);
}
}
}
return {
fieldChanged: fieldChanged,
}
});
The error that I get is 'currRec.getSublistSubrecord is not a function'
In a client script you will generally be dealing with currRec.getCurrentSublistSubrecord
e.g.
require(['N/currentRecord', 'N/record'], function(currRec, record) {
var rec = currRec.get();
rec.selectLine({
sublistId: 'inventory',
line: 1
});
var detail = rec.getCurrentSublistSubrecord({
sublistId: 'inventory',
fieldId: 'inventorydetail'
});
if(!detail) {
console.log('no current detail');
detail = record.create({type:'inventorydetail', isDynamic:true, defaultvalues:{
item: rec.getCurrentSublistValue({
sublistId:'inventory',
fieldId:'item'
})}});
rec.setCurrentSublistValue({
sublistId:'inventory',
fieldId:'inventorydetail',
value:detail
});
}
console.log(detail.getValue({
fieldId: 'item'
}));
detail.selectNewLine({sublistId:'inventoryassignment'});
detail.setCurrentSublistText({
sublistId:'inventoryassignment',
fieldId: 'binnumber',
value:'A-22'
});
});

SuiteScriptError INVALID_FLD_VALUE Value -1.0 outside of valid min/max range for field adjustqtyby

Trying to create an inventory adjustment to make all items available in the location to zero. While setting the negative value in the adjustqty field it says invalid field value.
Error message
"type":"error.SuiteScriptError","name":"INVALID_FLD_VALUE","message":"Value -1.0 outside of valid min/max range for field adjustqtyby"
code
let createInventoryAdjustment = (siteId)=> {
try {
var inventoryAdjustmentRecord = record.create({
type: record.Type.INVENTORY_TRANSFER,
isDynamic: true
});
inventoryAdjustmentRecord.setValue("account",accountId)
let index = 0;
let currentInventoryData = getSerielNumbers(siteId);
let itemVsSerialNo = currentInventoryData.results;
let itemsVsQty = currentInventoryData.itemsVsQty;
for (var key in itemVsSerialNo) {
log.debug("itemVsSerialNo[key]",itemVsSerialNo[key]);
var inveRec = inventoryAdjustmentRecord.selectNewLine({
sublistId : 'inventory'
});
inventoryAdjustmentRecord.setCurrentSublistValue({
fieldId: "item",
value: key,
line: index,
sublistId: 'inventory'
});
inventoryAdjustmentRecord.setCurrentSublistValue({
fieldId: "location",
value: siteId,
line: index,
sublistId: 'inventory'
});
log.debug(itemsVsQty[key],itemVsSerialNo[key]);
inventoryAdjustmentRecord.setCurrentSublistValue({
fieldId: "adjustqtyby",
value: parseInt(itemsVsQty[key]*-1),
line: index,
sublistId: 'inventory'
});
if(itemVsSerialNo[key][0].type!="inventoryitem") {
let inventoryDetail = inventoryAdjustmentRecord.getCurrentSublistSubrecord({
sublistId: 'inventory',
fieldId: 'inventorydetail',
line: index
});
for (let i=0;i<itemVsSerialNo[key].length;i++) {
let inventoryAssignment = inventoryDetail.selectNewLine({
sublistId : 'inventoryassignment'
});
inventoryAssignment.setCurrentSublistValue({
sublistId: 'inventoryassignment',
fieldId: 'quantity',
value: (itemVsSerialNo[key][i].qtyOnhand*-1),
line: i
});
inventoryAssignment.setCurrentSublistValue({
sublistId: 'inventoryassignment',
fieldId: 'issueinventorynumber',
value: itemVsSerialNo[key][i].inventoryNoId,
line: i
});
inventoryAssignment.commitLine({
sublistId : 'inventoryassignment'
});
}
}
index++;
inventoryAdjustmentRecord.commitLine({
sublistId : 'inventory'
});
log.debug("key",key);
//break;
}
if (index == 0) {
// NO items to reverse
return {
isCreated: true,
tranId: null,
};
}
let tranId = inventoryAdjustmentRecord.save({ignoreMandatoryFields: true});
log.debug("inventoryAdjustmentRecord",tranId);
return {
isCreated: true,
tranId: tranId
};
} catch (err) {
log.debug('error#inventoryAdjustment', err);
return {
isCreated: false,
error: 'Unable to create Return Inventory Transfer'
}
}
}
you are creating an inventory transfer which cannot transfer negative qty, instead of:
var inventoryAdjustmentRecord = record.create({
type: record.Type.INVENTORY_TRANSFER,
isDynamic: true
});
you should have:
var inventoryAdjustmentRecord = record.create({
type: record.Type.INVENTORY_ADJUSTMENT,
isDynamic: true
});

How to check if there is any changes in line items on after submit?

I have Po.
I want to check if the item lines have changed on after submit
function onAction(context) {
var newRec = context.newRecord;
var oldRec = context.oldRecord;
var newCount = newRec.getLineCount({
sublistId: 'item'
});
var oldCount = oldRec.getLineCount({
sublistId: 'item'
});
if (newCount != oldCount) {
log.debug('result', 'true')
} else {
log.debug('result', 'false')
}
}
return {
onAction: onAction
}
I did it with count line but I want also to check if there any changes in the line and not only the number of the lines
You would need to compare each field you care about on each line. Here's an example function that returns true/false if any lines were changed by comparing line count, then item, quantity and rate for each line. You can modify it to meet your needs.
Using some() this way, the first field on the first line that is different will stop the comparison and return true.
function linesChanged(context) {
const fields = ['item', 'quantity', 'rate'];
const oldRecord = context.oldRecord;
const newRecord = context.newRecord;
const oldLineCount = oldRecord.getLineCount({ sublistId: 'item' });
const newLineCount = newRecord.getLineCount({ sublistId: 'item' });
if (oldLineCount !== newLineCount) {
return true;
}
for (let i = 0; i < newLineCount; i++) {
var lineChanged = fields.some(field => {
const oldValue = oldRecord.getSublistValue({ sublistId: 'item', fieldId: field, line: i });
const newValue = newRecord.getSublistValue({ sublistId: 'item', fieldId: field, line: i });
return oldValue !== newValue;
});
if (lineChanged) {
return true;
}
}
return false;
}

Resources