This topic contains 14 replies, has 0 voices, and was last updated by michoel 6 years, 11 months ago.
-
AuthorPosts
-
November 28, 2017 at 1:48 pm #18470
latchadevHi- I’m brand new to SuiteScript. What I’ve been asked to do is really simple, but I’m not sure if it can be done in our Netsuite account.
We created a custom checkbox field for timesheets. When the customer field loses focus I want to grab the job id (or even the text would work) and if it’s a particular number then I want to make visible the checkbox column. We want to have the checkbox only apply on a couple of customers/jobs.
Normally this is very trivial, I’d do something like :
document.addEventListener(‘DomContentLoaded’, function(){
var jobElement = document.getElementById(‘someId’);
var customElement = document.getElementById(‘itsId’);
customElement.style.visibility = ‘hidden’ //default to not display
jobElement.addEventListener(‘blur’, function(e){
if (jobElement.value = ‘123’) {
customElement.style.visibility = ‘visible’;
}
else {
customElement.style.visibility = ‘hidden’;
}
})
})
How would I go about doing this with SuiteScript?
Thanks for any help!
This is a cached copy. Click here to see the original post. -
November 28, 2017 at 2:39 pm #18471
pcutlerSuiteScript doesn’t offer a supported way to show or hide fields in client scripts. But you have a few options:
1. Use a user event script (runs server side) and automatically refresh the page to trigger it.
2. Same as #1, but for a suitelet hosted within an iframe, so the whole page doesn’t reload.
3. Build your fields that need to be shown and hidden inside an HTML field and use JavaScript to show/hide those fields directly as in your example above.
4. Use undocumented methods to show/hide the fields – this may break when NetSuite updates their product
5. Consider disabling fields instead of hiding if it works for your use case. NetSuite does support enabling and disabling fields client side.
Most people on this forum will probably say they picked #4 and accepted the risk.
-
November 28, 2017 at 2:50 pm #18472
latchadevIs there a high level overview of how suitescript works- because honestly none of those options make any sense to me. Picture yourself having a web background and zero experience with SuiteScript. What do I need to know.
For example, #4- How would I find an undocumented method? etc.
-
November 28, 2017 at 2:56 pm #18473
pcutlerSorry if my response was overly complex – when trying to do something that SuiteScript isn’t supposed to support, there are often work-arounds, but they can get tricky. As far as what SuiteScript is designed to do, it’s actually pretty easy to use. You can click the HELP link at the top of any page and navigate to SuiteCloud > SuiteScript in the table of contents for documentation.
The first thing you have to know is that there are two different versions of SuiteScript – SuiteScript 1.0 and SuiteScript 2.0.
In SuiteScript 1.0, there is are two methods for disabling fields: nlapiDisableField and nlapiDisableLineItemField. SuiteScript 2.0 offers equivalent functionality.
As far as finding undocumented API methods, you can use your web browser’s developer tools. But if disabling fields works for your use case, I’d go with that instead.
Feeling any better now? ๐
-
November 28, 2017 at 3:59 pm #18474
michoelOriginally posted by pcutler
View Post
SuiteScript doesn’t offer a supported way to show or hide fields in client scripts. But you have a few options:
Actually SS2.0 does, with the N/currentRecord#Field.isDisplay property. In SS1.0 you had to use the undocumented nlapiSetFieldDisplay(), BUT if you wanted to do it in a “supported” way you could hide a field with a client side workflow which just generates JS code that uses that function!
Code:
/**
* @NApiVersion 2.x
* @NScriptType ClientScript
*/define([], function () {
function fieldChanged(context) {
if(context.fieldId === ‘someId’) {
var jobElementValue = context.currentRecord.getValue({ fieldId: ‘someId’ });
var customElement = context.currentRecord.getField({ fieldId: ‘itsId’ });
if (jobElementValue = ‘123’) {
customElement.isDisplay = false;
} else {
customElement.isDisplay = true;
}
}
}return {
fieldChanged: fieldChanged
}
}
); -
November 28, 2017 at 4:31 pm #18475
latchadevOk, great, that makes sense. I really appreciate it. This is exactly what I needed to see. What event fires, how to get elements and their values, and set attributes. Perfect.
pcutler, I didn’t mean to be rude so sorry if it came out that way. What Michoel wrote is precisely what I needed. I should have just asked “what is the SS equivalent to getElementById()”, etc..
-
November 28, 2017 at 4:38 pm #18476
pcutlermichoel – Thanks for pointing that out, I wasn’t aware of that new feature in SS 2.0!
latchadev – No offense taken, glad you were able to get the information you needed!
-
November 30, 2017 at 8:39 am #18477
latchadevHi, so I am getting back to this this morning and I would like to just display an alert or console.log message when a field changes.. I have uploaded and deployed the client script but nothing happens on the page type that I’ve declared (it’s applied to Timesheet record). Here is the full code:
/**
* @NApiVersion 2.x
* @NScriptType ClientScript
*/
define([], function () {
function fieldChanged(context) {
try
{
dialog.alert({title: ‘hi’, message: ‘field id is: ‘ + context.fieldId});
}
catch (e)
{
log.error({title: e.name, details: e.message});
}
}
return {
fieldChanged: fieldChanged
}
}
);
Am I missing a step in the deployment process perhaps? Because nothing happens in the console or anywhere else when I go to a new timesheet record in our sandbox environment and change a field. Thanks!
-
November 30, 2017 at 10:25 am #18478
latchadevActually – I created a new script and deployment and this time it at least tried to load my module. I am getting the error ”SuiteScriptModuleLoaderError” for requireJS. Do I need to add a dependency at define[] ? Or is there anything wrong that you can think of with my script above??? Thank you.
-
November 30, 2017 at 1:17 pm #18479
pcutlerDialog is not a global object, so to display a dialog you would need something like
define([‘N/ui/dialog’], function(dialog) {…
-
December 4, 2017 at 9:53 am #18480
latchadevThanks pccutler! So now I am to the point where I am using the ctx.fieldId to get the name of the field that triggers the change event. The one I am interested in is called ‘customer’. This is the one whose value I need to check. However, when trying to use currentRecord.getField, passing the field id ‘customer’, it returns undefined.
eg,
function(){
function alertTest(context) {
if (context.fieldId == ‘customer’) {
var v = context.currentRecord.getValue({‘fieldId’ : ‘customer’});
alert(v); //undefined.
}
}
}
Does anything stick out as here as to what I might be missing? Thanks !
-
December 4, 2017 at 3:43 pm #18481
latchadevSo I’ve tried every field that fires a change event and the result of ctx.currentRecord.getfield or getValue() is always null or undefined respectively. Here is my code:
/**
*@NApiVersion 2.0
*@NScriptType ClientScript
*/
define([‘N/currentRecord’], function () {
function fieldChanged(context) {
console.log(context.fieldId); //logs ‘isexempt’ when exempt checkbox changes, which is correct
console.log(context.currentRecord.getField({‘field Id’: ‘isexempt’})) //logs null
console.log(context.currentRecord.getValue({‘field Id’: ‘isexempt’})) //logs undefined
//to see these functions..
console.log(context.currentRecord.getField);
console.log(context.currentRecord.getValue);
}
return {
fieldChanged: fieldChanged
}
}
);
Can you see what I might be missing? I get the field Id successfully, but the getField/Value functions are not working. Is there another way to get the damn values?
-
December 4, 2017 at 9:10 pm #18482
michoelOriginally posted by latchadev
View Post
console.log(context.currentRecord.getField({‘field Id’: ‘isexempt’})) //logs null
You have an space between field and Id that shouldn’t be there
-
December 5, 2017 at 9:17 am #18483
latchadevMichoel- actually I don’t. Sorry, I typed that instead of copy/paste.
Here’s the thing. I tried it on another record type and it worked perfectly. GetField and GetValue work and so does isDisplay(). It’s just on the time grid fields on the Timesheet record type.
Here is the code, which I copied:
/**
*@NApiVersion 2.0
*@NScriptType ClientScript
*/
Code:
define([‘N/currentRecord’], function () {function fieldChanged(context) {
var f = context.currentRecord.getField({fieldId: context.fieldId});
//works on Job record but not on Timesheet (on Timesheet f is null)…
f.isDisplay = false;}
return {
fieldChanged: fieldChanged
}
}
);Is there a different way to get the data from a timesheet grid? Perhaps someone can try this really quickly and verify that it doesn’t work for a timesheet.
Thanks again.
-
December 5, 2017 at 7:44 pm #18484
michoelIt may be that the field you are trying to access is a line-level item. Then you would have to use the getSublistValue() and getSublistField() methods instead.
I haven’t tried though, it may not be possible to hide a field on the line level.
Code:
var field = context.currentRecord.getSublistField({
sublistId: context.sublistId,
fieldId: context.fieldId,
line: context.line,
}); -
AuthorPosts
You must be logged in to reply to this topic.