Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if then statements in javascripts ????

Status
Not open for further replies.

zishan876

Programmer
Mar 19, 2007
61
US
Hi I am new to javascript .. I am trying to break a query up into three sections and display the results when the output equals the number assigned.. I do not know what am I doing incorrectlt... Please see code below:
Code:
<html>
<head>
<script type="text/javascript" src="/soap/ajax/10.0/connection.js"></script>
<script type="text/javascript">
window.onload=init_page;
function init_page()
{
var j= "";
var output="";

strSQL="Select Id,AS400_Account_Number__c,Name,BillingCity,SF_DATE_ON_SERVICE__c,SF_DATE_OFF_SERVICE__c,SF_OFF_SERVICE_DESC__c from Account " +
strSQL = strSQL + "where OwnerId='00550000000mLCX'" +
if (output==1)
{
alert("bbb");
strSQL=strSQL + "and SF_DATE_ON_SERVICE__c = LAST_N_DAYS:30";
else (output==2)
strSQL=strSQL + "and SF_OFF_SERVICE_CODE__c<>'9' and SF_DATE_OFF_SERVICE__c = LAST_N_DAYS:30";
else (output==3)
strSQL = strSQL + "and SF_OFF_SERVICE_CODE__c='9' and SF_DATE_OFF_SERVICE__c = LAST_N_DAYS:30";
}

//SF_DATE_ON_SERVICE__c = LAST_N_DAYS:30and SF_OFF_SERVICE_CODE__c<>'9' and SF_DATE_OFF_SERVICE__c = LAST_N_DAYS:30";

var result = sforce.connection.query(strSQL);
var records = result.getArray("records");
output==1; //<over here I state the first output should display the query should be the entire strsql plus the where clause assigned
for (var i=0; i<records.length; i++)
{
j += '<a href="/' + records[i].Id + ' "target=_blank"">' + <note sfid= {records[i].Id}><id>{i+1}</id><date>{records[i].SF_DATE_ON_SERVICE__c}</date><acct>{records[i].AS400_Account_Number__c}</acct><name>{records[i].Name}</name><city>{records[i].BillingCity}</city></note> + '</a><br>';
}
document.getElementById('div_tag').innerHTML = j;
}
}
</script>
</head>
<body bgcolor="#F3F3EC">
<font size="2" face="Verdana">
<div id="div_tag">No Accts</div>
</font>
</body>
</html>
Thanks
Shan
 
try

Code:
if (output==1)
  strSQL=strSQL + "and SF_DATE_ON_SERVICE__c = LAST_N_DAYS:30";
else if (output==2)
  strSQL=strSQL + "and SF_OFF_SERVICE_CODE__c<>'9' and SF_DATE_OFF_SERVICE__c = LAST_N_DAYS:30";
else if (output==3)
  strSQL = strSQL + "and SF_OFF_SERVICE_CODE__c='9' and SF_DATE_OFF_SERVICE__c = LAST_N_DAYS:30";

you can only have one ELSE for each IF. Alternatively use the switch statement

Code:
switch (output) {
  case 1:
    strSQL=strSQL + "and SF_DATE_ON_SERVICE__c = LAST_N_DAYS:30";
    break
  case 2:
    strSQL=strSQL + "and SF_OFF_SERVICE_CODE__c<>'9' and SF_DATE_OFF_SERVICE__c = LAST_N_DAYS:30";
    break;
  case 3:
    strSQL = strSQL + "and SF_OFF_SERVICE_CODE__c='9' and SF_DATE_OFF_SERVICE__c = LAST_N_DAYS:30";
    break;
}
 
Thank you so much... so I did place thre switch statement in but I get nothing to display when there should be 5 records to display when output==1...
here is my code:
Code:
<html>
<head>
<script type="text/javascript" src="/soap/ajax/10.0/connection.js"></script>
<script type="text/javascript">
window.onload=init_page;
function init_page()
{
var j= "";
var output="";

strSQL="Select Id,AS400_Account_Number__c,Name,BillingCity,SF_DATE_ON_SERVICE__c,SF_DATE_OFF_SERVICE__c,SF_OFF_SERVICE_DESC__c from Account where OwnerId='00550000000mLCX' " +
switch (output)
{
case 1:
strSQL=strSQL + "and SF_DATE_ON_SERVICE__c = LAST_N_DAYS:30";
break;
case 2:
strSQL=strSQL + "and SF_OFF_SERVICE_CODE__c<>'9' and SF_DATE_OFF_SERVICE__c = LAST_N_DAYS:30";
break;
case 3:
strSQL = strSQL + "and SF_OFF_SERVICE_CODE__c='9' and SF_DATE_OFF_SERVICE__c = LAST_N_DAYS:30";
break;
}
//SF_DATE_ON_SERVICE__c = LAST_N_DAYS:30and SF_OFF_SERVICE_CODE__c<>'9' and SF_DATE_OFF_SERVICE__c = LAST_N_DAYS:30";

var result = sforce.connection.query(strSQL);
var records = result.getArray("records");
output==1;
for (var i=0; i<records.length; i++)
{
j += '<a href="/' + records[i].Id + ' "target=_blank"">' + <note sfid= {records[i].Id}><id>{i+1}</id><date>{records[i].SF_DATE_ON_SERVICE__c}</date><acct>{records[i].AS400_Account_Number__c}</acct><name>{records[i].Name}</name><city>{records[i].BillingCity}</city></note> + '</a><br>';
}
document.getElementById('div_tag').innerHTML = j;
}
}
</script>
</head>
<body bgcolor="#F3F3EC">
<font size="2" face="Verdana">
<div id="div_tag">No Accts</div>
</font>
</body>
</html>
Thanks
 
Switch is a good solution, but I almost always have a default option so my script won't break if I don't get an expected value.

Like So:

Code:
switch (output)
{
case 1:
strSQL=strSQL + "and SF_DATE_ON_SERVICE__c = LAST_N_DAYS:30";
break;
case 2:
strSQL=strSQL + "and SF_OFF_SERVICE_CODE__c<>'9' and SF_DATE_OFF_SERVICE__c = LAST_N_DAYS:30";
break;
case 3:
strSQL = strSQL + "and SF_OFF_SERVICE_CODE__c='9' and SF_DATE_OFF_SERVICE__c = LAST_N_DAYS:30";
break;

default:
     some code here;

}

Even if you think you will always know what you are switching, a default option will never hurt.

Just my 2 cents.



LJ Wilson

My personal saying - Just remember, it can always get worse, and usually will.
 
Even if you think you will always know what you are switching, a default option will never hurt.

Case and Point:

thread216-1457266

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top