'Close a Suitelet window on submit
I am trying to use window.close() on a suitelet as part of the post response.
The suitelet is a pop up that was opened from a client script.
This is the relevant part of the client script:
/**
*@NApiVersion 2.x
*@NScriptType ClientScript
*/
define(["N/record", "N/url", "N/currentRecord"], function (
record,
url,
currentRecord
) {
/**
* @param {ClientScriptContext.pageInit} context
* @param {ClientScriptContext.onclick_callforSuitelet} context
*/
function pageInit() {}
function onclick_callforSuitelet() {
var record = currentRecord.get();
var recordId = record.id;
var recordType = record.type;
log.debug("recId", recordId);
log.debug("recType", recordType);
//this is the script id on the script record for the suitelet (not the deployment)
var suiteletURL = url.resolveScript({
scriptId: "customscript_suitelet_notes",
//this is the id from the deployment record for the suitelet
deploymentId: "customdeploy_suitelet_notes",
params: { recordId: recordId, recordType: recordType },
});
window.open(
suiteletURL,
"_blank",
"popup=yes,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,top=500,left=500,width=1000,height=1000"
);
I am getting the following error message: "org.mozilla.javascript.EcmaError: ReferenceError: "window" is not defined.".
I can't find anything online on how to define the 'window' (i.e. the current window).
Alternatively, I am thinking of adding a onclick event that automatically closes the window on submit. I can't seem to find anything on how I would go about doing that (which APIs to use) or whether that is even possible?
So my questions are around:
- How do I define the 'window' in a server side script?
- If the submit button on the suitelet was added using
form.addSubmitButton({label:submit})
is it possible to add a function to the submit button so that in addition to saving the record. the window can be closed after a specified number of seconds? (i.e. adding a setTimeout method?)
Would I need a custom button to achieve this instead of the form.addSubmit button?
I have tried various methods though am having no joy since I am not even sure any of this is possible to do.
Edit: I have tried creating a new client script with the window.close function on pageinit and attaching this client script to the suitelet response per below
/** *@NApiVersion 2.x
* *@NScriptType Suitelet
* */
define(["N/ui/serverWidget", "N/log", "N/record", "N/url"], function (
serverWidget,
log,
record,
url
) {
/** * @param {SuiteletContext.onRequest} context */
function onRequest(context) {
if (context.request.method === "GET") {
var invoice_id = parseInt(context.request.parameters.recordId);
var form = serverWidget.createForm({ id: "notes", title: "Notes" });
var customerGroup = form.addFieldGroup({
id: "customerDetails",
label: "Customer Details",
});
customerGroup.isSingleColumn = false;
form.addSubmitButton({ label: "Submit" });
var select = form.addField({
id: "custpage_source",
type: serverWidget.FieldType.SELECT,
label: "Source of Communication",
container: "customerDetails",
});
select.addSelectOption({ value: 1, text: "Phone" });
select.addSelectOption({ value: 2, text: "Email" });
select.addSelectOption({ value: 3, text: "Website Contact Form" });
select.addSelectOption({ value: 4, text: "Other" });
form.addPageLink({
type: serverWidget.FormPageLinkType.CROSSLINK,
title: "Invoice",
url:
"https://<accountid>.app.netsuite.com/app/accounting/transactions/custinvc.nl?id=" +
invoice_id,
});
var parentTransaction = form.addField({
id: "custpage_parent_transaction",
type: serverWidget.FieldType.SELECT,
label: "Parent Transaction",
container: "customerDetails",
});
parentTransaction.addSelectOption({
value: invoice_id,
text: invoice_id,
});
context.response.writePage(form);
function getBaseUrl() {
return url.resolveRecord({ recordType: record.Type.INVOICE });
}
} else {
var delimiter = /\u0001/;
var sourceField = context.request.parameters.custpage_source;
var parentField = context.request.parameters.custpage_parent_transaction;
var invoiceRecord = record.load({
type: record.Type.INVOICE,
id: parentField,
});
log.debug("parent record", invoiceRecord);
var recObj = record.create({ type: "customrecord_user_notes" });
recObj.setValue({ fieldId: "custrecord_source", value: sourceField });
recObj.setValue({
fieldId: "custrecord_parent_transaction",
value: parentField,
});
var userNote = recObj.save({});
var notesFieldUpdate = record.submitFields({
type: record.Type.INVOICE,
id: parentField,
values: {
custbody_notes_check:
"<span style='color:#e74c3c;'>Check Notes</span>",
},
});
log.debug("notesfield", notesFieldUpdate);
// context.response.write("Note Created");
var form_close = serverWidget.createForm({ id: "notes_close", title: "Notes" });
form_close.clientScriptFileId = 2637;
context.response.writePage(form_close)
}
}
return { onRequest: onRequest };
});
The form_close page is being created on submit though the client script function is not triggering
This is the client script:
/**
*@NApiVersion 2.x
*@NScriptType ClientScript
*/
define([], function () {
/**
* @param {ClientScriptContext.pageInit} context
*/
function pageInit(context) {
alert("client script pageinit triggered")
window.close();
}
return {
pageInit: pageInit,
};
});
Am I missing something on the client script?
Solution 1:[1]
I could not get the client script to trigger on the post response of the suitelet.
Instead, I have found this is the only way to close the window after the suitelet has been submitted
/**
*@NApiVersion 2.x
*@NScriptType Suitelet
*/
define(["N/ui/serverWidget", "N/log", "N/record", "N/url"], function (
serverWidget,
log,
record,
url
) {
/**
* @param {SuiteletContext.onRequest} context
*/
function onRequest(context) {
if (context.request.method === "GET") {
// Section One - Forms
var invoice_id = parseInt(context.request.parameters.recordId);
var form = serverWidget.createForm({
id: "notes",
title: "Notes",
});
//form.clientScriptFileId=5110;
var customerGroup = form.addFieldGroup({
id: "customerDetails",
label: "Customer Details",
});
customerGroup.isSingleColumn = false;
form.addSubmitButton({
label: "Submit",
});
var select = form.addField({
id: "custpage_source",
type: serverWidget.FieldType.SELECT,
label: "Source of Communication",
container: "customerDetails",
});
//the value is the internal id of the option under customisation>lists,records and forms>lists>'source of communication' list
select.addSelectOption({
value: 1,
text: "Phone",
});
select.addSelectOption({
value: 2,
text: "Email",
});
select.addSelectOption({
value: 3,
text: "Website Contact Form",
});
select.addSelectOption({
value: 4,
text: "Other",
});
form.addPageLink({
type: serverWidget.FormPageLinkType.CROSSLINK,
title: "Invoice",
url:
"https://<accountid>.app.netsuite.com/app/accounting/transactions/custinvc.nl?id=" +
invoice_id,
});
var parentTransaction = form.addField({
id: "custpage_parent_transaction",
type: serverWidget.FieldType.SELECT,
label: "Parent Transaction",
container: "customerDetails",
});
parentTransaction.addSelectOption({
value: invoice_id,
text: invoice_id,
});
// form.updateDefaultValues({
// custpage_recordurl:
// "https://<accountid>.app.netsuite.com/app/accounting/transactions/custinvc.nl?id=" +
// recId,
// });
context.response.writePage(form);
function getBaseUrl() {
return url.resolveRecord({
recordType: record.Type.INVOICE,
});
}
// Section Two - Tabs - See "Steps for Adding a Tab to a Form"
// Section Three - Sublist - See "Steps for Adding a Sublist to a Form"
} else {
// Section Four - Output - Used in all sections
var delimiter = /\u0001/;
var sourceField = context.request.parameters.custpage_source;
var parentField = context.request.parameters.custpage_parent_transaction;
var invoiceRecord = record.load({
type: record.Type.INVOICE,
id: parentField,
});
log.debug("parent record", invoiceRecord);
// context.response.write(
// "You have entered:" + "<br/> Name: " + sourceField
// );
var recObj = record.create({
type: "customrecord_user_notes",
});
recObj.setValue({ fieldId: "custrecord_source", value: sourceField });
recObj.setValue({
fieldId: "custrecord_parent_transaction",
value: parentField,
});
var userNote = recObj.save({});
var notesFieldUpdate = record.submitFields({
type: record.Type.INVOICE,
id: parentField,
values: {
custbody_notes_check:
"<span style='color:#e74c3c;'>Check Notes</span>",
},
});
log.debug("notesfield", notesFieldUpdate);
// context.response.write((location, "_self").close());
context.response.write("Note Created");
context.response.write("<script>window.close();</script>");
// var close_form = serverWidget.createForm({
// id: "close_form",
// title: "close form",
// });
// close_form.clientScriptFileId = 2637;
// context.response.writePage(close_form);
}
}
return {
onRequest: onRequest,
};
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Vernita |