使用JavaScript从CRM中的日期和时间字段中获取日期字符串值

时间:2016-07-12 18:43:44

标签: javascript dynamics-crm return-value datefield

我正在尝试从自定义日期和时间字段中获取日期字符串值(mm / dd / yyyy),并将返回的值设置回自定义字段。我找到了这个脚本并对其进行了修改,但它似乎无法正常工作。当我逐步执行代码时,它会在 var year = startDate.getFullYear()+""; 中断,我有什么想法?谢谢。

    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();


}

1 个答案:

答案 0 :(得分:1)

假设new_dateauthorized是CRM日期字段,则Xrm.Page.getAttribute("new_dateauthorized").getValue()将返回Date object

在这种情况下,您可以操纵Date对象,如下所示:

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

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

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

但是,在某些情况下以这种方式添加月份会失败,请查看评论here以获取更多信息。