Suitescript - Hide Field based on Drop Down Value - netsuite

I need to hide a field in a record if a certain option is selected in a drop down field. Using the below code no matter what choice I make in the drop down the field is hiding. Any help appreciated:
define([], function () {
/*Field Change event*/
function fieldChanged(context) {
var records = context.currentRecord;
if (context.fieldId == 'custbody_pick_ship') {
var customElement = context.currentRecord.getField({ fieldId: 'custbody_zone' });
var type = records.getValue({
fieldId: 'custbody_pick_ship'
});
if (type = "Pick Up") {
customElement.isDisplay = false;
} else {
customElement.isDisplay = true;
}
}
}
return {
fieldChanged: fieldChanged
}
}
);

Your problem lies in the line if (type = "Pick Up"). A single = in JavaScript is an assignment operator, which means you are setting the value of the type variable to "Pick Up". As "Pick Up" is a non-falsy value that expression will always evaluate as true. You need to use either == or === to test equality (in general the triple equal is preferable).
if (type === "Pick Up") {
customElement.isDisplay = false;
} else {
customElement.isDisplay = true;
}

Related

Suitescript 2.0 - How to show a field and update value based on dropdown?

I have a drop down that, when a particular option is selected a hidden field will be displayed and that field which have a new value. I am able to get the field to display but am not able to populate the field with the value. Script below:
function fieldChanged(context) {
var records = context.currentRecord;
if (context.fieldId == 'custbody_data') {
var note = context.currentRecord.getField({ fieldId: 'custbody_note' });
var type = records.getValue({
fieldId: 'custbody_data'
});
if (type == "2") {
note.isDisplay = true;
note.setValue = "test";
} else if (type == "1") {
note.isDisplay = false;
note.setValue = "";
}
}
}
return {
fieldChanged: fieldChanged
}
note.setValue = "";
There are two problems in what you are trying to do:
With the NetSuite API, to manipulate the value of fields in the record you need to use the N/currentRecord#Record object , not the N/currentRecord#Field. In other words you need to be calling context.currentRecord.setValue().
setValue is a method, not a property. I.e. you need to call the function setValue() with the new value, and not assign it a value (note.setValue = "new value") like you are attempting.
Putting those together, the correct syntax for updating a field value is:
context.currentRecord.setValue({
fieldId: 'custbody_note',
value: "test",
});

ReferenceError - Context is not defined (Netsuite)

