You need to sign in to do that
Don't have an account?

How to compare Date fields with Today's date in an S-Control and calculate the difference in days.
I want to create an S-Control which retrieves project milestones (dates) from an asset and compares these dates with today's date or "Today - x days". But the following code does not work.
Code:
Code:
var ident= sforce.connection.query("SELECT Id, PurchaseDate , Delivery_Date__c, InstallDate, Acceptance_Date__c, Warranty_End_Date__c, ProjectState FROM Asset WHERE Type__c IN ('SIS' , 'WIS') AND Asset_Autostatus__c NOT IN ('Out of Operation' , 'Out of Warranty' )"); var irecords = ident.getArray("records");The first problem is the script fails at {IToday} and the second is that Today will be returned in european dd.mm.yyyy format while project milestones are returned in yyyy-mm-dd format.
int i=0;
while (i<300) { if (irecords[i].Warranty_End_Date__c > {!Today}) {irecords[i].ProjectState = "Out of warranty";} if (Acceptance_Date__c != null AND InstallDate < {!Today}-60) {irecords[i].ProjectState = "Factory Acceptance overdue";} i++;}
Message Edited by FGNY on 11-26-2007 01:57 AM
Your Javascript is failing because of syntax errors once the value for {Today} is substituted.
A. Firstly, I haven't checked, but I'm not sure there *is* a substitution for {!Today}??? I know there is one
for {!$System.OriginDateTime}, but I think the Today() functions is for Functions in Salesforce Formulas and not for S-Control substitution... I may be wrong.
B. Assuming you were correct for A, all Salesforce does when creating the page that your browser runs to run the s-control is to blithly substitute in the string you've requested *wherever* you requested it. Regardless of syntax. So, if I
were to write if ("Steve" == {!Contact.Firstname}) this would translate to:
if ("Steve" == Steve) which would be a syntax problem.
C. I don't know your dataset, but rather than query all the Assets and going through them yourself, might you just change the query to salesforce to include the criteria ... and ((Warranty_End_Date__c > TODAY) or (Acceptance_Date_C != null and InstallDate < LAST_N_DAYS:60))
Then you can cycle through the results, knowing that they all need an update, you just don't know which one. Just saves on the number of records retrieved. Since the ProjectState is mutually exclusive your code can then read:
while(...){
if (irecords[i].Warranty_End_Date__c > "{!$System.OriginDateTime}") {
....projectState = "out of warranty";
} else {
...projectState = "Factory....";
}
Instead of using {!$System...} you could build your own string in whatever format you want with Javascript Date().
D. I won't even go into the question of using a workflow or an Apex trigger. ;-)
Best, Hope my late night ramblings help, Steve.
I also can't use the query to filter those assets out since there are additional conditions to be checked dependent on the previous condition:
The whole idea behind this S-Control is to check if project milestones were not updated by the Project Manager (i.e. Install date should be within 2 Months after delivery date, and acceptance not longer then 2 months after install) and then create a task for this Project Manager to remind him to check the milestones.
Here is the whole code:
Code: However there is also an additional problem with the script in the createTask function. Apparently only a small mistake but I don't see it. Can somebody help
Message Edited by FGNY on 11-27-2007 09:54 AM
OriginDateTime - A global merge field that represents the literal value of 1900-01-01 00:00:00. Use this global variable when performing date/time offset calculations or to assign a literal value to a date/time field.
This will only lead to headache if you attempt to use it to determine if your warranties are out of date.
Cheers,
See, that's what happens when it's late and you write things without looking them up to be sure!
Mea Culpa, -Steve
For those who have the same problem, here's the code:
In the file: soap/ajax/9.0/connection.js which you're including already, Salesforce provides a function: