简体   繁体   English

Dynamics CRM 2011 表单 jscript 以检索查找数据

[英]Dynamics CRM 2011 form jscript to retrieve lookup data

What are my mistakes, why do I get the "object expected" error, and, eventually, how does one debug jScript?我的错误是什么,为什么会出现“预期对象”错误,以及最终如何调试 jScript?

I am new to Dynamics CRM and I would like to do a small customisation, which seem to require jScript.我是 Dynamics CRM 的新手,我想做一个小的定制,这似乎需要 jScript。 The instance (version 2011) is used mostly to manage client support.该实例(2011 版)主要用于管理客户端支持。

There are 2 custom entities with relationships: FundLegalEntity --> SubFund有 2 个具有关系的自定义实体: FundLegalEntity --> SubFund

The Case (Incident) form is linked to the FundLegalEntity and the SubFund.案例(事件)表格链接到 FundLegalEntity 和 SubFund。
When user enters a SubFund I would like to have the FundLegalEntity filled automatically (if empty).当用户输入 SubFund 时,我希望 FundLegalEntity 自动填充(如果为空)。
My question was: how do I code that ?我的问题是:我该如何编码

With the help of this great tutorial , and the very usefull oData Tool , and great help (below) from user @dub, here is my latest code:借助 这个很棒的教程非常有用的 oData 工具,以及来自用户 @dub 的大力帮助(如下),这是我的最新代码:

function recalcParent()
{ 
    var lookupValue = Xrm.Page.getAttribute("new_subfundid").getValue();   

    var subFundId= lookupValue[0].id;
    // alert(subFundId);

    var request =  Xrm.Page.context.getServerUrl() + 
        "/xrmServices/2011/OrganizationData.svc/new_subfundSet?" + 
        "$select=new_LegalEntityId&" + 
        "$filter=new_subfundId eq guid'"+ subFundId+ "'";
    // alert(request);

$.ajax({
    type: "GET",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    url: request,
    async: false,
    beforeSend: 
        function (XMLHttpRequest) 
        { 
            XMLHttpRequest.setRequestHeader("Accept", "application/json"); 
        },
    success: 
        function (data, textStatus, XmlHttpRequest) 
        {
            var result = data.d.results[0];
            alert(result);
            var parentFundLookup = [{ entityType : "new_fund", id : result.LegalEntityId, name : result.FundLegalEntity}];                  
            // Set the value of the parent fund lookup 
        },
    error: 
        function (XmlHttpRequest, textStatus, errorThrown) 
        { 
            alert('Failed'); 
        }
    });       

} 

I have no more errors, the first 2 alerts (commente out) are giving me correct results.我没有更多错误,前 2 个警报(注释掉)给了我正确的结果。 THe 3rd alert displays "object Object", and the control I expect to update is not updated.第三个警报显示“对象对象”,我希望更新的控件没有更新。
Any hint please?请问有什么提示吗? I suppose the last problem is in the var parentFundLookup = line...我想最后一个问题是在var parentFundLookup =行...
I am a bit confused by all these different names.我对所有这些不同的名字有点困惑。
Thanks !谢谢 !


Edit:编辑:

It's nearly working now: when I modify the sub-fund in the Incident, the Legal Entity gets updated with the correct legal entity name, but the text box has a strange aspect, and the icon at the left of the text box is strange as well.现在几乎可以工作了:当我在事件中修改子基金时,法人实体被更新为正确的法人实体名称,但文本框有一个奇怪的方面,文本框左侧的图标很奇怪出色地。 Here is the latest bit of code:这是最新的代码:

success: 
    function (data, textStatus, XmlHttpRequest) 
    {
        var result = data.d.results[0];
        //alert(result.new_LegalEntityId.Name);
        var parentFundLookup = [{ entityType : "new_LegalEntity", id : result.new_LegalEntityId.Id, name : result.new_LegalEntityId.Name}];    
        Xrm.Page.getAttribute("new_fundlegalentityid").setValue(parentFundLookup);
    },

I suspect that the problem lies in the entityType: "new_LegalEntity" , but I don't know what to put in there.我怀疑问题出在entityType: "new_LegalEntity" ,但我不知道该放什么。 Any clue on this?对此有任何线索吗? What does that represent?那代表什么?
Here is a screenshot of the Legal Entity after the Sub-Fund is updated and the script has run.这是子基金更新和脚本运行后法人实体的屏幕截图

You can use the Rest endpoint from your script to retrieve data from the organization service.您可以使用脚本中的 Rest 端点从组织服务中检索数据。 Here's an example to get you started.这是一个让您入门的示例。 You can also look at the SDK documentation there's a lot of useful information there.您还可以查看 SDK 文档,那里有很多有用的信息。

var subfundid; // get the id from the lookup 

var request = 
    Xrm.Page.context.getServerUrl() + 
    "/XRMServices/2011/OrganizationData.svc/new_subfundSet?" + 
        "$select=ParentId&" +
        "$top=1&" + 
        "$filter=new_subfundId eq guid'"+ subfundid + "'";

$.ajax({
    type: "GET",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    url: request,
    async: false,
    beforeSend: 
        function (XMLHttpRequest) 
        { 
            XMLHttpRequest.setRequestHeader("Accept", "application/json"); 
        },
    success: 
        function (data, textStatus, XmlHttpRequest) 
        {
            var result = data.d.results[0];
            var parentFundLookup = [{ entityType : "new_fund", id : result.ParentId, name : result.FundName}];                  
            // Set the value of the parent fund lookup 
        },
    error: 
        function (XmlHttpRequest, textStatus, errorThrown) 
        { 
            alert('Failed'); 
        }
    });

Since this code uses JQuery, you'll need to add the JQuery library as a web resource and include it in your form.由于此代码使用 JQuery,因此您需要将 JQuery 库添加为 web 资源并将其包含在您的表单中。 See CRM 2011 "$ is undefined"请参阅CRM 2011“$ 未定义”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM