Answered by:
MS CRM 2011 Javascript ( Set a datetime field to current date )

Question
-
I need to use in the form’s OnLoad() event to set the default value to the current date in a datetime field on Microsoft CRM 2011, any advice ???
Thanks
Tuesday, August 16, 2011 5:31 AM
Answers
-
You can upload this as a web resource if you want and then in the form editor GUI in form properties you can
attach this function to the form onload event. This set's the date to one day in the future, but you get the idea.Set the value of a Date field:Note: this example sets the Birthday field on the Contact form to 1 day from todayfunction SetDateField() { var BirthDate = Xrm.Page.data.entity.attributes.get("birthdate"); var today = new Date(); var futureDate = new Date(today.setDate(today.getDate() + 1)); BirthDate.setValue(futureDate); }
Jamie Miley
Check out my about.me profile!
http://mileyja.blogspot.com
Linked-In Profile
Follow Me on Twitter!- Proposed as answer by Jamie MileyModerator Tuesday, August 16, 2011 3:08 PM
- Marked as answer by Jamie MileyModerator Sunday, April 1, 2012 8:23 PM
Tuesday, August 16, 2011 3:07 PMModerator -
Its not a nonsense since the crmForm will be deprecated in future you should use the Xrm JS API.
Here's the one-liner equivalent to your code:
Xrm.Page.getAttribute("birthdate").setValue(new Date());
regards
Uli
- Proposed as answer by Ulrich Hüttinger Monday, January 30, 2012 10:05 AM
- Marked as answer by Jamie MileyModerator Sunday, April 1, 2012 8:23 PM
Monday, January 30, 2012 10:05 AM
All replies
-
You can upload this as a web resource if you want and then in the form editor GUI in form properties you can
attach this function to the form onload event. This set's the date to one day in the future, but you get the idea.Set the value of a Date field:Note: this example sets the Birthday field on the Contact form to 1 day from todayfunction SetDateField() { var BirthDate = Xrm.Page.data.entity.attributes.get("birthdate"); var today = new Date(); var futureDate = new Date(today.setDate(today.getDate() + 1)); BirthDate.setValue(futureDate); }
Jamie Miley
Check out my about.me profile!
http://mileyja.blogspot.com
Linked-In Profile
Follow Me on Twitter!- Proposed as answer by Jamie MileyModerator Tuesday, August 16, 2011 3:08 PM
- Marked as answer by Jamie MileyModerator Sunday, April 1, 2012 8:23 PM
Tuesday, August 16, 2011 3:07 PMModerator -
Dont listen to that nonsense , One line of code is all it takes
crmForm.all.new_yourfieldname.DataValue = new Date();
Thursday, January 19, 2012 4:50 PM -
Its not a nonsense since the crmForm will be deprecated in future you should use the Xrm JS API.
Here's the one-liner equivalent to your code:
Xrm.Page.getAttribute("birthdate").setValue(new Date());
regards
Uli
- Proposed as answer by Ulrich Hüttinger Monday, January 30, 2012 10:05 AM
- Marked as answer by Jamie MileyModerator Sunday, April 1, 2012 8:23 PM
Monday, January 30, 2012 10:05 AM -
Your Code is of crm 4.0, upgrade it pleaseThursday, October 11, 2012 12:48 PM
-
Hi,
please use the following code
Xrm.Page.data.entity.attributes.get("new_date1").setValue(new Date());
- Proposed as answer by meenakshi Patnala Monday, March 11, 2013 10:46 AM
Monday, March 11, 2013 10:46 AM -
Hi Haris,
Use the following code and add it on onload of event.
funtion onload()
{
var currentdate=new Date();
Xrm.Page.data.entity.attributes.get("Date field name").setValue(currentdate);
}
- Proposed as answer by Samba88 Wednesday, March 13, 2013 6:14 AM
Wednesday, March 13, 2013 6:14 AM -
Hi,
How should this code be changed in order that when I create the record the first time it defaults to today, but once the record is saved, it keeps that date forever unless I manually change it?
I would need the detailed script - sorry I am an absolute beginner with Javascripts ....thanks a lot
Regards
silvia
Wednesday, March 13, 2013 8:20 AM -
Hi Silvia,
You can put some conditions based on that conditions it will put current date.
Suppose if i take an Option set , this will contain 3 values - A, B, C.
If i change the Option set to "A" - then it will add Current Date.
On Next Day i changed the Option set to "B" - Then it will add Current Date.
- Proposed as answer by Samba88 Wednesday, March 13, 2013 8:47 AM
Wednesday, March 13, 2013 8:47 AM -
Hi,
How should this code be changed in order that when I create the record the first time it defaults to today, but once the record is saved, it keeps that date forever unless I manually change it?
I would need the detailed script - sorry I am an absolute beginner with Javascripts ....thanks a lot
Regards
silvia
function OnLoad() { var formType = Xrm.Page.ui.getFormType(); if (formType == 1) { Xrm.Page.getAttribute("fieldname").setValue(new Date()); } }
- Proposed as answer by Guido PreiteMVP Wednesday, March 13, 2013 8:49 AM
Wednesday, March 13, 2013 8:49 AM -
I have another question.
This scripts works fine (many thanks).
When I open the record, it defaults to today. If necessary I can overwrite it, and it keeps the change.
But if I cancel the date because in some cases I do not want any date on the record, and I save, it applies the default.... How can I solve this? I mean, when I cancel the default and save, I want the date field to stay empty.
Thanks
Wednesday, March 13, 2013 2:39 PM -
I have another question.
This scripts works fine (many thanks).
When I open the record, it defaults to today. If necessary I can overwrite it, and it keeps the change.
But if I cancel the date because in some cases I do not want any date on the record, and I save, it applies the default.... How can I solve this? I mean, when I cancel the default and save, I want the date field to stay empty.
Thanks
Wednesday, March 13, 2013 3:35 PM -
Hi,
There can be several condition check possible based on your form, one can be:
you can check if the date field is empty and if not, do not call your function.
var date = Xrm.Page.getAttribute("fieldname").getValue();
if (date == null)
{
function OnLoad() {
var formType = Xrm.Page.ui.getFormType();
if (formType == 1) {
Xrm.Page.getAttribute("fieldname").setValue(new Date());
}
}}
Thanks
Kunal Mehta
Kunal Mehta
Tuesday, March 19, 2013 7:17 AM