I want to edit a record, then click "Save" and the field "custitem_con" to be updated with a new value and the record saved.
/**
*#NApiVersion 2.0
*#NScriptType ClientScript
*/
define(['N/currentRecord'],
function(currentRecord) {
function saveRecord (){
var objRecord = currentRecord.get();
var con = 'Success!...but record is not saved :(';
objRecord.setValue({
fieldId: 'custitem_con',
value: con,
});
}
return {
saveRecord: saveRecord
};});
However while the field custitem_con gets the value, the record is not saved, but remains in edit mode. How do I get the record saved?
In order to allow the record to be submitted, you need to return true from the saveRecord() function, thus:
/**
*#NApiVersion 2.0
*#NScriptType ClientScript
*/
define(['N/currentRecord'],
function(currentRecord) {
function saveRecord (){
var objRecord = currentRecord.get();
var con = 'Success!...but record is not saved :(';
objRecord.setValue({
fieldId: 'custitem_con',
value: con,
});
return true;
}
return {
saveRecord: saveRecord
};});
Related
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
};
});
I am Learning netsuite,
I am trying to fetch the Customfield "custitem_celigo_sfnc_salesforce_id" from item, on sales order module when adding that item in SO line items
here is my sample
/**
*#NApiVersion 2.x
*#NScriptType ClientScript
*/
define(['N/record', 'N/search', 'N/url', 'N/https', 'N/runtime'], function(record, search, url, https, runtime) {
function fieldChanged(context) {
var currentRecord = context.currentRecord;
var sublistName = context.sublistId;
var sublistFieldName = context.fieldId;
var line = context.line;
var SFID = context.custitem_celigo_sfnc_salesforce_id;
var descriptionValue = currentRecord.getCurrentSublistValue({
sublistId: sublistName,
fieldId: "custitem_celigo_sfnc_salesforce_id"
})
alert(JSON.stringify(currentRecord));
alert(JSON.stringify(test));
return true;
}
var exports = {};
exports.fieldChanged = fieldChanged;
return exports;
});
It is not fetching the custom field , what is the way to do that.
Thanks in advance,
Try this one
var descriptionValue = currentRecord.getCurrentSublistValue({
sublistId: item,
fieldId: "custitem_celigo_sfnc_salesforce_id",
line:line
})
I am making a usereventcript that computes for the rate using subfields from the sales order item list. trying to save and deploy the script launches an error Fail to evaluate script: {"type":"error.SuiteScriptModuleLoaderError","name":"UNEXPECTED_ERROR","message":"missing } after property list (SS_SCRIPT_FOR_METADATA#32)","stack":[]}
/**
*#NApiVersion 2.x
*#NScriptType UserEventScript
*/
define(
[
'N/record'
],
function (record) {
/**
* #param {UserEventContext.beforeSubmit} context
*/
function beforeSubmit(context) {
//get taxcode
var taxcode = context.newRecord.getValue({
fieldId: 'taxcode'
});
if(taxcode !== 0 || taxcode !== 4){
// Gets the Total Amount
var amount = context.getValue.getValue({
fieldId: "amount"
});
// Gets the quantity of an item selected
var quantity = context.newRecord.getValue({
fieldId: 'quantity'
});
var rate_ = amount/quantity;
var newRate = context.newRecord.setValue({
fieldId : 'rate'
value : ('rate',rate_)
});
}
}
return {
// beforeSubmit: beforeSubmit,
};
});
Your code is not syntactically valid. Please replace below code
var newRate = context.newRecord.setValue({
fieldId: 'rate'
value: ('rate', rate_)
});
with
var newRate = context.newRecord.setValue({
fieldId: 'rate',
value: ('rate', rate_)
});
You can try validating your code with JS syntax validator esprima. Although alot of IDE's now support validation.
So, basically I am trying to get the XML template for the PDF and I plan to eventually add some additional XML in the code after getting the template working in this manner. However, when I attempt to pass a data source object to the PDF it is not working. Does anyone know the cause of this issue, and what I am doing incorrectly here?
XML template (stripped out everything but the variable in the table for testing):
<?xml version="1.0"?><!DOCTYPE pdf PUBLIC "-//big.faceless.org//report" "report-1.1.dtd">
<pdf>
<!--removed lengthy head to make code more readable-->
<body footer="nlfooter" footer-height="20pt" padding="0.5in 0.5in 0.5in 0.5in" size="Letter">
<table class="body" style="width: 100%; margin-top: 10px;">
<tr>
<td>${jsonObj.terms}</td>
</tr></table>
</body>
</pdf>
Script:
/**
* #NApiVersion 2.x
* #NScriptType UserEventScript
* #NModuleScope SameAccount
*/
define(['N/error','N/render','N/file','N/record','N/log'],
/**
* #param {error} error
*/
function(error, render, file, record, log) {
function beforeSubmit(context) {
log.debug('After submitting invoice, create advanced PDF detail layout', context);
var isInvoice = context.newRecord.type == 'invoice';
// Create advanced PDF
if (isInvoice){
log.audit('Creating invoice');
renderRecordToPdfWithTemplate(context.newRecord);
}
else{
error.create({
name: 'ERROR_RECEIVED',
message: 'Cannot create advanced PDF from this record type'
});
log.audit(error.name,error.message);
}
}
return {
beforeSubmit: beforeSubmit
};
function renderRecordToPdfWithTemplate(context) {
var jsonObj = {
terms: "test terms"
};
var templateId = '7959'; // ID of the XML
var templateFile = file.load({id: templateId});
var renderer = render.create();
renderer.templateContent = templateFile.getContents();
/*
renderer.addRecord({
type: record.Type.INVOICE,
record: context
});
*/
renderer.addCustomDataSource({
format: render.DataSource.OBJECT,
alias: "jsonObj",
data: jsonObj
});
log.debug('Rendering as PDF');
var renderXmlAsString = renderer.renderAsString();
log.debug('Added record to PDF', context);
var invoicePdf = render.xmlToPdf({
xmlString: renderXmlAsString
});
invoicePdf.name = 'Testpdf2.pdf';
invoicePdf.folder = -15;
try{
var fileId = invoicePdf.save();
log.debug('Saved PDF to file '+fileId);
}
catch(e){
alert('Error saving file');
log.debug('Error saving file');
debugger;
}
}
});
You don't need the renderer.renderAsString(); since you're already loading the XML from the file cabinet.
function renderRecordToPdfWithTemplate(context) {
var jsonObj = {
terms: "test terms"
};
var templateId = '7959'; // ID of the XML
var templateFile = file.load({id: templateId});
var renderer = render.create();
renderer.templateContent = templateFile.getContents();
renderer.addCustomDataSource({
format: render.DataSource.OBJECT,
alias: "jsonObj",
data: jsonObj
});
log.debug('Rendering as PDF');
var invoicePdf = renderer.renderAsPdf();
invoicePdf.name = 'Testpdf2.pdf';
invoicePdf.folder = -15;
try{
var fileId = invoicePdf.save();
log.debug('Saved PDF to file '+fileId);
}
catch(e){
alert('Error saving file');
log.debug('Error saving file');
debugger;
}
}
I'm trying to set a value for the sublist 'addressbookaddress'. But the script fail with error. However, I'm able to get the subrecord value.
Error:
Not supported on the current subrecord: CurrentSubrecord.setValue.
Executed code:
/**
*#NApiVersion 2.0
*#NScriptType ClientScript
*/
define(["N/currentRecord"], function(currentRecord){
/*
Copy phone number from vendor to address, when creating a new sublist entry
*/
var lineInit = function(context) {
var record = context.currentRecord;
var sublistId = context.sublistId;
var subrecord = record.getCurrentSublistSubrecord({
sublistId: sublistId,
fieldId: 'addressbookaddress'
});
if (!subrecord) {
return;
}
var address = subrecord.getValue({
fieldId: 'addr1'
});
subrecord.setValue({
fieldId: 'addr1',
value: 'test'
});
return;
}
return {
lineInit: lineInit,
}
});
Client scripts have read-only access to subrecords.
A client script can be deployed on the Address form. Using values from the entryformquerystring one can search for the parent record.