【问题标题】:Can't get setValue to work on a Dynamics CRM editable grid无法让 setValue 在 Dynamics CRM 可编辑网格上工作
【发布时间】:2018-05-09 17:12:07
【问题描述】:

我希望获得一些关于如何在 Dynamics CRM 365(本地)可编辑网格中执行 setValue 的指示:

无论我尝试什么,我都无法更新网格中的值。此代码获取属性的引用,但 setValue 似乎对网格没有影响。

    function updateDocsOK(ctlName, grdName, attributeName) {
    var selectedRow = null;
    var attributeColl = null;
    var twoOptionValue = 0;


    try {

        //This is the Yes/No value in the dropdown
        var ctlValue = Xrm.Page.getAttribute(ctlName).getValue();
        if (ctlValue) {
            twoOptionValue = 1;
        }
        //get the selected rows - use the getControl method and pass the grid name.
        selectedRow = Xrm.Page.getControl(grdName).getGrid().getRows();

        //loop through rows and get the attribute collection
        selectedRow.forEach(function (row, rowIndex) {


            var att = row.getData().getEntity().attributes.getByName(attributeName);

            //This setValue does not work on a two-option
            if (att) {
                console.log(att.getValue());
                att.setValue(twoOptionValue);
                console.log(att.getValue());
            }


            //This setValue does not work on a text field
            att = row.getData().getEntity().attributes.getByName("new_testtext");

            if (att) {
                att.setValue("hello");
            }


        });
    } catch (e) {
        Xrm.Utility.alertDialog(e.message);
    }
}

【问题讨论】:

    标签: javascript dynamics-crm dynamics-crm-365


    【解决方案1】:

    您应该选择传递执行上下文的选项,并使用executionContext.getFormContext() 获取可编辑网格中的当前行。

    function updateDocsOK(executionContext) {
        var entityObject = executionContext.getFormContext().data.entity;
        var att = entityObject.attributes.getByName("new_testtext");
        att.setValue("hello");
     }
    

    您不能在可编辑网格上使用 Xrm.Page 命令。在我的示例中,我将 想设置概率值。

    在表格上类似 Xrm.Page.getAttribute(“closeprobability”).setValue(80) 会做 诡计。但这不适用于可编辑的网格。

    相反,我需要使用已发布的新方法 Dynamics 365 (getFormContext)。

    getFormContext 返回表单 (Xrm.Page) 或 可编辑网格(GridRow)。这意味着我们现在可以拥有将 在这两种情况下都可以工作。

    Read more

    【讨论】:

    • 谢谢,但我应该描述一下我想要做什么。我在网格外部有一个字段,当它的值发生变化时,我想更改网格中每一行的数据。除非我有误解,否则上面的示例在从网格上下文中触发时会起作用,但我的调用是外部的。
    • @DaveS 明白了。在这种情况下,您可以直接使用服务调用更新所有子记录吗?因此,通过刷新网格,它将显示最新值。您只想填充但不想保存?
    • 再次感谢。理想情况下,我想更新行/单元格但不保存。但是,如果唯一的方法是调用服务,那么我将采用这条路线。
    • 仅供参考,我使用 Web API 解决了更新每条记录的问题。再次感谢您的帮助
    【解决方案2】:

    getEventSource 可用于 Dynamics CRM 365 可编辑网格获取或设置值:

    var year;  
     var weekNumber;  
     var selectedRow = null;  
     var attributeColl = null;  
     function dateChange(eContext) {  
       debugger;  
       var nameAttr = eContext.getEventSource();  
       var attrParent = nameAttr.getParent();  
       var startDateField = attrParent.attributes.get("new_startdate");  
       var date = startDateField.getValue();  
       //Get week number  
       var currentWeekNumber = parseFloat(date.getWeek());  
       //Get full year  
       const dt = new Date(date);  
       var currentyear = parseFloat(dt.getFullYear());  
       //set week value  
       var new_weeknumbercstField = attrParent.attributes.get("new_weeknumbercst");  
       new_weeknumbercstField.setValue(currentWeekNumber);  
       // Set year value  
       var new_yearcstField = attrParent.attributes.get("new_yearcst");  
       new_yearcstField.setValue(currentyear);  
     }  
     Date.prototype.getWeek = function() {  
       var onejan = new Date(this.getFullYear(), 0, 1);  
       return Math.ceil((((this - onejan) / 86400000) + onejan.getDay() + 1) / 7);  
     } 

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多