Javascript 无法获取setValue以在Dynamics CRM可编辑网格上工作

Javascript 无法获取setValue以在Dynamics CRM可编辑网格上工作,javascript,dynamics-crm,dynamics-crm-365,Javascript,Dynamics Crm,Dynamics Crm 365,我希望得到一些关于如何在Dynamics CRM 365内部可编辑网格中执行设置值的指导: 无论我尝试什么,我都无法更新网格中的值。此代码获取属性的引用,但setValue似乎对网格没有影响 function updateDocsOK(ctlName, grdName, attributeName) { var selectedRow = null; var attributeColl = null; var twoOptionValue = 0; t

我希望得到一些关于如何在Dynamics CRM 365内部可编辑网格中执行设置值的指导:

无论我尝试什么,我都无法更新网格中的值。此代码获取属性的引用,但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);
    }
}

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

function updateDocsOK(executionContext) {
    var entityObject = executionContext.getFormContext().data.entity;
    var att = entityObject.attributes.getByName("new_testtext");
    att.setValue("hello");
 }
不能在可编辑网格上使用Xrm.Page命令。在我的例子中,我将 要设置概率值

在形式上类似于 getAttribute“closeprobability”.setValue80将执行以下操作 戏法但这在可编辑栅格上不起作用

取而代之的是,我需要使用一个新的方法,这个方法已经发布了 Dynamics 365 getFormContext

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


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

var年; 变量周数; var selectedRow=null; var attributeColl=null; 函数DateChangeContext{ 调试器; var nameAttr=eContext.getEventSource; var attrpent=nameAttr.getParent; var startDateField=attrParent.attributes.getnew_startdate; var date=startDateField.getValue; //获取周数 var currentWeekNumber=parseFloatdate.getWeek; //整年 const dt=新日期; var currentyear=parseFloatdt.getFullYear; //设置周值 var new_weeknumbercstField=attrplaent.attributes.getnew_weeknumbercst; new_weeknumbercstField.setValuecurrentWeekNumber; //设定年份值 var new_yearcstField=attrplaent.attributes.getnew_yearcst; new_yearcstField.setValuecurrentyear; } Date.prototype.getWeek=函数{ var onejan=new Datethis.getFullYear,0,1; 返回Math.ceilthis-onejan/86400000+onejan.getDay+1/7;
}谢谢,但我应该描述一下我想做什么。我在网格外部有一个字段,当该字段的值更改时,我希望更改网格中每一行的数据。除非我有误解,否则当从网格上下文中触发时,上面的示例将起作用,但我的调用是外部的。@DaveS明白了。在这种情况下,您可以直接使用服务调用更新所有子记录吗?因此,通过刷新网格,它将显示最新的值。您只想填充但不想保存?再次感谢。理想情况下,我希望更新行/单元格,但不保存。但是,如果唯一的方法是服务调用,那么我将继续这条路线。仅供参考,我使用Web API更新了每条记录。再次感谢你的帮助