Adding if statement to a Netsuite Script - netsuite

I am wanting to display lot numbers on item fulfilment and invoices.
I don't have any scripting ability... sadly...
So I implemented this script and it works really well.
https://jcurvesolutions1.zendesk.com/hc/en-us/articles/4711971574425-Remove-Bin-Number-from-item-inventorydetail-When-Printing-with-Advanced-PDF-Templates
/**
* #NApiVersion 2.x
* #NScriptType UserEventScript
* #NModuleScope SameAccount
*/
define(['N/record'],
function(record) {
function beforeSubmit(scriptContext) {
var rec = scriptContext.newRecord;
var lines = rec.getLineCount({sublistId:'item'});
var tempS = '';
for(var i=0;i<lines;i++){ // for each line
var subList = rec.getSublistSubrecord({
sublistId:'item',
fieldId:'inventorydetail',
line:i
});
if(subList.getValue('id')){ //if the line has inventory details
var sublistLines = subList.getLineCount({
sublistId:'inventoryassignment'
});
for(var j=0;j<sublistLines;j++){ //for each line of the inventory details
tempS+=subList.getSublistText({
sublistId:'inventoryassignment',
fieldId:'issueinventorynumber',
line:j
});
if(j<sublistLines-1) tempS+='\n';
}
rec.setSublistValue({
sublistId:'item',
fieldId:'custcol_tb_lot_no_via_js',
line:i,value:tempS
});
tempS='';
}
}
}
return {
beforeSubmit: beforeSubmit
};
});
However I have one issues with it.
In our Netsuite account I have it set up so that Invoices display items that are on Backorder.
However backorder items don't have "inventory detail" so when I try and edit or save the invoice I get the following error.
{"type":"error.SuiteScriptError","name":"USER_ERROR","message":"Invalid number (must be positive)","stack":["anonymous(N/serverRecordService)","beforeSubmit(/SuiteScripts/DisplayLot# Packing Slip.js:16)"],"cause":{"type":"internal error","code":"USER_ERROR","details":"Invalid number (must be positive)","userEvent":"beforesubmit","stackTrace":["anonymous(N/serverRecordService)","beforeSubmit(/SuiteScripts/DisplayLot# Packing Slip.js:16)"],"notifyOff":false},"id":"","notifyOff":false,"userFacing":false}
It is due to one of the lines not having any inventory detail as it is on back order.
so what I am asking is if some kind person could tell me what I need to change in the above script so that it ignores backorder items.
Thanks.

To ignore backordered line items, you must get the back ordered quantity per line item. In my UI I see the field id "backordered", however in the NS records browser the field id is "quantityremaining". See which works for you.
var backOrderedQty = rec.getSublistValue({sublistId: 'item', fieldId: 'backordered', line: i}); //or try field id quantityremaining
then evaluate if it is equal to 0. If it is, then do not proceed with code for this line. If it is, then do proceed. Example:
if(backOrderedQty == 0) {
//do nothing
} else {
//do something
}
In your code, you already have a check for if the line has inventory details. So we can add this check for back ordered quantity to the same if statement and we do not need an else statement, since we want to "do nothing".
Full code, with modified lines not indented:
/**
* #NApiVersion 2.x
* #NScriptType UserEventScript
* #NModuleScope SameAccount
*/
define(['N/record'],
function(record) {
function beforeSubmit(scriptContext) {
var rec = scriptContext.newRecord;
var lines = rec.getLineCount({sublistId:'item'});
var tempS = '';
for(var i=0;i<lines;i++){ // for each line
//get the current lines backordered qty
var backOrderedQty = rec.getSublistValue({
sublistId: 'item',
fieldId: 'backordered', //or quantityremaining
line: i
});
var subList = rec.getSublistSubrecord({
sublistId:'item',
fieldId:'inventorydetail',
line:i
});
if(subList.getValue('id') && backOrderedQty ==0){ //if the line has inventory details AND back ordered qty is 0
var sublistLines = subList.getLineCount({
sublistId:'inventoryassignment'
});
for(var j=0;j<sublistLines;j++){ //for each line of the inventory details
tempS+=subList.getSublistText({
sublistId:'inventoryassignment',
fieldId:'issueinventorynumber',
line:j
});
if(j<sublistLines-1) tempS+='\n';
}
rec.setSublistValue({
sublistId:'item',
fieldId:'custcol_tb_lot_no_via_js',
line:i,value:tempS
});
tempS='';
}
}
}
return {
beforeSubmit: beforeSubmit
};
});

