function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
gmc74gmc74 

How do I add an If statement to this S-Control?

I have a simple S-Control that shows some HTML on a screen.  I want to add an if statement to this where if the value returned is 'In Office' (with out quotes of course) then run as it is, if it is anything else, then I want to change the color of the font. I know the HTML, but I can't get the if statement to work.  Any ideas?

Code:
<html>
<head>
<script src="/soap/ajax/10.0/connection.js"></script>
<script src="/js/dojo/0.4.1/dojo.js"></script>

<script>
  dojo.addOnLoad(init);


function init() {
  var callback = {
    onSuccess : displayResult,
    onFailure : displayError
  };
sforce.connection.query("SELECT Support_In_Out__c FROM User where ID  = '{!Case.Route_To_UserId__c}'", callback);
}



function displayResult(result) {
  var it = new sforce.QueryResultIterator(result);
  var html = [];
  while(it.hasNext()) {
    var record = it.next();
    html.push("<td> This Account Manager is listed as <b>" + record.Support_In_Out__c + "</b></td>");
    html.push("</tr>");
    
  }

  document.getElementById("output-div").innerHTML = html.join("");
}

function displayError(error) {
  document.getElementById("output-div").innerHTML =
    "oops something went wrong ... " + error;
}
</script>


</head>
<body>

  <div id="output-div"></div>


</body>
</html>

 

WhyserWhyser
Maybe try something like this?
 
    html.push("<tr>");
  
    // Do an if statement (using the "?" to test for a condition) within the push statement
    html.push( record.Support_In_Out__c == "In Office" ? "<td>" : "<td style='color:red'>" );
    html.push( "This Account Manager is listed as <b>" + record.Support_In_Out__c + "</b></td>");

    html.push("</tr>");


Message Edited by Whyser on 06-02-2008 11:05 AM
gmc74gmc74
That didn't seem to have an affect, thanks for the post though
WhyserWhyser
Am I even on the right track? Your question is not entirely clear. What variable do you want to run the condition "In Office" against? Is it the record.Support_In_Out__c field?? I'm only assuming this field because that's the only one where you're displaying HTML.
 
also the <td style = 'color:red'>  (or whatever color of your choice) should work, the only way it would not have an effect is if you had ALL your records returned as "In Office", or else the spelling is off (check for upper/lower cases). You stated you only wanted to change the color if the value (for whatever variable you wanted) was NOT "In Office".
gmc74gmc74
I have never tried to use the type of if that you were using, so assuming that it is correct formatting, then yes.
 
Basically, if that field that I am returning (Support_In_Out__c) = In Office, then the following line should be
 
Code:
  html.push("<td> This Account Manager is listed as <b>" + record.Support_In_Out__c + "</b></td>");

 

If it is anything else, then this line
 
Code:
  html.push("<td><font color="red"> This Account Manager is listed as <b>" + record.Support_In_Out__c + "</font></b></td>");

 


 
WhyserWhyser
well you can do it both ways then
Code:
if ( record.Support_In_Out__c == "In Office" )
{
  html.push("<td> This Account Manager is listed as <b>" + record.Support_In_Out__c + "</b></td>");
}
else
{
  html.push("<td style='color:red'>This Account Manager is listed as <b>" + record.Support_In_Out__c + "</b></td>");
}

 or you can do it the other way I showed
 
Code:
var tempHTML;
// Use the field to choose the color of the cell
tempHTML = record.Support_In_Out__c == "In Office" ? "<td>" : "<td style='color:red'>";
tempHTML += "This Account Manager is listed as <b>" + record.Support_In_Out__c + "</b></td>";

html.push( tempHTML );

 The "if" statement I showed you is a short form method. The syntax is
 
[expression] ? [value/expression if true] : [value/expression if false] ;
In either case, both methods above should work as you have asked. Unless I am missing something completely!
 


Message Edited by Whyser on 06-02-2008 01:59 PM
gmc74gmc74

Tried the first method -

Code:
<html>
<head>
<script src="/soap/ajax/10.0/connection.js"></script>
<script src="/js/dojo/0.4.1/dojo.js"></script>

<script>
  dojo.addOnLoad(init);


function init() {
  var callback = {
    onSuccess : displayResult,
    onFailure : displayError
  };
sforce.connection.query("SELECT Support_In_Out__c FROM User where ID  = '{!Case.Route_To_UserId__c}'", callback);
}



function displayResult(result) {
  var it = new sforce.QueryResultIterator(result);
  var html = [];
  while(it.hasNext()) {
    var record = it.next();
var tempHTML;// Use the field to choose the color of the cell
tempHTML = record.Support_In_Out__c == "In Office" — "<td>" : "<td style='color:red'>";
tempHTML += "This Account Manager is listed as <b>" + record.Support_In_Out__c + "</b></td>";
html.push( tempHTML );
html.push("</tr>");
    
  }

  document.getElementById("output-div").innerHTML = html.join("");
}

function displayError(error) {
  document.getElementById("output-div").innerHTML =
    "oops something went wrong ... " + error;
}
</script>


</head>
<body>

  <div id="output-div"></div>


</body>
</html>


Both In Office and Out of Office showed up in Black 

The second option worked, thank you for the help.  I am not sure what the heck I did originally, I thought I had done it just like that but it didn't work.  Oh well, thanks for the assistance!

 

Code:
<html>
<head>
<script src="/soap/ajax/10.0/connection.js"></script>
<script src="/js/dojo/0.4.1/dojo.js"></script>

<script>
  dojo.addOnLoad(init);


function init() {
  var callback = {
    onSuccess : displayResult,
    onFailure : displayError
  };
sforce.connection.query("SELECT Support_In_Out__c FROM User where ID  = '{!Case.Route_To_UserId__c}'", callback);
}



function displayResult(result) {
  var it = new sforce.QueryResultIterator(result);
  var html = [];
  while(it.hasNext()) {
    var record = it.next();
if ( record.Support_In_Out__c == "In Office" )
{
  html.push("<td> This Account Manager is listed as <b>" + record.Support_In_Out__c + "</b></td>");
}
else
{
    html.push("<td> <font color=red>This Account Manager is listed as <b>" + record.Support_In_Out__c + "</font></b></td>");
}
    html.push("</tr>");

    
  }

  document.getElementById("output-div").innerHTML = html.join("");
}

function displayError(error) {
  document.getElementById("output-div").innerHTML =
    "oops something went wrong ... " + error;
}
</script>


</head>
<body>

  <div id="output-div"></div>


</body>
</html>