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
}
});
Related
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
};
});
Most examples involve calling a client script in suitelet and then the suitelet button calls a function in client script. I was wondering if u can write the button function in suitelet instead.. is it possible?
This is because I want to run n/task module which cannot be used in a client side script and has to be used in the suitelet.
An example would be appreciated, thanks.
use the form.addSubmitButton. when clicked it works on the post action and the task module can be used.
You might also want to call a suitelet directly per your original question. If you want to avoid a client script just to have a function to call you can do:
/**
* #NApiVersion 2.x
* #NScriptType UserEventScript
*/
define(["N/log", "N/search", "N/url"], function (log, search, url) {
function beforeLoad(ctx) {
var form = ctx.form;
log.debug({ title: 'before load with ' + ctx.type, details: null });
if (ctx.type == ctx.UserEventType.VIEW) {
var ffRec = ctx.newRecord;
if (ffRec.getValue({/* some logic that requires the action */})) {
var orderStatus = search.lookupFields({
type: 'salesorder',
id: ffRec.getValue({ fieldId: 'createdfrom' }),
columns: [
'statusref'
]
}).statusref[0].value;
if (orderStatus.indexOf('pendingBilling') != -1) {
var suiteletURL = url.resolveScript({
scriptId: 'customscript_my_script',
deploymentId: 'customdeploy_my_script',
returnExternalUrl: true
});
form.addButton({
id: 'custpage_button_1',
label: 'My Action',
functionName: '(function processRebill(){window.location.href=\"'+ suiteletURL + '&custparam_ff=' + ffRec.id + '\";})'
});
}
}
}
}
return{
beforeLoad : beforeLoad
}
});
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
}
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.
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;