I am trying to hide a field when a checkbox is checked in an Opportunity but the script I have is throwing a ReferenceError - Context is not defined.
Here is my code:
define(['N/currentRecord','N/search', 'N/format'],
function(currentRecord, search, format) {
function pageInit_disableMyText(scriptContext) {
console.log('START : here you will be able see logs via console (F12 on browser)');
var recCurrent = context.currentRecord; //This represents your current loaded record, you can get the values of the fields here
var myCheckbox = recCurrent.getValue('custbody7'); //This is the code for getting the value of your field
console.log('myCheckbox Value is : ' + myCheckbox);
if(myCheckbox == true)
{
//get the field and disable it
fld1 = recCurrent.getField('custbody3');
fld1.isDisabled = true;
}
else
{
//if you didn't set the field to be disabled by default, then you don't need the code to enable it here
//since it runs only once when the page loads
}
}
function fieldChanged_toggleBox(scriptContext) {
//Different function that triggers on change of a field value, can use this to toggle the checkbox
var currentFieldID = context.fieldId;
console.log('context.fieldId : '+ currentFieldID);
//Check which field is toggled
if(currentFieldID == 'custbody7')
{
//Essentially do the same as above
var recCurrent = context.currentRecord;
var myCheckbox = recCurrent.getValue('custbody7');
if(myCheckbox == true)
{
//get the field and disable it
fld1 = recCurrent.getField('custbody3');
fld1.isDisabled = true;
}
else
{
//Now you need code to enable it as you are toggling the disabling and enabling it realtime
fld1 = recCurrent.getField('custbody3');
fld1.isDisabled = false;
}
}
}
//this is the retrun statement where we declare the script type to implement
return {
pageInit: pageInit_disableMyText,
fieldChanged: fieldChanged_toggleBox
};
});
When I use devtools in Chrome it shows the error on this line:
var recCurrent = context.currentRecord; //This represents your current loaded record, you can get the values of the fields here
But I cannot see what the problem is.
Any help is appreciated.
Thanks!
The parameter name in your function is scriptContext , change it to context
function pageInit_disableMyText(scriptContext) { // <---- Change this parameter to context
Basically you can call it whatever you like, but then you have to use the same variable.

Not able to get Lookup field value in Sharepoint 2013 online Custom List Using SPD

I have one lookup column named "ActivityStatus" and it has two values.
and I just want to check that lookup field value on Presave Action.
Below is my code but it is not working.
<script type="text/javascript">
function PreSaveAction()
{
alert("Inside");
var elm = document.getElementById("ActivityStatus");
alert(elm);
if (elm == "Incomplete" || elm == "Inprogress")
{
document.getElementById("ActivityStatus").style.display='none';
alert("Previous Activity is in Progress...");
return false ;
}
else
{
return true ;
}
}
</script>
The way you are getting lookup value will not work.
you have to do something as below. As it is rendered as a Select tag.
Refer below code :
function PreSaveAction()
{
var elm = document.getElementById("plpace ID your control Here");
var elmValue = elm.options[elm.elmectedIndex].text.trim();
if (elmValue == "Incomplete" || elmValue == "Inprogress")
{
//
// your other code
//
}
else
{
return true ;
}
}
Here in variable selectedValue you will get actual value.
And replace the ID accordingly for your control..

NetSuite SuiteScript 2.0 disable field based on checkbox

I apologize if this is a dumb question, but I am new to NetSuite, and have noticed that their documentation is absolutely ridiculously horrifyingly and atrociously disgusting. All humor and bitterness aside though, I can't find the details that should exists in SuiteAnswers. I can find the type Field or Record, but it doesn't show me the options available for those types. It just shows what methods to call to return a field or record.
So I have it on the fieldChanged event as the training specifies, and below is what I have.
function fieldChanged(context) {
debugger;
var customer = context.currentRecord
if (context.fieldId == 'custentity_apply_coupon') {
var field = record.getField("custentity_apply_coupon");
if (record.getValue("custentity_apply_coupon") == true) {
reord.getField("custentity_coupon_code").isDisabled = false;
}
else{
reord.getField("custentity_coupon_code").isDisabled = true;
}
field.isDisabled = false;
}
}
Turns out that, and I never found this in the documentation, that once you get the field from currentRecord.currentRecord, you can set it to disabled via field.isDisabled. Took me forever to find out that isDisabled was a property of field, and then took a complete guess to see that isDisabled was a get/set call for ClientSide Scripts. Below is the code that ended up working.
function fieldChanged(scriptContext) {
var customer = scriptContext.currentRecord;
if(scriptContext.fieldId == "custentity_sdr_apply_coupon"){
debugger;
var field = customer.getField("custentity_sdr_coupon_code");
field.isDisabled = !customer.getValue(scriptContext.fieldId);
if(field.isDisabled){
customer.setValue(field.id, "");
}
}
}
I hope this will help.
function fieldChanged(context) {
var currentRecord = context.currentRecord;
var approvalChkBox = currentRecord.getValue({
fieldId: 'supervisorapproval'
});
var memoField = currentRecord.getField("memo");
if (approvalChkBox)
memoField.isDisabled = true;
else
memoField.isDisabled = false;
}
Thats a good question, this is the simplest solution you are looking for. use getValue method and isDisabled to meet this requirement. the code is self explanatory. Good Luck.
function fieldChanged(context) {
var record = context.currentRecord;
var fieldname = context.fieldId;
var changedValue = record.getValue(fieldname); //getValue method is the key here
var couponid = record.getField('custentity_kld_coupon_code');
if (fieldname == 'custentity_kld_apply_coupon' && changedValue == true) {
couponid.isDisabled = false; //isDisabled helps you to enable or disable a field
} else {
couponid.isDisabled = true;
}
}
Totally agree. I think the SuiteScript 2.0 Student Guide could've been more helpful if they included a preview of their codes along the way.
For anyone else who is still following along, this code below worked for me. Thanks for everyone else that contributed in this post. Used your codes to create this too. I also included some other codes from the previous exercises (i.e. displaying a message when entering 'x' into the coupon code).
/**
* #NScriptType ClientScript
* #NApiVersion 2.0
*/
define([],
function() {
function fieldChanged (context) {
var customer = context.currentRecord;
if(context.fieldId = 'custentity_sdr_apply_coupon') {
var check = customer.getValue('custentity_sdr_apply_coupon');
var code = customer.getField('custentity_sdr_coupon_code');
if (check == true){
code.isDisabled = false;
} else {
code.isDisabled = true;
}
}
}
function saveRecord(context) {
var customer = context.currentRecord;
var empCode = customer.getValue('custentity_sdr_coupon_code')
if(empCode == 'x') {
alert('Invalid code value. Please try again');
return false;
}
return true;
}
return {
fieldChanged: fieldChanged,
saveRecord: saveRecord,
};
});
var objRec_Curr = scriptContext.currentRecord;
var TransferType = objRec_Curr.getCurrentSublistValue({sublistId:'xxxxxxxxxx', fieldId : 'xxxxxxxxxxxx'});
if(TransferType == 'ABC')
eval("nlapiDisableLineItemField('custpage_sublist_out', 'custpage_out_transfer_location', true)");
else
eval("nlapiDisableLineItemField('custpage_sublist_out', 'custpage_out_transfer_location', false)");

Dropdown field - first item should be blank

Using sharepoint build in lookup column and it set to required field. SharePoint automatically selects the first item in the dropdown box (kinda misleading for the end users).
Is there a way to display blank or Null for the first row of this drop down box?
(I am open to any solution. I prefer javascript type solution)
For Choice fields, the default value is configured in the column settings. If the "Default value" input box is populated, delete the value in order to use no default value.
Edit
For Lookup fields, the field seems to change dramatically if it is required. Fields that are NOT required have a "(None)" value by default. However, toggling the field to required will remove the "(None)" value and the first value is selected automatically.
One thing I found, is that if you use JavaScript to add the null value to the dropdown and then try to press OK you get an error page: "An unexpected error has occurred." As a workaround, I wrote some more code to do a quick validation that the field has a value before the form is submitted. If the field has no value, then it will prompt the user and cancel the submit. (Note: this code is only attached to the OK buttons so you may get errors while editing EditForm.aspx.. just choose a value for your lookup field and you'll be able to edit like normal)
Anyways, onto the code... I think the only line you'll need to change is var fieldTitle = 'Large Lookup Field'; to update it to the name of your field.
<script type="text/javascript">
function GetDropdownByTitle(title) {
var dropdowns = document.getElementsByTagName('select');
for (var i = 0; i < dropdowns.length; i++) {
if (dropdowns[i].title === title) {
return dropdowns[i];
}
}
return null;
}
function GetOKButtons() {
var inputs = document.getElementsByTagName('input');
var len = inputs.length;
var okButtons = [];
for (var i = 0; i < len; i++) {
if (inputs[i].type && inputs[i].type.toLowerCase() === 'button' &&
inputs[i].id && inputs[i].id.indexOf('diidIOSaveItem') >= 0) {
okButtons.push(inputs[i]);
}
}
return okButtons;
}
function AddValueToDropdown(oDropdown, text, value, optionnumber){
var options = oDropdown.options;
var option = document.createElement('OPTION');
option.appendChild(document.createTextNode(text));
option.setAttribute('value',value);
if (typeof(optionnumber) == 'number' && options[optionnumber]) {
oDropdown.insertBefore(option,options[optionnumber]);
}
else {
oDropdown.appendChild(option);
}
oDropdown.options.selectedIndex = 0;
}
function WrapClickEvent(element, newFunction) {
var clickFunc = element.onclick;
element.onclick = function(event){
if (newFunction()) {
clickFunc();
}
};
}
function MyCustomExecuteFunction() {
// find the dropdown
var fieldTitle = 'Large Lookup Field';
var dropdown = GetDropdownByTitle(fieldTitle);
if (null === dropdown) {
alert('Unable to get dropdown');
return;
}
AddValueToDropdown(dropdown, '', '', 0);
// add a custom validate function to the page
var funcValidate = function() {
if (0 === dropdown.selectedIndex) {
alert("Please choose a value for " + fieldTitle + ".");
// require a selection other than the first item (our blank value)
return false;
}
return true;
};
var okButtons = GetOKButtons();
for (var b = 0; b < okButtons.length; b++) {
WrapClickEvent(okButtons[b], funcValidate);
}
}
_spBodyOnLoadFunctionNames.push("MyCustomExecuteFunction");
</script>
In response Kit Menke, I've made a few changes to the code so it will persist previous value of the dropdown. I have added the following lines of code to AddValueToDropdown()....
function AddValueToDropdown(oDropdown, text, value, optionnumber){
var selectedIndex
if (oDropdown.options.selectedIndex)
selectedIndex = oDropdown.options.selectedIndex;
else
selectedIndex = -1;
// original code goes here
// changed last line of code (added "selectedIndex+1")
oDropdown.options.selectedIndex = selectedIndex+1;
}
To improve on top of Aaronster's answer: AddValueToDropdown can be done that way:
var injectedBlankValue = false;
function AddValueToDropdown(oDropdown, text, value, optionnumber) {
for (i = 0; i < oDropdown.options.length; i++) {
option = oDropdown.options[i];
if(option.getAttribute('selected')) // If one is already explicitely selected: we skip the whole process
return;
}
var options = oDropdown.options;
var option = document.createElement('OPTION');
option.appendChild(document.createTextNode(text));
option.setAttribute('value', value);
if (typeof (optionnumber) == 'number' && options[optionnumber]) {
oDropdown.insertBefore(option, options[optionnumber]);
}
else {
oDropdown.appendChild(option);
}
// changed last line of code (added 'selectedIndex+1')
oDropdown.options.selectedIndex = 0;
injectedBlankValue = true;
}
This is needed for document libraries where "add" and "set properties" are two distinct pages.
And funcValidate starts with:
var funcValidate = function () {
if (!injectedBlankValue)
return true;
All these changes is to make the whole thing work with document libraries.

Resources