Solved

Javascript if statement if a field is not null

Posted on 2009-03-31
9
4,934 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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
 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

816 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

8 Experts available now in Live!

Get 1:1 Help Now