1

I am trying to get the date string value(mm/dd/yyyy)from a custom date and time field and set the returned value back into the custom field. I found this script and modified it but it does not seem to work. When I step through the code it breaks on var year = startDate.getFullYear() + ""; Any ideas what I am doing wrong? Thanks.

    function ConcatChainsAuth() {

    var startDate = Xrm.Page.getAttribute("new_dateauthorized").getValue();

    if (startDate != null) {
        var year = startDate.getFullYear() + "";
        var month = (startDate.getMonth() + 1) + "";
        var day = startDate.getDate() + "";
        var dateFormat = month + "-" + day + "-" + year;

      Xrm.Page.getAttribute("new_dateauthorized").setValue(dateFormat);

    }

    var lookupObject = Xrm.Page.getAttribute("new_chain");

    if (lookupObject != null) {

        var lookUpObjectValue = lookupObject.getValue();

        if ((lookUpObjectValue != null)) {

            var Chain = lookUpObjectValue[0].name;

        }

    }
    var lookupObject = Xrm.Page.getAttribute("new_package");

    if (lookupObject != null) {

        var lookUpObjectValue = lookupObject.getValue();

        if ((lookUpObjectValue != null)) {

            var Package = lookUpObjectValue[0].name;

        }

    }

    var concatedField = Chain + "-" + Package + "-" + dateFormat;

    Xrm.Page.getAttribute("new_name").setValue(concatedField);

    Xrm.Page.data.entity.save();


}
PJM
  • 45
  • 2
  • 7
  • I am concatenating 3 fields into the name field on the form. I am trying to get the field to concatenate like so: Field1 - Field2 - 7 -12 - 2016 but instead it concatenates like this : Field1 - Field2 - Tue Jul 12 2016 00:00:00 GMT-0700(Pacific Daylight Time) – PJM Jul 12 '16 at 18:57
  • What is the data type of `startDate`? – trincot Jul 12 '16 at 19:34
  • @trincot Thank you for your response. startDate is the variable for a custom date/time field on the form. – PJM Jul 12 '16 at 19:47
  • I understand that, but it looks like for some reason it is not recognised as a date. Have confirmed that data type to be date/time with `console.log(startDate)`? – trincot Jul 12 '16 at 19:55
  • @trincot I have confirmed because I can view the data type in the field properties in CRM. – PJM Jul 12 '16 at 20:03
  • Which error message do you get in console when your *code breaks*? – trincot Jul 12 '16 at 20:07
  • @trincot It looks like I got the code to work but I am still not getting the correct results returned in the field I am concatenating these values into. In the name field on the form I want to concatenate field1 - field2 - date. The code works in that it concatenantes the values such as: field1-field2-Tue Jul 12 2016 00:00:00 GMT-0700 (Pacific Daylight Time) but I want it to look like this: field1 - field2 - 7-12-2016.... – PJM Jul 12 '16 at 21:28
  • But in your question you wrote *"When I step through the code it breaks"*: what does that mean? Has your question changed since you first wrote it? If so, could you update your question and put the code with which you are able to make the concatenation work? – trincot Jul 13 '16 at 11:01
  • @trincot I have fixed the issue by adding an if statement. Please see code above. Thank you for help. – PJM Jul 14 '16 at 20:21
  • The question still says the *the code breaks*... Could you please update the question to highlight what your problem is now? – trincot Jul 14 '16 at 20:23

1 Answers1

1

Assuming that new_dateauthorized is a CRM date field, then Xrm.Page.getAttribute("new_dateauthorized").getValue() will return a Date object.

In which case you can just manipulate the Date object, like so:

var currentDate = Xrm.Page.getAttribute("new_dateauthorized").getValue();

currentDate.setMonth(currentDate.getMonth() + 1);

Xrm.Page.getAttribute("new_dateauthorized").setValue(currentDate);

However, adding months in this fashion fails in some cases, check out the comments here for more info.

Community
  • 1
  • 1
James Wood
  • 17,286
  • 4
  • 46
  • 89