javascript data getting undefined - javascript

Variable CIMtrek_Attachement_1 getting undefined it is not checking the if condition it moving to this line var selecteditems = CIMtrek_Attachement_1.split("\\");
var CIMtrek_Attachement_1= $("#CIMtrek_RegWhseCo_Attachement_1").val();
alert("CIMtrek_Attachement_1=666666==>> "+CIMtrek_Attachement_1);
if (CIMtrek_Attachement_1 !== null && CIMtrek_Attachement_1 !== "" && CIMtrek_Attachement_1 === undefined)
{
var selecteditems = CIMtrek_Attachement_1.split("\\");
var filename = selecteditems[(selecteditems.length-1)];
alert("selecteditems=666==>> "+selecteditems);
alert("filename=66666==>> "+filename);
nodeField = createNodeField(xmlDoc, new Array("FieldName"), "Record")
nodeField.attributes[0].value = NewFormFields[i]
nodeField.appendChild(xmlDoc.createTextNode(filename));
rootElement.appendChild(nodeField);
}

try typeof(CIMtrek_Attachement_1) === undefined :) and shouldn't it be !== instead of === ?

Related

JavaScript multidimensional-array check if typeof is 'undefined'

If you create a multidimensional-array:
var ThisArray = [];
ThisArray["a"] = [];
ThisArray["a"]["b"] = [];
ThisArray["a"]["b"]["c"] = "This is a string.";
How can you check if ThisArray["a"]["w"]["c"] for example is defined. Right now I'm doing this:
if (typeof ThisArray !== 'undefined') {
if (typeof ThisArray["a"] !== 'undefined') {
if (typeof ThisArray["a"]["w"] !== 'undefined') {
if (typeof ThisArray["a"]["w"]["c"] !== 'undefined') {
// ThisArray["a"]["w"]["c"] is defined!
}
}
}
}
How can I do this better and cleaner?
Use optional chaining:
if (typeof ThisArray?.["a"]?.["w"]?.["c"] !== 'undefined') {
// ThisArray["a"]["w"]["c"] is defined!
}
As noted in the comments, this is a relatively new language feature, and is not supported by older browsers. See Browser Compatibility.
You can use optional chaining.
the optional chaining operator enables simple way to access values through connected objects when it's possible that a reference or function may be undefined or null, hence, protecting you from getting null exception on null/undefined objects.
var ThisArray = [];
ThisArray["a"] = [];
ThisArray["a"]["b"] = [];
ThisArray["a"]["b"]["c"] = "This is a string.";
console.log(ThisArray.a.b.c?.d?.e);
This is a perfect place to use optional chaining!
You can do so like this:
if (typeof ThisArray.a?.b?.c !== 'undefined') {
console.log(`ThisArray["a"]["b"]["c"] is defined!`);
}
Here's a full demo:
var ThisArray = [];
ThisArray["a"] = [];
ThisArray["a"]["b"] = [];
ThisArray["a"]["b"]["c"] = "This is a string.";
if (typeof ThisArray.a?.b?.c !== 'undefined') {
console.log(`ThisArray["a"]["b"]["c"] is defined!`);
console.log(`ThisArray.a.w.c === "${ThisArray.a.b.c}"`)
} else {
console.log(`ThisArray["a"]["b"]["c"] is NOT defined!`);
}
go with try catch
var thirdDimensionValue = null;
try{
thirdDimensionValue = ThisArray["a"]["b"]["c"]
}
catch{
thirdDimensionValue = null
}
if(thirdDimensionValue){
console.log("value exist")
}
else{
console.log("No value exist in the property")
}

Netsuite User Event Script is Getting Empty values for the JavaScript Global Variable

