0
votes

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
Did you try updating the field's value at the end of the Success function ? Something like Xrm.Page.getAttribute("new_subfundid").setValue(parentFundLookup);dub
No, I did not realize that I had to relace that part. I'll try it right now. In the meantime, I found that alert(result.new_LegalEntityId.Name); gives me the name of the Legal Entity, so I am now on the right track I guess. I feel a bit stupid here, since this is a totally new area for me. I know my stuff pretty well with VBA and databases, but never did any JavaScript nor called a web service.Patrick Honorez
@dub: nearly done, see Edits. Thanks !!Patrick Honorez
You say the textbox has a strange aspect. Can you add a screenshot or further describe the problem ?dub
Ah I see what's wrong. EntityType must be lowercase so it would be entityType : "new_legalentity".dub

1 Answers

3
votes

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"