Creating form in netsuite using suitscript 2.0 - netsuite

var formData = new FormData();
formData.append("name", "John");
formData.append("age", "31");
for (var value of formData.values()) {
log.debug(value);
}
but when i want to log form values using formData api. It's giving below error.
ReferenceError: "FormData" is not defined.

FormData is a client side API managed under XMHttpRequest
UserEvent scripts are server side scripts with no browser based APIs available at all.
So you could use FormData in a client script to send info to a Suitelet or RESTlet but it's not present in a UserEvent script.
If you want to create a form in a Suitelet using SS2.0 you can use the following as a sample:
/**
*#NApiVersion 2.x
*#NScriptType Suitelet
*/
define(["N/log", "N/redirect", "N/runtime", "N/ui/serverWidget", "N/url", "./kotnRECBCFilters"],
function (log, redirect, runtime, ui, url, kotnRECBCFilters_1) {
function showPropertiesForm(context) {
var form = ui.createForm({
title: 'Property Trust Ledger'
});
var req = context.request;
var fromLoc = form.addField({
id: 'custpage_loc',
type: ui.FieldType.SELECT,
label: 'For Property',
source: 'location'
});
fromLoc.updateLayoutType({ layoutType: ui.FieldLayoutType.NORMAL });
fromLoc.updateBreakType({ breakType: ui.FieldBreakType.STARTCOL });
if (req.parameters.custpage_loc) {
fromLoc.defaultValue = req.parameters.custpage_loc;
}
var notAfterDate = form.addField({
id: 'custpage_not_after',
type: ui.FieldType.DATE,
label: 'On or Before'
});
if (req.parameters.custpage_not_after) {
notAfterDate.defaultValue = req.parameters.custpage_not_after;
}
form.addSubmitButton({
label: 'Get Detail'
});
//... bunch of stuff removed
context.response.writePage(form);
}
function onRequest(context) {
if (context.request.method === 'POST') {
var currentScript = runtime.getCurrentScript();
var params = {};
for (var k in context.request.parameters) {
if (k.indexOf('custpage_') == 0 && k.indexOf('custpage_transactions') == -1) {
if ((/^custpage_.*_display$/).test(k))
continue;
params[k] = context.request.parameters[k];
}
}
redirect.toSuitelet({
scriptId: currentScript.id,
deploymentId: currentScript.deploymentId,
parameters: params
});
return;
}
showPropertiesForm(context);
}
exports.onRequest = onRequest;
});

Related

Netsuite Print Button - "error.SuiteScriptError","name":"MISSING_PDF_PARAMETERS",

I have created a print button to print an advanced PDF from a sales order, so the client can get a commercial invoice generated. I have done this for other clients with success but I am running into an issue where the standard templates will print a pdf from the button but the customized ones will not. Where am I going wrong in the code here:
Suitelet:
function onRequest(context) {
var custom_id = context.request.parameters.custom_id;
var tempid = context.request.parameters.tempid;
log.debug("id", custom_id);
log.debug("id", context.request.parameters);
var pdfFileName = "Commercial Invoice";
var renderer = render.create();
var content = renderer.addRecord({
templateName: 'record',
record: record.load({
type: record.Type.SALES_ORDER,
id: custom_id
})
});
renderer.setTemplateByScriptId(tempid);
var pdfFile = renderer.renderAsPdf();
log.debug ("pdf", pdfFile)
context.response.writeFile(renderer.renderAsPdf(),true);
}
return {
onRequest: onRequest
}
});
Client Script:
function onButtonClick() {
var suiteletUrl = url.resolveScript({
scriptId: 'customscript_commercialinv_so_sl',
deploymentId: 'customdeploy_commercialinv_so_sl',
returnExternalUrl: true,
params: {
custom_id: currentRecord.get().id, tempid: "CUSTTMPL_109_4753601_SB1_795"
},
});
window.open(suiteletUrl,"_blank");
}
exports.onButtonClick = onButtonClick;
exports.pageInit = pageInit;
return exports;
});
My user script looks solid. I am getting this error when I direct the Client script to a custom template as referenced above:
{"type":"error.SuiteScriptError","name":"MISSING_PDF_PARAMETERS","message":"Missing parameters required to generate PDF","stack":["renderAsPdf(N/render)","onRequest(/SuiteScripts/MegWebb/cust_SOprinttemplate_SL.js:28)"],"cause":{"type":"internal error","code":"MISSING_PDF_PARAMETERS","details":"Missing parameters required to generate PDF","userEvent":null,"stackTrace":["renderAsPdf(N/render)","onRequest(/SuiteScripts/MegWebb/cust_SOprinttemplate_SL.js:28)"],"notifyOff":false},"id":"","notifyOff":false,"userFacing":false}
Any help would be much appreciated!

Search not working in restlet SuiteScript 2.0

