Answered by:
MS CRM 2011 - JavaScript Assistance Required

Question
-
Hello,
I am still fairly new to javascript so I apologise for my lack of knowledgein advance. I have a multiple line of text field "new_mlt" on my account entity form, when I select a specific value in a optionset "new_optionset" it pulls through some basic text. This works fine. However what I am looking to do is add a date field from the form into the basic text on change event: below is my current code, can anyone point me in the right direction about the method required:
function addtext(){
if (Xrm.Page.getAttribute("new_optionset").getSelectedOption() == null)
{
Xrm.Page.getAttribute("new_optionset").setValue("");
}
if(Xrm.Page.getAttribute("new_optionset").getSelectedOption() != null)
{
if(Xrm.Page.getAttribute("new_optionset").getSelectedOption().text =="A. Value 1")
{
Xrm.Page.getAttribute("new_mlt").setValue("add basic text from this date >> XX/XX/XXXX");
}
}
}
Where I have the XX/XX/XXXX above this is where I need a date field inserting from the form, IE: "new_datefield".Many Thanks
Adam
Tuesday, February 4, 2014 10:34 AM
Answers
-
Try this :
function addtext() { if (Xrm.Page.getAttribute("new_optionset").getSelectedOption() == null) { Xrm.Page.getAttribute("new_optionset").setValue(""); } if (Xrm.Page.getAttribute("new_optionset").getSelectedOption() != null) { if (Xrm.Page.getAttribute("new_optionset").getSelectedOption().text == "A. Value 1") { var date=Xrm.Page.getAttribute("new_datefield").getValue(); var stringdate= date.getDate() + "/" + date.getMonth()+1 + "/" + date.getFullYear(); Xrm.Page.getAttribute("new_mlt").setValue("add basic text from this date >>" + stringdate); } } }
For more information on JavaScript date data types, please check the following link :
http://www.w3schools.com/jsref/jsref_obj_date.asp
Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !!!
Vikram Singh. !!! My Blog- Proposed as answer by Guido PreiteMVP Tuesday, February 4, 2014 10:50 AM
- Marked as answer by samrr1875 Tuesday, February 4, 2014 11:53 AM
Tuesday, February 4, 2014 10:49 AM -
It would be like this :
Xrm.Page.getAttribute("new_mlt").setValue("Hi, my name is Adam. Today is" + stringdate + "and I live in the United Kingdom");
Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !!!
Vikram Singh. !!! My Blog- Marked as answer by samrr1875 Tuesday, February 4, 2014 12:47 PM
Tuesday, February 4, 2014 11:59 AM
All replies
-
Try this :
function addtext() { if (Xrm.Page.getAttribute("new_optionset").getSelectedOption() == null) { Xrm.Page.getAttribute("new_optionset").setValue(""); } if (Xrm.Page.getAttribute("new_optionset").getSelectedOption() != null) { if (Xrm.Page.getAttribute("new_optionset").getSelectedOption().text == "A. Value 1") { var date=Xrm.Page.getAttribute("new_datefield").getValue(); var stringdate= date.getDate() + "/" + date.getMonth()+1 + "/" + date.getFullYear(); Xrm.Page.getAttribute("new_mlt").setValue("add basic text from this date >>" + stringdate); } } }
For more information on JavaScript date data types, please check the following link :
http://www.w3schools.com/jsref/jsref_obj_date.asp
Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !!!
Vikram Singh. !!! My Blog- Proposed as answer by Guido PreiteMVP Tuesday, February 4, 2014 10:50 AM
- Marked as answer by samrr1875 Tuesday, February 4, 2014 11:53 AM
Tuesday, February 4, 2014 10:49 AM -
Hi,
instead of
Xrm.Page.getAttribute("new_mlt").setValue("add basic text from this date >> XX/XX/XXXX");
you can write
var d = Xrm.Page.getAttribute("new_datefield").getValue(); var month = '' + (d.getMonth() + 1); var day = '' + d.getDate(); var year = ''+ d.getFullYear(); if (month.length < 2) month = '0' + month; if (day.length < 2) day = '0' + day; var dFormatted = [day, month, year].join('/'); Xrm.Page.getAttribute("new_mlt").setValue("add basic text from this date >> " + dFormatted);
My blog: www.crmanswers.net - Rockstar 365 Profile
- Proposed as answer by Guido PreiteMVP Tuesday, February 4, 2014 10:49 AM
- Edited by Guido PreiteMVP Tuesday, February 4, 2014 10:50 AM
Tuesday, February 4, 2014 10:49 AM -
Thanks Vikram, that is a big help and I understand the script in order to modify if required. I have a another question, if I need to add the date field in the middle of my text, for example:
Xrm.Page.getAttribute("new_mlt").setValue("add basic text from this date >>" + stringdate);
So I add the setValue("Hi, my name is Adam. Today is STRINGDATE and I live in the United Kingdom"
How would I do this in JavaScript? I guess I would have to break the text up into 2 scripts and add the stringdate between?
Thanks Again for the quick response.
Adam
Tuesday, February 4, 2014 11:46 AM -
It would be like this :
Xrm.Page.getAttribute("new_mlt").setValue("Hi, my name is Adam. Today is" + stringdate + "and I live in the United Kingdom");
Hope this helps. If you get answer of your question, please mark the response as an answer and vote as helpful !!!
Vikram Singh. !!! My Blog- Marked as answer by samrr1875 Tuesday, February 4, 2014 12:47 PM
Tuesday, February 4, 2014 11:59 AM