I'm having an JavaScript Global variable called itemId( this itemId is an Object with Key Value Pairs),this Object will store Standard Item Record Details.
I'm able to store the values for this at "User Event Script Before-Load Event" and trying to access the global variable "itemId" at the "Before-Submit Event" but this itemId(Object) is getting empty values.
when is try to get values on this object using the Key it is showing the Error :" :Cannot read property "ItemQuantityTolerance" from undefined"
Code:
SuiteScript Version:2.0
var itemId = new Object();
function beforeLoad(scriptContext) {
try {
var IR_Record = scriptContext.newRecord;
var form = scriptContext.form;
form.clientScriptFileId = 50137;
if (IR_Record != null && IR_Record != '' && IR_Record != undefined) {
var ItemsId = new Array();
var IR_LineItemCount = null;
var lineNumber = null;
IR_LineItemCount = IR_Record.getLineCount('item');
log.debug('Value', 'IR_LineItemCount : ' + IR_LineItemCount);
if (IR_LineItemCount != null && IR_LineItemCount != '' && IR_LineItemCount != undefined) {
for (var i = 0; i < IR_LineItemCount; i++) {
lineNumber = i;
log.debug('Value', 'LineNumber : ' + lineNumber);
var IR_ItemId = IR_Record.getSublistValue({
sublistId: 'item',
fieldId: 'item',
line: i
});
ItemsId[lineNumber] = IR_ItemId;
log.debug('Value', 'IR_ItemId : ' + IR_ItemId);
}
if (ItemsId != null && ItemsId != '' && ItemsId != undefined) {
log.debug('Value:', 'ItemsId:' + ItemsId);
getItemToleranceValues(ItemsId);
}
}
}
} catch (exception) {
log.debug("Value :", "Before-Load Error :", exception.message);
throw exception.message;
}
}
function getItemToleranceValues(ItemsId) {
try {
var ItemDetialsSearch = "";
var ItemSearchResults = "";
var Item_Id = null;
var Item_Name = "";
ItemDetialsSearch = search.create({
type: 'item',
filters: ['internalid', 'anyof', ItemsId],
columns: ['internalid', 'itemid']
});
if (ItemDetialsSearch != null && ItemDetialsSearch != '' && ItemDetialsSearch != undefined) {
ItemSearchResults = ItemDetialsSearch.run().getRange(0, 200);
if (ItemSearchResults != null && ItemSearchResults != '' && ItemSearchResults != undefined) {
for (var i = 0; i < ItemSearchResults.length; i++) {
var itemDetails = new Object();
Item_Id = ItemSearchResults[i].getValue('internalid');
Item_Name = ItemSearchResults[i].getValue('itemid');
if (Item_Name != null && Item_Name != '' && Item_Name != undefined) {
itemDetails['ItemName'] = Item_Name;
}
itemId[Item_Id] = itemDetails;
}
log.debug("itemId:", itemId);
}
}
} catch (exception) {
log.debug("Value:", "Item-Tolerance-Value Error:" + exception.message);
throw exception.message;
}
}
function beforeSubmit(scriptContext) {
try {
var IR_Record = scriptContext.newRecord;
var itemName = "";
if (IR_Record != null && IR_Record != '' && IR_Record != undefined) {
if (itemId != null && itemId != '' && itemId != undefined) {
log.debug("itemId:", itemId);
itemName = itemId[IR_ItemId]['ItemName']; // Getting Empty Values at this point, eventhough it is global variable with values
log.debug('Value', 'itemName : ' + itemName);
}
}
}catch(exception){
throw exception.message;
}
Example Values for the itemId object:
{
337: {
ItemName: "Apple",
},
359: {
ItemName: "Orange"
}
}
how to access the global Variable itemId get the value using it property "ItemName".
thanks in advance.
You are mis-understanding the script execution lifecycle. On the server side the before load, before submit and after submit executions are all completely separate. It looks like the only place you are loading itemId is via the call to getItemToleranceValues in your beforeLoad script but you are accessing the empty value in your beforeSubmit script.
If there is some reason to load the value in your beforeLoad script you could cache the value in a session variable and then retrieve it in your beforeSubmit script. See Session.get/set in the N/runtime module

Only include a variable in an if statement, if another variable exists?

The following function is meant to check to see if a custom Date widget (javascript) is empty or not. Problem is, there are many variations of this widget where M/D/Y fields display, or it could be M/D or, M/Y.
Of course, I could hard code all the combinations as if checks, but is there a better way of saying "there are 3 possible nodes, that might have values...if x out of 3 nodes exist AND they all have values, set empty to false."
checkIfEmpty: function () {
var empty = true;
var mNode = this.getNode('month');
var month = mNode ? mNode.value : null;
var dNode = this.getNode('day');
var day = dNode ? dNode.value : null;
var yNode = this.getNode('year');
var year = yNode ? yNode.value : null;
if (month && day && year) {
empty = false;
}
return empty;
}
checkIfEmpty: function () {
var empty = true;
var dateParts = [];
var mNode = this.getNode('month');
if(mNode && mNode.value){
dateParts.push('month');
}
var dNode = this.getNode('day');
if(dNode && dNode.value){
dateParts.push('day');
}
var yNode = this.getNode('year');
if(yNode && yNode.value){
dateParts.push('year');
}
if (dateParts.length) {
empty = false;
}
return empty;
}
You can add to see if the node does not exist
if ( (!mNode || month) && (!dNode || day) && (!yNode || year) ) {
checkIfEmpty: function () {
var empty = false;
var mNode = this.getNode('month');
if(mNode && !mNode.value) {
empty = true;
}
var dNode = this.getNode('day');
if(dNode && !dNode.value) {
empty = true;
}
var yNode = this.getNode('year');
if(yNode && !yNode.value) {
empty = true;
}
return empty;
}
Trying to solve my own question - so far, this is the most efficient way of achieving what I am trying to do. Anyone, suggestions on how to make it even more efficient?
If the value property exists for all valid nodes then:
if(mNode && dNode && yNode){
empty = false;
}
Otherwise:
if(mNode && mNode.value && dNode && dNode.value && yNode && yNode.value){
empty = false;
}
I am not sure if I followed, but if you need that at least one to be true so empty is false then:
if(mNode || dNode || yNode) {
empty = false;
}
Again, if the value property is not standard for all nodes:
if((mNode && mNode.value) || (dNode && dNode.value) || (yNode && yNode.value)){
empty = false;
}
I think it's clearer if you think about it this way:
If (node && node.value) returns a truthy value then the date property exists otherwise the date property doesn't exist.

JavaScript true false not executing correctly in if else statement

For some reason when I'm using the true/false values and checking if at least one of the values is true, the if/else statement is not working correctly.
I have this:
$scope.checkValues = function (qId) {
var airport = $scope.airports[0].questID;
var destAirport = $scope.destAirports[0].questID;
var airportVal = isFalseOrUndefined($scope.answers[airport]);
var destAirportVal = isFalseOrUndefined($scope.answers[destAirport])
if (airportVal == false || destAirportVal == false) {
$surveyNav.skipPage = true;
}
}
function isFalseOrUndefined(val) {
if(val == null || val === false) {
return true;
} else {
return false;
}
}
In this image below, as you can see the value for airportVal is true, the other value for destAirportVal in that same scenario is true, but I'm still able to get correctly in to the if condition and set the scope value.
Does anyone see any issue?
You should be using === and !== operators when checking for equality in Javascript.
Javascript Comparison and Logical operators
op1 === op2 - Will check if op1 is explicitly equal to op2
op1 !== op2 - Will check if op1 is not explicitly equal to op2
Also: you can condense you isFalseOrUndefined function
Note 1: you are not actually checking if val is undefined.
To check if something is undefined: typeof val === 'undefined'
This is different than checking if a variable is null
Note 2: Keep in mind that your variables are not entirely clear here. airportVal will be equal to true when $scope.answers[airport] is false or null. Is this your intention?
$scope.checkValues = function (qId) {
var airport = $scope.airports[0].questID;
var destAirport = $scope.destAirports[0].questID;
var airportVal = isFalseOrUndefined($scope.answers[airport]);
var destAirportVal = isFalseOrUndefined($scope.answers[destAirport])
if (airportVal === false || destAirportVal === false) {
$surveyNav.skipPage = true;
}
}
function isFalseOrUndefined(val) {
return (val === null || val === false);
}
Your function should probably do what it claims to do:
function isFalseOrUndefined(val) {
return typeof val === 'undefined' || val === false || val === null || val === ''/* add this if you think it should be falsy*/;
}
But then, testing for !val should be sufficient:
$scope.checkValues = function (qId) {
var airport = $scope.airports[0].questID;
var destAirport = $scope.destAirports[0].questID;
if (!airport || !destAirport) {
$surveyNav.skipPage = true;
}
}

What is expected from a SlickGrid editor factory?

Should the return value of SomeEditorFactory.getEditor(column) be a function or an object?
From the code
function getEditor(row, cell) {
var column = columns[cell];
var rowMetadata = data.getItemMetadata && data.getItemMetadata(row);
var columnMetadata = rowMetadata && rowMetadata.columns;
if (columnMetadata && columnMetadata[column.id] && columnMetadata[column.id].editor !== undefined) {
return columnMetadata[column.id].editor;
}
if (columnMetadata && columnMetadata[cell] && columnMetadata[cell].editor !== undefined) {
return columnMetadata[cell].editor;
}
return column.editor || (options.editorFactory && options.editorFactory.getEditor(column));
}
Not immediately obvious.
It should be an object that implements getEditor(columnDefinition) and returns the editor for a given column.
See https://github.com/mleibman/SlickGrid/wiki/Writing-custom-cell-editors and https://github.com/mleibman/SlickGrid/blob/master/slick.editors.js.
When SlickGrid calls YourEditorFactory.getEditor(column), it expects a function.

Categories