I am trying to use search.create in RESTlet to check whether a contact exist in my netsuite account or not. This is my code
define(['N/record','N/search'],
function(record,search) {
function dosearch(contact_mail){
var result;
try{
result = search.create({
type : record.Type.CONTACT,
filters:['email','IS',contact_mail],
columns: ['email']
}).run().getRange({
start: 0,
end: 1
});
log.debug(result);
} catch(e){
result = e;
}
return result;
}
function doGet(requestParams) {
var contact_mail = requestParams.email;
log.debug(contact_mail);
var result = dosearch(contact_mail);
return result;
}
And I am accesing this RESTlet from my system using this node js code.
async function myGet(){
var para = {id: 'my_id' , email: 'email'};
var accountSettings = {
//'Content-Type': 'application/json',
//params: para,
accountId: account_id,
tokenKey: token_key,
tokenSecret: token_secret,
consumerKey: consumer_key,
consumerSecret: consumer_secret };
var urlSettings = {
url: link
}
var myInvoices = nsrestlet.createLink(accountSettings, urlSettings)
try{
var res = await myInvoices.get(para);
console.log(res);
} catch(e){
console.log(e);
}
}
Is there something wrong with the code? I tried same search code with user event script and it worked there but its not working in RESTlet. Can someone help me with this.
Silently failing a search is an indicator that the role you are using with your RESTlet doesn't have access to contacts. I generally see this with transactions I'd expect contacts work similarly because under the covers a contact is a form of entity.

Trigger a client script on a button clicked on a Suitelet?

I am trying to update the values on a custom record based on the button clicked on a suitelet
This will have 3 or 4 different buttons.
There is only one set up page that can be loaded on click of the button.
For example, 'recurring' is clicked on the suitelet. This will load a custom record page and set the parameters.
If 'fortnightly invoice' button is clicked, this will also load the same custom record page and set the parameters.
Where I am getting stuck: I have a suitelet with the buttons that are designed to load the custom record by calling a client script function
Instead, as soon as the suitelet loads, the custom record page loads and is stuck in an infinite loop reloading again and again
This is my suitelet script:
/**
*#NApiVersion 2.x
*#NScriptType Suitelet
*/
define(["N/ui/serverWidget"], function (ui) {
var exports = {};
function onRequest(context) {
if (context.request.method === "GET") {
var form = ui.createForm({
title: "Consolidated Invoicing Type",
});
// form.clientScriptModulePath =
// "SuiteScripts/sdf_ignore/Consolidated Invoice Client Script.js";
form.clientScriptFileId = 2659;
form.addButton({
id: "recurring",
label: "Recurring",
functionName: "pageInit",
});
context.response.writePage(form);
} else if ((context.response.method = "POST")) {
log.debug({
title: "request method type",
details: "suitelet is posting",
});
}
}
exports.onRequest = onRequest;
return exports;
});
This is the client script:
/**
*#NApiVersion 2.x
*#NScriptType ClientScript
*/
define(["N/record", "N/runtime", "N/url"], function (
record,
runtime,
url
) {
/**
* #param {ClientScriptContext.pageInit} context
*/
function pageInit(context) {
var scriptObj = runtime.getCurrentScript();
var recordType = scriptObj.getParameter("custscript_ci_suitelet_record");
var pageUrl =
"https://tstdrv.app.netsuite.com/app/common/custom/custrecordentry.nl?rectype=143&id=1&e=T";
var url = new URL(pageUrl);
window.location.href = url;
}
return {
pageInit: pageInit,
};
});
Do I need to use another script type to set the values on the custom record? (i.e not a client script)
How would I link a userevent script to the suitelet so that it is triggered on button click?
Why would the client script be automatically initiated on loading the suitelet page if it is supposed to be tied to a button on the form?
Thanks
The page keeps reloading because the "pageInit" function in your client script will be executed automatically by Netsuite, because "pageInit" is a default Netsuite Entry function. If you just rename your function it will work:
On the Suitelet:
form.addButton({
id: "recurring",
label: "Recurring",
functionName: "executeRecurring",
});
On the client script:
/**
*#NApiVersion 2.x
*#NScriptType ClientScript
*/
define(["N/record", "N/runtime", "N/url"], function (
record,
runtime,
url
) {
/**
* #param {ClientScriptContext.pageInit} context
*/
function executeRecurring() {
var scriptObj = runtime.getCurrentScript();
var recordType = scriptObj.getParameter("custscript_ci_suitelet_record");
var pageUrl =
"https://tstdrv.app.netsuite.com/app/common/custom/custrecordentry.nl?rectype=143&id=1&e=T";
var url = new URL(pageUrl);
window.location.href = url;
}
function pageInit(context) { // you need to keep at least one Netsuite Entry function, otherwise you will get an error
}
return {
pageInit: pageInit,
executeRecurring: executeRecurring
};
});
Also if the parameter custscript_ci_suitelet_record is a parameter on the Suitelet script, then you won't be able to get its value on the client script, you must get the value on the Suitelet script and pass it as parameter during the button call:
Suitelet:
/**
*#NApiVersion 2.x
*#NScriptType Suitelet
*/
define(["N/ui/serverWidget", "N/runtime"], function (ui, runtime) {
var exports = {};
function onRequest(context) {
if (context.request.method === "GET") {
var form = ui.createForm({
title: "Consolidated Invoicing Type",
});
// form.clientScriptModulePath =
// "SuiteScripts/sdf_ignore/Consolidated Invoice Client Script.js";
form.clientScriptFileId = 2659;
var recordType = runtime.getCurrentScript().getParameter("custscript_ci_suitelet_record");
form.addButton({
id: "recurring",
label: "Recurring",
functionName: "executeRecurring('" + recordType + "')",
});
context.response.writePage(form);
} else if ((context.response.method = "POST")) {
log.debug({
title: "request method type",
details: "suitelet is posting",
});
}
}
exports.onRequest = onRequest;
return exports;
});
Client script:
/**
*#NApiVersion 2.x
*#NScriptType ClientScript
*/
define(["N/record", "N/runtime", "N/url"], function (
record,
runtime,
url
) {
/**
* #param {ClientScriptContext.pageInit} context
*/
function executeRecurring(recType) {
var pageUrl =
"https://tstdrv.app.netsuite.com/app/common/custom/custrecordentry.nl?rectype=" + recType + "&id=1&e=T";
var url = new URL(pageUrl);
window.location.href = url;
}
function pageInit(context) { // you need to keep at least one Netsuite Entry function, otherwise you will get an error
}
return {
pageInit: pageInit,
executeRecurring: executeRecurring
};
});

Submit button and Client script button conflicting on Suitelet. SS 2.0

i have 2 buttons on my suitelet form, Standard Submit Button and Client Script button. The submit button does not execute if i have the clientScriptModulePath in my suitelet code. once i remove the clientScriptModulePath , the submit button starts working. Anyone have any idea what could be the reason?
Suitelet:
function onRequest(context) {
log.debug('request type', context.request.method);
var form = serverWidget.createForm({ title: 'Item Search' });
if (context.request.method === 'GET') {
var rec_id = context.request.parameters.id;
log.debug('rec id', rec_id)
form.addButton({
id: 'custpage_btn_goback',
label: 'Go Back',
functionName: 'onBackButtonClick'
});
form.clientScriptModulePath = '../client/mx_itemsearch_filter_cs.js';
form.addSubmitButton({
label: 'Submit'
});
context.response.writePage(form);
}
else {
log.debug('POST', 'POST')
}
}
Client script function:
function onBackButtonClick() {//TODO:
try {
console.log('in back button')
var currRec = currentRecord.get();
var suiteletUrl = url.resolveScript({
scriptId: 'customscript_mx_itemsearch_sl',
deploymentId: 'customdeploy_mx_itemsearch_sl'
});
console.log(suiteletUrl)
window.open(suiteletUrl, '_self');
} catch (e) {
log.error({ title: fx, details: 'Error - Code: ' + e.code + ', Message: ' + e.message });
}
}
You need to add one valid client side endpoint function. The endpoint function can just be a blank function. Example
function pageInit (context) {};
The your client script should have export the client endpoint function and your custom function.
return { pageInit: pageInit, onBackButtonClick: onBackButtonClick};

Pagination in Suitescript 2.0 Netsuite

I tried implementing netsuite pagination in suitescript 2.0, the prev button is working but instead of 20 search data it's return like whole. Next button is not working always throwing error INVALID_PAGE_RANGE
Here is the suitelet code
var loadSearch = search.load({
id: 'customsearch_inv123'
});
var i = 0;
var pagedData = loadSearch.runPaged({
pageSize: 20
});
pagedData.pageRanges.forEach(function (pageRange) {
var lastPageRange = pagedData.pageRanges[pagedData.pageRanges.length - 1];
var Page = pagedData.fetch({
index: lastPageRange.index
});
if (context.request.parameters.next) {
Page = Page.next();
}
if (context.request.parameters.prev) {
Page =Page.prev();
}
Page.data.forEach(function (result) {
var number = result.getValue({
name: 'inventorynumber'
});
if (number) {
updateSublist.setSublistValue({
id: 'custpage_number',
line: i,
value: number
});
}
i++;
});
});
// submit button
form.addSubmitButton({
label: 'Submit'
});
form.addButton({
id: '_next',
label: 'Next',
functionName: 'next'
});
form.addButton({
id: '_prev',
label: 'Prev',
functionName: 'prev'
});
form.clientScriptModulePath = 'SuiteScripts/client.js';
In client script i wrote the function next and prev, and redirected to the same page with next or prev parameters and based on that i called page.next and page.prev.
If you have implemented this please help me.
here is client code.
next: function () {
window.location.href = 'example.com' + '&next=t';
},
prev: function () {
window.location.href = 'example.com' + '&prev=t';
}
You should pass current/requested pageNumber from client script and then use it to fetch data for that specific page like below
var pagedData = loadSearch.runPaged({
pageSize: 20
});
requestedPageNumber = 2
var page = pagedData.fetch({ index: requestedPageNumber });
page.data.forEach(function (result) {
// result is the searchResult object
// YOUR CODE GOES HERE
var number = result.getValue({
name: 'inventorynumber'
});
})
What you did wrong was you were starting from the last index and then trying to fetch unexisting array index in case where user clicks next.
Note: previous button should be disabled/hidden where there are no pages so as to not run into the same error and the same could be done for the next button too.

Resources