Answered by:
Display only the year

Question
-
Hello all,
I would like to add a field on the opportunity form that only displays the year or the record's creation.
I have seen through the forum that I can make it with javascript, but since I'm not a developer, I don't know how to do it.
So if anybody has the time to explain me step by step how to implement, it'll help me a lot.
Thank you for your help,
Sylvain
Wednesday, August 12, 2015 3:29 PM
Answers
-
Hello,
1. Create field of type int and put in on CRM Form (for example with new_year schema name).
2. Create JavaScript webresource with following content:
function populateyear(){ if (Xrm.Page.ui.getFormType() == 1){ var d = new Date(); Xrm.Page.getAttribute("new_year").setValue(d.getFullYear()); } }
3. Add created webresource for form and add onload event handler that points to populateyear function.
Recheck this article - http://happycrm.blogspot.com/2010/11/ms-crm-2011-my-first-experience-adding.html
Dynamics CRM MVP
My blog- Proposed as answer by Edwin2win Thursday, August 13, 2015 5:43 AM
- Marked as answer by sylvain_43 Thursday, August 13, 2015 8:46 AM
Thursday, August 13, 2015 5:01 AMModerator -
1. Use Whole number field. No Datetime fields.
2. use field change field to lowercase in code - new_date instead of new_Date.
Dynamics CRM MVP
My blog- Proposed as answer by Andrii ButenkoMVP, Moderator Thursday, August 13, 2015 8:21 AM
- Marked as answer by sylvain_43 Thursday, August 13, 2015 8:46 AM
Thursday, August 13, 2015 7:55 AMModerator -
Try to use following code:
Xrm.Page.getAttribute("new_year").setValue(d.getFullYear() % 100);
Dynamics CRM MVP
My blog- Proposed as answer by Andrii ButenkoMVP, Moderator Thursday, August 13, 2015 8:21 AM
- Marked as answer by sylvain_43 Thursday, August 13, 2015 8:46 AM
Thursday, August 13, 2015 8:21 AMModerator -
that mod operator can be used if the value need to be an integer, because for years inside the first decade it returns only the one digit
If you need to get the two digits as a string (so 01 instead of 1) you can use slice
Xrm.Page.getAttribute("new_year").setValue(d.getFullYear().toString().slice(-2));
My blog: www.crmanswers.net - CRM Theme Generator
- Edited by Guido PreiteMVP Thursday, August 13, 2015 11:26 AM
- Marked as answer by sylvain_43 Thursday, August 13, 2015 11:55 AM
Thursday, August 13, 2015 11:24 AM
All replies
-
Hello,
1. Create field of type int and put in on CRM Form (for example with new_year schema name).
2. Create JavaScript webresource with following content:
function populateyear(){ if (Xrm.Page.ui.getFormType() == 1){ var d = new Date(); Xrm.Page.getAttribute("new_year").setValue(d.getFullYear()); } }
3. Add created webresource for form and add onload event handler that points to populateyear function.
Recheck this article - http://happycrm.blogspot.com/2010/11/ms-crm-2011-my-first-experience-adding.html
Dynamics CRM MVP
My blog- Proposed as answer by Edwin2win Thursday, August 13, 2015 5:43 AM
- Marked as answer by sylvain_43 Thursday, August 13, 2015 8:46 AM
Thursday, August 13, 2015 5:01 AMModerator -
Hello,
Thanks for your response.
I tried your solution but it doesn't work for me. It's probably because of the field type: I created a field named 'Date', and I tried diffrent type: Whole number, Date and Time (calculated or not) and even single line of text.
Then I change within the function new_year as new_Date
If you see what I did wrong, it'll be helpfull.
Sylvain
Thursday, August 13, 2015 7:51 AM -
1. Use Whole number field. No Datetime fields.
2. use field change field to lowercase in code - new_date instead of new_Date.
Dynamics CRM MVP
My blog- Proposed as answer by Andrii ButenkoMVP, Moderator Thursday, August 13, 2015 8:21 AM
- Marked as answer by sylvain_43 Thursday, August 13, 2015 8:46 AM
Thursday, August 13, 2015 7:55 AMModerator -
Yes it's working!!! Thank you very much
I have just a last request, how is it possible to display only the two last digits of the year? (eg: 15 instead of 2015)?
Sylvain
Thursday, August 13, 2015 8:05 AM -
Try to use following code:
Xrm.Page.getAttribute("new_year").setValue(d.getFullYear() % 100);
Dynamics CRM MVP
My blog- Proposed as answer by Andrii ButenkoMVP, Moderator Thursday, August 13, 2015 8:21 AM
- Marked as answer by sylvain_43 Thursday, August 13, 2015 8:46 AM
Thursday, August 13, 2015 8:21 AMModerator -
It's working, thank you!
Sylvain
Thursday, August 13, 2015 8:46 AM -
that mod operator can be used if the value need to be an integer, because for years inside the first decade it returns only the one digit
If you need to get the two digits as a string (so 01 instead of 1) you can use slice
Xrm.Page.getAttribute("new_year").setValue(d.getFullYear().toString().slice(-2));
My blog: www.crmanswers.net - CRM Theme Generator
- Edited by Guido PreiteMVP Thursday, August 13, 2015 11:26 AM
- Marked as answer by sylvain_43 Thursday, August 13, 2015 11:55 AM
Thursday, August 13, 2015 11:24 AM -
Ok thank youThursday, August 13, 2015 11:55 AM
-
Hello,
you can also use workflows to fill your new custom integer fields for month and year.
At Codeplex is a custom workflow "Get Date Parts" available (https://wfdatetimeutilities.codeplex.com).
Install these custom workflows, use it in a process und you can update the month or year value in the datafieldsHerzliche Grüße / Best regards Markus Müller If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"
Tuesday, August 18, 2015 8:33 AM