Link to home
Start Free TrialLog in
Avatar of TartanTaurus
TartanTaurusFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Javascript if statement if a field is not null

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

SOLUTION
Avatar of rafael_acc
rafael_acc
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TartanTaurus

ASKER

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;
}
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?
Avatar of HonorGod
Is oppoRecord null?

if ( oppoRecord && oppoRecord.oppo_admissiondate ) {
  // You have an actual value with which to work
}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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!
Accepting the syntax as != '' as the answer I was looking for. Thanks