The Problem: I want to add a day to a field value, but when I do, I get weird values like "17 Jul 20061". Help!
The Cause: Your Groove script code is probably taking the text from a field and adding a '1' on the end of it. Script code is dumb and does't understand that "17 Jul 2006" is more than just a collection of letters and numbers.
The Solution: You need to use a bit of JScript (JavaScript) code to convert the text to a date, and do the arithmetic on the date.
How to:
- Open the forms Designer
- If you cannot see a tab labelled "Form Scripts", check the "Show Advanced Design Features".
- Choose Add Script.
- Choose Choose New, give your script a name such as "field_changes.js".
- Copy the code below (the weird stuff between the two horizontal lines, dude!) into the script editor window.
- Change all occurrences of ContractDate to the date field you want to use as the starting date for your calculation. You can check what your fields are called by looking at the Fields -> Date Fields part of the Design Object Pane. The name isn't always the same as the label you gave the fields when you created them.
- Change the occurrence of ExpirationDate to the date field you want to calculate.
- Click Ok, then Save then Save To Groove.
- Test it. Put in a date in the first value - the second will change if all is good. If not, see the notes below.
- You're done!
When you change the starting date, the code should run and fill in the second date.
function ContractDate_OnChange() {
try {
var StartDateStr = GetHTMLFieldValue ("ContractDate");
//alert ("StartDateStr:" + StartDateStr);
var StartDateNum = GetApp().ParseDate(StartDateStr, GrooveIntlDateFormatStyle_Short);
//alert ("StartDateNum: " + StartDateNum);
var StartDate = new Date(StartDateNum);
var NextDayOfMonth = StartDate.getDate() +1;
//alert ("nextDayOfMonth:" + NextDayOfMonth);
var NextDate = new Date(StartDate.setDate(NextDayOfMonth));
var NextDateNum = NextDate.valueOf();
var NextDateStr = GetApp().FormatDate(NextDateNum, GrooveIntlDateFormatStyle_Short);
SetHTMLFieldValue ("ExpirationDate", NextDateStr);
}
catch (err) {
GetApp().DisplayError("ContractDate_OnChange: " + err.description);
}
}
How it works:
- Get the html field value we want to start from.
- Convert it to a number.
- Convert the number to a JScript Date object from the number.
- Calculate the next day of the month. Adding 1 to 31 works fine.
- Create a new JScript Date object by adding the next day of the month to the date we started with.
- Get its numeric value.
- Convert it to Text.
- Save it in our second field.
- You're done.
Notes:
- Get Function Names right: Groove automatically links script code onto the forms objects, provided you make sure the function names match exactly what Groove is looking for. Here, our field is called ContractDate and we want to do our stuff when the value changes, so we need ContractDate_OnChange as our function name. The use of capital letters is important - ContractDate_Onchange (notice the little 'c'?) is not going to work.
- JScript Dates are a pain! Working with dates in JScript is an unpleasant experience. Sorry, there's nothing I can do about that apart from point you to Microsoft's own documentation. See the link below.
- Make sure you get the date formats right. On my form the dates are shown in the short date format, so I've used GrooveIntlDateFormatStyle_Short to make sure m script code works to the sam format. If you use a incompatible formats between the code and the form you'll get an error.
- Turn on some messages. If you're having problems with the script giving an error, you can start to work out where and why by un-commenting the 'alert' messages. Remove the // characters and the script will start showing dialog boxes. You can work out how far through you are getting which will hopefully point you towards the answer.
- Still Stuck? Drop me an email - mark.smith@infopatterns.net.
Other Links:
Microsofts documentaion on the JScript Date object. It really prefers you to be using Internet Explorer.