Related

On an afterSubmit when we creating a copy of one inventory item (name with '-c') ,The original ID of item link should come in a field on a copy order

I tried this above, here I am getting a null value only from my previous record.
Kindly give some guidance to solve my questions.
thanks in advance.
/**
*#NApiVersion 2.0
*#NScriptType UserEventScript
*/
define(["N/url", "N/record", "N/runtime"], function (url, record, runtime) {
function afterSubmit(context){
var recordobj = context.newRecord;
var prevItemrecord= context.oldRecord;
var Itemname = recordobj.getValue({fieldId:'itemid'});
var prevItemname = prevItemrecord.getValue({fieldId : 'itemid'});
var Type=context.type;
var checkbox=recordobj.getValue({fieldId:'custitem17'});
if(Type== context.UserEventType.CREATE)
if((Itemname=prevItemname+'-c')&&(checkbox=true))
record.submitFields({
type: recordobj.type,
id: recordobj.id,
values:{custitem_item_link:prevItemname}
});
}
return{
afterSubmit:afterSubmit
}
});
This is my code
On create there is no old record.
Since you are trying to update the same record as was just created you are better off having this in a beforeSubmit event script
if((Itemname=prevItemname+'-c')&&(checkbox=true)) this is an error
if((Itemname == prevItemname+'-c') && checkbox) is more what you need
If you are trying to capture a copy operation you can set that up in the beforeLoad event that you use in the beforeSubmit event.
function beforeLoad(ctx){
if(ctx.type == ctx.UserEventType.COPY){
if(ctx.form){
ctx.form.addField({
id:'custpage_original_item',
label:'Copied Item',
type:ui.FieldType.SELECT,
source:'item'
}).updateDisplayType({
displayType:ui.FieldDisplayType.HIDDEN
}).defaultValue = ctx.request.parameters.id;
// your naming makes me wonder if you are trying to link to the
// source item rather than just saving a reference to the source
// item's name
/*
* Using the original item's name like below is closer to what you
* posted but I think by the time the script runs the itemid field
* has been cleared.
* ctx.form.addField({
* id:'custpage_original_item_name',
* label:'Copied Item Name',
* type:ui.FieldType.TEXT
* }).updateDisplayType({
* displayType:ui.FieldDisplayType.HIDDEN
* }).defaultValue = ctx.newRecord.getValue({fieldId:'itemid'});
*/
}
}
}
function beforeSubmit(ctx){
if(ctx.type == ctx.UserEventType.CREATE){
const itemRec = ctx.newRecord;
if(itemRec.getValue({fieldId:'custitem17'})){
const sourceId = itemRec.getValue({fieldId:'custpage_original_item'})
if(sourceId){
itemRec.setValue({
fieldId:'custitem_item_link:prevItemname',
value:sourceId
});
/* or use a search function to look up the original item's name
* and then test the new item's name.
*/
}
}
}
}

"SuiteScript 2.0 entry point scripts must implement one script type function" Error

I'm trying to upload this code to NetSuite
/**
* #NApiVersion 2.0
* #NScriptType ClientScript
* #NModuleScope SameAccount
*/
define(['N/ui/dialog'],
function(dialog){
/**
* Validation function to be executed when sublist line is committed.
*
* #param {Object} context
* #param {Record} context.currentRecord - Current form record
* #param {string} context.sublistId - Sublist name
*
* #returns {boolean} Return true if sublist line is valid
*
* #since 2015.2
*/
function validadeRate(context){
try{
var currentRecord = context.currentRecord
var sublistName = context.sublistId
if(sublistname ==='expense'){
var categ = CurrentRecord.getCurrentSublistValue({
sublistId: sublistName,
fieldId: 'category'
})
if ((categ = 259) && (rate != 0.819)){
var currIndex = currentRecord.getCurrentSublistIndex({
sublistId: sublistName
})
currIndex +=1
var options = {
title : 'Rate Incorreto!',
message:'Por favor, verifique o valor informado no campo Rate na linha ' + currIndex + '.',
}
dialog.alert(options).then(function (result) { }).catch(function(result){})
return false
}
}
return true
}
catch(ex){
log.error('validateLine: ', ex.message)
}
}
return {
validadeRate : validadeRate
}
});
But I'm getting this error when I'm trying to upload to file to Netsuite:
Notice
SuiteScript 2.0 entry point scripts must implement one script type function.*
This is part of a function that will validade the rate for one expense category.
How can I solve this?
thanks in advance!
This is NetSuite's 'Entry Point Script Validation' saying that the script is invalid because it doesn't include one of the predefined entry point (event) functions. These functions are:
fieldChanged
lineInit
pageInit
postSourcing
saveRecord
sublistChanged
validateDelete
validateField
validateInsert
validateLine
You can work around this validation and upload the script by adding one of those entry points, even if it does nothing. For example, inside your function (dialog) function you can add a pageInit() function:
function pageInit(scriptContext) {}
and change your return block to:
return {
validadeRate : validadeRate,
pageInit: pageInit
}
Now it has a valid entry point and the validation should pass.
However, there may be an even easier way. It appears (going by the JSDoc block), that your validadeRate function is supposed to be triggered each time a sublist line is added. This is exactly what the validateLine entry point is for. So you could just change the key in your return block to "validateLine"
return {
validateLine: validadeRate
}
and NetSuite would know to call validadeRate each time a line is added.
You have specified this as a Client Script module, but have not assigned a handler to any of the Client Script entry points. Read the Help document SuiteScript 2.0 Client Script Entry Points and API, and implement any one of the entry points in your module.
Change return function as below. and test once.
return
{
validateLine : validadeRate
}

NetSuite Create Transfer Order from Sales Order

I am trying to write a script that will create a new transfer order from a sales order, copying all of the lines in the process. I have the script written, but I am getting an error that "define" is not defined. This script is modified from another script, so it is possible that I missed something. I am new to scripting, so I would appreciate any help and could do without criticism (even if my script is complete garbage).
/**
***************** ALEM ********************
* After Submit User Event script running on Sales Orders. Generates a TO.
* Version Date Author Remarks
* 1.0 9 Jan madams Initial Create
*/
/**
* #NApiVersion 2.0
* #NScriptType UserEventScript
* #NModuleScope Public
*
*/
define(['N/record',], function (record) {
function afterSubmit(context) {
if(context.type == 'delete'){
log.debug('Exiting script', '...');
return;
}
try{
var so = record.load({
type:'salesorder',
id:context.newRecord.id
});
var so_items = so.getLineCount({sublistId:'item'});
// Create new Transfer Order if Record is On Create.
var to_record = record.create({
type:'transferorder',
isDynamic:true
});
to_record.setValue({fieldId:'customform', value:136});
to_record.setValue({fieldId:'class', value:so.getValue('class')});
to_record.setValue({fieldId:'transferlocation',
value:so.getValue('location')});
setLineItemsOnTO(so_items, to_record, so);
to_record.setValue({fieldId:'custbody_related_record',
value:context.newRecord.id});
so.setValue({fieldId:'custbody_related_record',
value:to_record.save()});
so.setValue({fieldId:'orderstatus',value:'B'});
so.save({ignoreMandatoryFields:true});
} catch(e){
log.debug('Error Loading Record' + context.newRecord.id, e);
return;
}
}
return {
afterSubmit: afterSubmit
}
function setLineItemsOnTO(so_items, to_record, so){
for(var i=0; i<so_items; i++){
to_record.selectNewLine({sublistId:'item'});
to_record.setCurrentSublistValue({
sublistId:'item',
fieldId:'item',
value:so.getSublistValue({
sublistId:'item',
fieldId:'item',
line:i
})
});
to_record.setCurrentSublistValue({
sublistId:'item',
fieldId:'quantity',
value:so.getSublistValue({
sublistId:'item',
fieldId:'quantity',
line:i
})
});
to_record.commitLine({sublistId:'item'});
}
}
});
Did NetSuite import the script as SuiteScript 2.0? It probably imported the script as SS1.0.
The comment block containing #NApiVersion 2.0 needs to be the first comment block in the file. NetSuite looks for that block only at the top of the file to identify SS2.0 scripts.

