Reputation: 205
I have a suitelet with a sublist button and I am trying to get the button to execute a function on a custom module. I can not get it to work. I get an error "Cannot call method "receive" of undefined" Any Ideas?
Snippet of code to add button
define(['N/error', 'N/record', 'N/search', 'N/ui/serverWidget','./lib1'],
function(error, record, search, ui, lib1) {
//... some code here
searchSublist.addButton({
id: 'custpage_recievepayment',
label: 'Receive Payment',
functionName: "lib1.receive()"});
}
Snippet of custom Module
define(['N/redirect'],
function(redirect){
function receive(){
var deal = '497774';
var url = redirect.toSuitelet({
scriptId: 'customscript_deal_entry_2_0',
deploymentId: 'customdeploy1',
returnExternalUrl: false,
params: {
prevdeal: url
}
})
}
});
Upvotes: 8
Views: 13350
Reputation: 205
I was able to get this to work with out the client script or exporting it to the global object that was suggested. The key was to modify my custom module to return the function I wanted to use on the button and calling the custom module file with form.clientScriptFileId
//suitelet
define(['N/error', 'N/record', 'N/search', 'N/ui/serverWidget'],
function(error, record, search, ui) {
// other code here
form.clientScriptFileId = 78627;//this is the file cabinet internal id of my custom module
var searchSublist = form.addSublist({
id: 'custpage_deals',
type: ui.SublistType.LIST,
label: 'Deals'
})
searchSublist.addButton({
id: 'custpage_receivepayment',
label: 'Receive Payment',
functionName: "receive()"
});
//custom module
define(['N/url','N/error'],
function(url, error) {
return{
receive: function(){
//function code here
}
}
})
Upvotes: 4
Reputation: 31
For suitescript 2.0, you'll need to define the file that actually contains your function.
/**
*@NApiVersion 2.x
*@NScriptType UserEventScript
*/
define([],
function() {
function beforeLoad(context) {
if(context.type == "view") {
context.form.clientScriptFileId = 19181;
context.form.addButton({
id: 'custpage_dropshippo',
label: 'Generate Dropship PO',
functionName: 'generateDropshipPurchaseOrder'
});
}
}
return {
beforeLoad: beforeLoad,
};
}
);
In that example, the value 19181 is the file cabinet ID of the following file (which doesn't need a deployment but does need a script record):
/**
*@NApiVersion 2.x
*@NScriptType ClientScript
*/
define([],
function() {
function pageInit() {
}
function generateDropshipPurchaseOrder() {
console.log("foo");
}
return {
pageInit: pageInit,
generateDropshipPurchaseOrder: generateDropshipPurchaseOrder
};
});
Upvotes: 3
Reputation: 21
Thanks for sharing this information. I finally made it to work by using the "file cabinet internal id" not the "client script" internal id, as mentioned by someone in this thread.
Also i noticed through the chrom developer tool that "scriptContext" is not automatically passed over to the client script function as i had expected. So i had to manually pass in the data i need in the client script through the following way:
function beforeLoad(scriptContext) {
var form = scriptContext.form;
form.clientScriptFileId = 33595032;
form.addButton({
id: 'custpage_reprocessinvoice',
label: 'Reprocess Invoice',
functionName: 'reprocess(' + scriptContext.newRecord.id + ')'
});
}
Hope this may help someone and save him/her a bit of time.
Upvotes: 2
Reputation: 205
After not getting this to work after multiple tries I filed a defect with Netsuite (Defect 390444) and they have just told me that this has now been fixed and tested and will be in the next major release.
Upvotes: 0
Reputation: 8847
Button click handlers is something of an issue (at least, in my opinion) in 2.0. You are getting this error because lib1
is not defined on the client side.
What I have had to do is create a Client Script that exports my click handlers to the global object to make them accessible. Something like:
// Client script
define(['my/custom/module'], function (myModule) {
window.receive = myModule.receive;
});
// Suitelet
define(...
// some code...
searchSublist.addButton({
id: 'custpage_recievepayment',
label: 'Receive Payment',
functionName: "receive"
});
// other code...
It's not ideal, and certainly not good practice in general to export to the global space, but I do not know any other way to reference a function within a module on a button click.
Upvotes: 1