Solved

Javascript if statement if a field is not null

Posted on 2009-03-31
9
4,925 Views
Last Modified: 2012-05-06
Hi,

This is probably very simple or my logic is possibly incorrect! What I want to do is to say:

If a field is not Null (contains any data) then fire the operation.

The line i need help with is * if (oppoRecord.oppo_date!=="") *

Clearly that's not correct I've looked into possibly using the length property to say if length > 0 then do operation but again my coding lets me down!

Thanks
function UpdateRecord()
 

{
 

var oppoRecord = eWare.FindRecord("Opportunity",WhereClause); 
 

if (oppoRecord.oppo_date!=="")

{

Values("oppo_customerref")=oppoRecord.oppo_opportunityid; 

}
 

}

Open in new window

0
Comment
Question by:TartanTaurus
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 11

Assisted Solution

by:rafael_acc
rafael_acc earned 40 total points
ID: 24031747
it's "!=" not "!==!"

In javascript, if my memory is not failing me, you can also use i (opoRecord.opo_date != null)
0
 
LVL 1

Author Comment

by:TartanTaurus
ID: 24031928
I've tried the following combinations:

if (oppoRecord.oppo_admissiondate !='')
if (oppoRecord.oppo_admissiondate !="")
if (oppoRecord.oppo_admissiondate !=null)

None work the line below still fires
{
Values("oppo_customerref")=oppoRecord.oppo_opportunityid;
}
0
 
LVL 11

Expert Comment

by:rafael_acc
ID: 24031982
I think your problem lies somewhere else ...
Instead of doing the conditional test, replace the comand with:

alert(oppoRecord.oppo_admissiondate);

if this object is valid, you should really get either "[object]" in a popup, or the value of the variable at that time. You shouldn't get an error - if you do get an error, what does the error message say?
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24032051
Is oppoRecord null?

if ( oppoRecord && oppoRecord.oppo_admissiondate ) {
  // You have an actual value with which to work
}
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 9

Assisted Solution

by:Ramanhp
Ramanhp earned 40 total points
ID: 24032478
use this syntax >>> if (oppoRecord.oppo_date != '' )
0
 
LVL 18

Accepted Solution

by:
Morcalavin earned 45 total points
ID: 24032697
It depends on what you mean by 'null' and if you are wanted to do type coercion or not.

var foo = ''; //this is not null, this is an empty string

var foo;  //this is not null, this is undefined

var foo = null;  //this is null
 

var foo = null;

if(foo != '') {

//this code would run.  foo is null, not an empty string

}
 

var foo;

if(foo != '') {

//again, this code runs.  foo is undefined, not an empty string.

}
 

var foo = '';

if(foo != '') {

//this doesn't run.  foo is an empty string.

}
 

var foo;

if(foo != null) {

//this code does NOT run.  != and == do type coercion, so undefined and null are the same

} 
 

var foo;

if(foo !== null) {

//this code runs.  !== and === do NOT do type coercion, so null and undefined are not the same

}
 

var foo = 1

if(foo != true) {

//this code does not run. != and == do type coercion, so 1 and true are the same

}
 

var foo = 1

if(foo !== true) {

//this code does run.  !== and === do NOT do type coercion, so 1 and true are not the same.

}

Open in new window

0
 
LVL 18

Expert Comment

by:Morcalavin
ID: 24032761
Forgot a  few.  Make sure you know exactly what you want to test and the type of object is should be when you test it.

var foo;

if(!foo) {

//runs foo is undefined

}
 

var foo = null;

if(!foo) {

//runs foo is null

}
 

var foo = false;

if(!foo) {

//runs foo is false

}
 

var foo = 0;

if(!foo) {

//runs foo is 0(type coercion!  is this what you REALLY wanted)?

}
 

var foo = '';

if(!foo) {

/runs.  foo is an empty string(type coercion!  is this what you REALLY wanted)?

}

Open in new window

0
 
LVL 1

Author Comment

by:TartanTaurus
ID: 24033425
Ok the syntax if (oppoRecord.oppo_stage !== 'UnderReview') works fine.

The problem clearly lies with the oppo_admissiondate field. This field is part of an active record as the script only serves to "update" an existing record. In the SQL query analyzer the field shows as 'NULL'. That is nothing has ever been entered into it, maybe that's the problem.

It runs ok with all fields that contain text. The line above works fine. I will accept multiple solutions for all who responded as such.

thanks very much!
0
 
LVL 1

Author Closing Comment

by:TartanTaurus
ID: 31564964
Accepting the syntax as != '' as the answer I was looking for. Thanks
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now