Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Javascript if statement if a field is not null

Posted on 2009-03-31
9
Medium Priority
?
4,969 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 11

Assisted Solution

by:rafael_acc
rafael_acc earned 120 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 120 total points
ID: 24032478
use this syntax >>> if (oppoRecord.oppo_date != '' )
0
 
LVL 18

Accepted Solution

by:
Morcalavin earned 135 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…

604 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