Hide other field on field change - Suite Script 2.0

I would like to hide the ID 'custrecord_hrx_vendor_status_list'
once item has selected in select box ( options ).
Here is my code.
/**
* #NApiVersion 2.x
* #NScriptType ClientScript
*/
define(['N/ui/serverWidget', 'N/error'],
function (error) {
function fieldChanged(context) {
var currentRecord = context.currentRecord;
var fieldId = context.fieldId;
if( fieldId === 'custrecord_hrx_negotiation_type' ){
var selectedType = currentRecord.getText(fieldId);
console.log(currentRecord.getField('custrecord_hrx_vendor_status_list'));
currentRecord.updateDisplayType({
id: 'custrecord_hrx_vendor_status_list',
displayType: serverWidget.FieldDisplayType.HIDDEN
});
}
}
return {
fieldChanged: fieldChanged
}
}
);
----HERE IS THE ERROR
As the error message says, you are trying to load a module that is not available. You are writing a client script, and trying to load a module that is only for server-side scripts.
Additionally, N/currentRecord#CurrentRecord does not have a updateDisplayType() method.
The way to hide a field in a SS2.0 client script is:
currentRecord.getField({
fieldId: 'custrecord_hrx_vendor_status_list'
}).isDisplay = false;
N/ui/serverwidget Module doen not work in client script.you must use like this to hide currentRecord.getField( { fieldId: id } ).isDisplay = false;

Pass variables from suitelet to clientscript in 2.x API?

I built a form with suitelet, that has a sublist, dropdown and a button. After user would tick some selections on the sublist, a button is pressed and the selected items are sent via rest elsewhere.
Suitelet:
#NApiVersion 2.x
*#NScriptType Suitelet
*/
define(['N/ui/serverWidget', 'N/search', 'N/https', 'N/record'],
function(serverWidget, search, https, record) {
function onRequest(context) {
if (context.request.method === 'GET') {
var form = serverWidget.createForm({ ... });
form.clientScriptModulePath = 'path/to/client/script';
// code to build a sublist, add a button and write page
} return {
onRequest: onRequest
};
});
Then, my clientscript is something like:
* #NApiVersion 2.x
* #NScriptType ClientScript
*/
define(
[ 'N/currentRecord', 'N/https' ],
function(currentRecord, https) {
functionSendRequest(sublist //the sublist that I want to get from the suitelet)
{
//code to build json string and send http request
} return {
saveRecord: test
}
});
Now, after spending a number of hours on this, a N/currentRecord came to my attention (I'm noobie with netsuite) and it would've seem as a problem solver for me, as it retrieves records that are currently active in client-side context. It works for great for dropdown menu and has a method getSublist(options), though it returns record.Sublist which has only getColumn() method. Thus, it won't really work for me. So, is there a way to pass sublist parameter to the clientscript from the suitelet once a button is pressed?
To solve your problem you could use getSublistValue from currentRecord like this:
var currentRec = currentRecord.get();
var numLines = currentRec.getLineCount({
sublistId: 'item'
});
var sublistFieldValue = currentRec.getSublistValue({
sublistId: 'item',
fieldId: 'item',
line: 3
});
If you really want to pass something from the Suitelet to the clientside function you gotta set your button this way:
var someTextToPassToTheClientscript = 'The Suitelet send its regards';
form.addButton({
id : 'custpage_some_button',
label : 'MyButton',
functionName : 'functionSendRequest("' + someTextToPassToTheClientscript + '")'
});
And then have your clientscript receive it like this:
/*
* #NApiVersion 2.x
* #NScriptType ClientScript
*/
define(
['N/currentRecord', 'N/https'],
function (currentRecord, https) {
functionSendRequest(textReceivedFromSuitelet) {
//code to build json string and send http request
}
return {
functionSendRequest : functionSendRequest
}
});

Resources