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
}
});
Related
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;
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
}
});
Say I have the following snippet, which is basically a form with a button attached to it. On click, I want to execute a function:
define(['N/ui/serverWidget', 'N/search', 'N/https'],
function(serverWidget, search, https) {
function onRequest(context) {
if (context.request.method === 'GET')
{
var form = serverWidget.createForm({
title: 'Some Form'
});
// some code
form.addButton({
id : '_buttonId',
label : 'Button Label',
functionName: "someFunctinonIWantToCallOnClick(myParam)"
});
context.response.writePage(form)
} else {
// some other code
}
}
function someFunctinonIWantToCallOnClick(myParam)
{
// some code
}
return {
onRequest: onRequest
};
});
According to the NetSuite's documentation:
options.functionName
string
optional
The function name to be triggered on a click event.
Version 2016 Release 1
What am I doing wrong?
There are a number of things wrong with your example.
you are not actually writing the form. Eventually you need:
context.response.writePage(form);
Your function you want to call is only on the server. It is not defined on the client. You need to include a client script that has that function:
form.clientScriptModulePath = './myFormClient.js';