Dynamics crm 2011 CRM 2011-使用JScript设置默认值

Dynamics crm 2011 CRM 2011-使用JScript设置默认值,dynamics-crm-2011,Dynamics Crm 2011,我们有CRM 2011内部部署。联系人实体已自定义为使用自定义实体国家/地区的查找,而不仅仅是文本字段。创建新联系人时,我们希望国家字段默认设置为加拿大。我有以下功能可以做到这一点: function SetDefaultCountryCode(countryFieldId) { var _canadaId = "{FC167B4D-1C3B-E111-8904-F2EA3FE25706}"; var c

我们有CRM 2011内部部署。联系人实体已自定义为使用自定义实体国家/地区的查找,而不仅仅是文本字段。创建新联系人时,我们希望国家字段默认设置为加拿大。我有以下功能可以做到这一点:

            function SetDefaultCountryCode(countryFieldId) {

                var _canadaId = "{FC167B4D-1C3B-E111-8904-F2EA3FE25706}";

                var countryControl = Xrm.Page.getAttribute(countryFieldId);

                // only attempt the code if the control exists on the form
                if (countryControl != null) {
                    var currentCountry = countryControl.getValue();

                    // if country is not specified, then set it to the default one (Canada) 
                    if (currentCountry == null) {
                        var defaultCountry = new Object();
                        defaultCountry.entityType = "cga_country";
                        defaultCountry.id = _canadaId;
                        defaultCountry.name = "Canada";
                        var countryLookupValue = new Array();
                        countryLookupValue[0] = defaultCountry;

                        countryControl.setValue(countryLookupValue);
                    }
                }
            }
在表单OnLoad上,我调用如下函数:

    // set Country fields to Canada if not set
    SetDefaultCountryCode('cga_address1country');
我们有两个服务器-DEV和TEST。这个JScript在DEV中工作得很好。当我在测试中运行它时,它不工作,因为Canada in测试在我手动创建它时具有不同的id(GUID)。我希望我可以从DEV导出Country实体值,并将它们导入到TEST中,保留它们的guid。不幸的是,这不起作用。我将数据导出到Excel文件,它具有国家的GUID。在导入之前,我还会删除测试中的任何现有国家/地区记录。当我尝试导入它时,导入成功,但不会创建任何记录。如果我在excel文件中添加新行而不指定Guid,它将导入该行。在我看来,导入功能并不是为了保存记录的guid。但这也意味着我的脚本将无法工作,因为它依赖于guid

我这里有两个问题:

  • 是否可以导出/导入保留GUID的实体数据

  • 如果我不能在DEV中使用相同的guid并测试如何使JScript正常工作


  • 提前感谢您提供的任何帮助/反馈。

    硬编码您的GUI是一种非常糟糕的做法,您发现了其中的问题。 如上所述,我们不能有相同的guid,但我们有相同的名称。因此,我们必须使用JScript和jQuery查询国家名称以检索GUID

    为了从客户端(或实体表单)中删除信息:

  • 我们将使用/使用REST端点(在浏览器中进行测试)
  • 上传jQuery库
  • 上传Json2库
  • 使用jQuery库中的AJAX函数
  • 定义实体、列和条件
  • 让我们来看一下查询REST端点

     http://yourHostName/yourOrg/XRMServices/2011/OrganizationData.svc/new_CountrytSet?$select=new_Name,new_CountryId&$filter=new_Name eq 'Canada'
    
    获取此URL,替换实际值并将其粘贴到浏览器中,您会发现响应以XML格式返回。如果有任何错误,请确保实体名称及其属性为case senisitve

    看到结果后,我们将使用AJAX调用调用此URL

    $.ajax({
                    type: "GET",
                    contentType: "application/json; charset=utf-8",
                    datatype: "json",
                    url: 'http://yourHostName/yourOrg/XRMServices/2011/OrganizationData.svc/new_CountrytSet?$select=new_Name,new_CountryId&$filter=new_Name eq 'Canada'',
                    beforeSend: function (XMLHttpRequest) {
                        //Specifying this header ensures that the results will be returned as JSON.             
                        XMLHttpRequest.setRequestHeader("Accept", "application/json");
                    },
                    success: function (data) {
                        if (data.d && data.d.results) {
                            //var _canadaId = "{FC167B4D-1C3B-E111-8904-F2EA3FE25706}"; no longer be used
                            var _canadaId = data.d.results[0].ContactId;
    
                            // now we have the GUID of Canada, now I can continue my process
    
                        }
    
                    },
                    error: function (XmlHttpRequest) {
    
                        alert("Error : " + XmlHttpRequest.status + ": " + XmlHttpRequest.statusText + ": " + JSON.parse(XmlHttpRequest.responseText).error.message.value);
                    }
    
                });
    
    但是在将代码复制到表单之前,必须从 然后将其作为Web资源上载,并将此Web资源添加到表单加载库中

    以下是要放入表单加载事件处理程序中的完整代码:

    var context = GetGlobalContext();
    
    // retireve the invoice record id (Opened Form)
    var invoiceId = context.getQueryStringParameters().id;
    var customerId;
    
    //Retrieve the server url, which differs on-premise from on-line and 
    //shouldn't be hard-coded.
    // this will return something like http://yourHostName/yourOrg
    var serverUrl = context.getServerUrl();
    
    
    //The XRM OData end-point
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
    
    var odataUri = serverUrl + ODATA_ENDPOINT;
    
    function SetDefaultCountryCode(countryFieldId, odataUri) {
    
        odataUri = odataUri + '/ContactSet?$select=ContactId,FullName&$filter=FullName eq \'Ahmed Shawki\'';
        $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: odataUri,
            beforeSend: function (XMLHttpRequest) {
                //Specifying this header ensures that the results will be returned as JSON.             
                XMLHttpRequest.setRequestHeader("Accept", "application/json");
            },
            success: function (data) {
                if (data.d && data.d.results) {
                    //var _canadaId = "{FC167B4D-1C3B-E111-8904-F2EA3FE25706}"; no longer be used
                    var _canadaId = data.d.results[0].ContactId;
    
                    var countryControl = Xrm.Page.getAttribute(countryFieldId);
    
                    // only attempt the code if the control exists on the form
                    if (countryControl != null) {
                        var currentCountry = countryControl.getValue();
    
                        // if country is not specified, then set it to the default one (Canada) 
                        if (currentCountry == null) {
                            var defaultCountry = new Object();
                            defaultCountry.entityType = "cga_country";
                            defaultCountry.id = _canadaId;
                            defaultCountry.name = "Canada";
                            var countryLookupValue = new Array();
                            countryLookupValue[0] = defaultCountry;
    
                            countryControl.setValue(countryLookupValue);
                        }
                    }
                }
    
            },
            error: function (XmlHttpRequest) {
    
                alert("Error : " + XmlHttpRequest.status + ": " + XmlHttpRequest.statusText + ": " + JSON.parse(XmlHttpRequest.responseText).error.message.value);
            }
    
        });
    
    }
    
    还有一件事,不要忘记选中表单属性上的“将执行上下文作为第一个参数传递””框

    编辑:除了将jQuery库添加到表单加载事件处理程序中之外,还将该库添加为web资源


    有关.

    的更多信息,确实可以将记录及其GUID一起导出和导入,但不能以本机方式导出和导入。您必须构建一个可以为您导出数据的应用程序,然后在目标环境中通过CRM API创建相同的记录。您只需清除对create无效的字段(createdon、statecode等),然后指定相同的Guid即可。然后,CRM将使用该Guid创建记录


    旧的4.0做到了这一点。我不记得它是否适用于2011年的组织,但它可能是一个起点。

    您是否考虑过查询
    Guid
    而不是硬编码它?请记住,您还应该将其作为web资源包括在内,因为Dynamics CRM支持的旧版IE没有本机JSON支持。此解决方案也可以。我同意硬编码guid不是一个好的实践,但是我可以证明这一点,前提是我可以在两种环境中保持相同的guid。这就像使用一个常量——它不会改变,在每个实例上查询服务以获取GUID并不是我喜欢的事情。有没有一种方法可以获取GUID并以某种方式存储它以供以后重用?谢谢。解决这个问题的两种方法是:不用大的查询,只需使用Xrm.Page.context.getOrganUniqueName()获取当前组织名称即可;然后使用它在dev/test/live环境的有效guid之间切换。或者,您可以向合同实体添加一个选项集,并为所需的guid添加选项作为字符串。将每个环境的默认值设置为正确的值。从脚本中的该字段获取值(只要该字段位于表单上,即使它设置为“默认可见”=“否”),也可以执行此操作)。谢谢-该工具完全满足我的需要。它与源代码一起提供,您必须编译它。这个解决方案是针对VS2008的,但我能够用VS2010编译它,它与CRM 2011一起工作。再次感谢!