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
HarmpieHarmpie 

Date field best practises S-control

Could someone give me some best practises + samples on how to deal with date fields in S-Controls?
  • How to select a date value using SOQL
  • How to parse a date retrieved by merge fields i.e. ({!Opportunity.CloseDate}
  • How to re-use a selected date from either SOQL or mergefield in a new query
  • What javascript files to include?
sfdcfoxsfdcfox
To locate a date value, use an ISO-formatted date or 'date literal' (as defined in the API docs). Check the docs for all of the date literals.
 
For example:
 
Code:
select id from opportunity where closedate = 2007-01-20

select id from opportunity where closedate = LAST_N_DAYS:15

 To use a date field from a merge field, you should probably use the following:
 
Code:
myDate = new Date("{!Opportunity.CloseDate}")

This will get the date into a JavaScript object, which allows you use getMonth(), getDaygetYear(), etc.
 
To make a date into a SOQL-compatible date, use the sforce.internal.dateToString() command, as follows:
 
Code:
sforce.connection.query(
  "select id from opportunity where closedate = " +
  sforce.internal.dateToString(new Date(queryResults.getArray("records")[0]["Date_Field__c"])) +
  " and stagename = 'Closed Won'")

 
The only javascript file you should need to include ordinarily is the AJAX Toolkit; the link for this file is "/soap/ajax/10.0/connection.js". You can also include the Dojo Toolkit if you need it, or any other files from the Salesforce server or from a remote server.
 
~ sfdcfox ~
 

 
HarmpieHarmpie
Thanks! You perfectly understood my question. This will be very helpful ..
HarmpieHarmpie

Just 1 question though, regarding

Code:

myDate = new Date("{!Opportunity.CloseDate}")


 
is this 100% fullproof ? Isnt the format of the mergefield determined by locale of the user and thus very unconsistant ?

HarmpieHarmpie
Decided simply to test my last question and it seems I was right. The aproach works when a user has US date format (yyyy-mm-dd), but breaks (Invalid Date) with the Dutch date format (dd-mm-yyyy). Does this mean I need to string-juggle myself or is there a better solution? (Except querying for the date which returns ISO format)
HarmpieHarmpie
Code:
var getDateResult = sforce.connection.retrieve("Terminated_per__c","Opportunity",[thisOppId]);

thisOppEndDate = getDateResult[0].Terminated_per__c;

if(confirm("Are you sure?")) {
 
 if(thisOppEndDate != ''){
  var tempDate = new String(thisOppEndDate).split("-");
  
  var startDag = tempDate[2];
  var startMaand = tempDate[1];
  var startJaar = tempDate[0];
  
  var queryDate = formatDate(new Date(parseInt(startJaar),parseInt(startMaand-1),parseInt(startDag)),'yyyy-MM-dd');

 
Some more feedback. It seems the new Date() variant breaks in a lot of situations and is far from robust. So far the only way that I can get robust performance is using the code above. And I don't like it :) But it works
 
sfdcfoxsfdcfox
You don't need to do that, either; you can use formulas in your code, so this is an acceptable alternative:
Code:
myDate = new Date({!Year(CloseDate)},{!Month(CloseDate)},{!Day(CloseDate)})
This will cause Salesforce to create the correct date format for javascript automatically. So you don't need to do that query anyways!

~ sfdcfox ~
 

knicholsknichols
This always returns a month later than what it should.  For example, if the field I'm reading in is '9/18/2007' after I parse the string it and display an alert it shows everything correct except for the month which has been bumped up to October.  Any ideas?

sfdcfoxsfdcfox
Doh. I forgot, JavaScript expects 0-11, Salesforce outputs 1-12 (the normal meaning of a month). Try this:

myDate = new Date({!Year(CloseDate)},{!Month(CloseDate)-1},{!Day(CloseDate)})
Also, it should be noted that the Date({!CloseDate}) syntax "normally" works (that is, users that use mm/dd/yyyy normally have their computer locale set to the same, and dd/mm/yyyy will have their computer locale set to the same). You have to actually "go out of the way" to break it, since most users would have their Salesforce locale and computer locale set to the same values.

~ sfdcfox ~
knicholsknichols
Thanks for the quick response!  That worked.
gmc74gmc74
Hi,
 
I am trying to reformat a date in an s-control that I have created using the ajax toolkit. 
 
The date format for the LastMidifiedDate field below is 2008-03-04T22:53:24.000Z, GMT
 
I would like to format it MM/DD/YYYY, HH:MM for the user's local time, or at the very least to EST, but I can't seem to figure it out.  Can someone point me in the correct direction?
 
Thanks,
 
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 Name, LastModifiedDate, Support_In_Out__c FROM User where Support_In_Out__c = 'In Office' Order By Name", callback);
}

function displayResult(result) {
var it = new sforce.QueryResultIterator(result);
var html = [];
html.push("<table border width=90%>");
html.push("<tr><td>");
html.push("<th colspan=6>In Office</th></tr></td>");
html.push("<th>Name</th>");
html.push("<th>Last Update</th>");
html.push("<th>Status</th>");
while(it.hasNext()) {
var record = it.next();

html.push("<tr Align=center>");
html.push("<td>" + record.Name + "</td>");
html.push("<td>" + record.LastModifiedDate + "</td>");
html.push("<td>" + record.Support_In_Out__c + "</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>