Dynamics CRM 2011 form jscript to retrieve lookup data

為{幸葍}努か 提交于 2019-12-25 03:55:38

问题


What are my mistakes, why do I get the "object expected" error, and, eventually, how does one debug jScript ?

I am new to Dynamics CRM and I would like to do a small customisation, which seem to require jScript. The instance (version 2011) is used mostly to manage client support.

There are 2 custom entities with relationships: FundLegalEntity --> SubFund

The Case (Incident) form is linked to the FundLegalEntity and the SubFund.
When user enters a SubFund I would like to have the FundLegalEntity filled automatically (if empty).
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:

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. 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...
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. 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.


回答1:


You can use the Rest endpoint from your script to retrieve data from the organization service. Here's an example to get you started. You can also look at the SDK documentation there's a lot of useful information there.

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. See CRM 2011 "$ is undefined"



来源:https://stackoverflow.com/questions/7093409/dynamics-crm-2011-form-jscript-to-retrieve-lookup-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!