web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

jScript changes in CRM 2016?

(0) ShareShare
ReportReport
Posted on by 1,465

I couldn't find any documentation from MS on changes to jScript in 2016. I'm hoping someone can point me at a resource that can help explain what I'm seeing.

We only track Accounts as our customers on our case form. If a user creates a case from a contact, we have jscript that sets our customer field to the contact's account. In CRM 2015 this works perfectly, but this breaks in CRM 2016.

We are referencing the Entity.Attributes property. I have a screenshot below showing the discrepancy. For some reason, the logicalName (entity type) is blank in CRM 2016, and the value is moved to the name. Meanwhile, the "name" field is completely lost.

Any help on this is appreciated.

CRM-jScript-issue.png

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Please provide code you use.

  • J Matlon Profile Picture
    1,465 on at

    I included the fetch, which grabs my Contact entity. The code that is experiencing my issue is towards the bottom.

    ----------------------

     if (updateCustomer) {
            var contactFetch = "<fetch version='1.0' output-format='xml-platform' mapping='logical' no-lock='true' distinct='false'>" +
            "  <entity name='contact'>" +
            "    <attribute name='parentcustomerid' />" +
            "    <filter type='and'>" +
            "      <condition attribute='contactid' operator='eq' value='" + customer[0].id + "' />" +
            "    </filter>" +
            "  </entity>" +
            "</fetch>";
            _oService = new FetchUtil(_sOrgName, _sServerUrl);
            var contact = _oService.Fetch(contactFetch);
            if (contact != null && contact && contact.length > 0) {
                var a = contact[0];
                copyField(a, "parentcustomerid", "customerid"); // Call to the offending function
            }
            Xrm.Page.getAttribute("primarycontactid").setValue(customer); // Set Contact field
        }
    }
    
    function copyField(source, sourceProp, destinationProp) {
        if (type == "a:EntityReference") {
            var array = new Array();
            var lookup = new Object();
            lookup.id = source.attributes[sourceProp].guid;
            lookup.name = source.attributes[sourceProp].name; //<-- Not the expected value
            lookup.entityType = source.attributes[sourceProp].logicalName; //<-- Not the expected value
            array[0] = lookup;
            alert("id: " + lookup.id + "; name: " + lookup.name + "; type: " + lookup.entityType);
            Xrm.Page.getAttribute(destinationProp).setValue(array); // Throws an error due to type not set
        }
    }
  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Code looks incomplete. Please provide code of FetchUtil.

  • J Matlon Profile Picture
    1,465 on at

    FetchUtil is simply a SOAP wrapper. Code follows.

    ---------------

    function FetchUtil(sOrg, sServer) {
        this.org = sOrg;
        this.server = sServer;
    
        if (sOrg == null) {
            if (typeof (ORG_UNIQUE_NAME) != "undefined") {
                this.org = ORG_UNIQUE_NAME;
            }
        }
    
        if (sServer == null) {
            this.server = window.location.protocol + "//" + window.location.host;
        }
    }
    
    FetchUtil.prototype.Fetch = function (sFetchXml, fCallback) {
        /// <summary>Execute a FetchXml request. (result is the response XML)</summary> 
        /// <param name="sFetchXml">fetchxml string</param> 
        /// <param name="fCallback" optional="true" type="function">(Optional) Async callback function if specified. If left null, function is synchronous </param>
    
    
        var request = "<s:Envelope xmlns:s=\"schemas.xmlsoap.org/.../envelope\">";
        request += "<s:Body>";
    
        request += '<Execute xmlns="schemas.microsoft.com/.../Services">' +
                    '<request i:type="b:RetrieveMultipleRequest" ' +
                    ' xmlns:b="schemas.microsoft.com/.../Contracts" ' +
                    ' xmlns:i="www.w3.org/.../XMLSchema-instance">' +
                    '<b:Parameters xmlns:c="schemas.datacontract.org/.../System.Collections.Generic">' +
                    '<b:KeyValuePairOfstringanyType>' +
                    '<c:key>Query</c:key>' +
                    '<c:value i:type="b:FetchExpression">' +
                    '<b:Query>';
    
        request += CrmEncodeDecode.CrmXmlEncode(sFetchXml);
    
        request += '</b:Query>' +
    '</c:value>' +
    '</b:KeyValuePairOfstringanyType>' +
    '</b:Parameters>' +
    '<b:RequestId i:nil="true"/>' +
    '<b:RequestName>RetrieveMultiple</b:RequestName>' +
    '</request>' +
    '</Execute>';
    
        request += '</s:Body></s:Envelope>';
    
        return this._ExecuteRequest(request, "Fetch", this._FetchCallback, fCallback);
    }
  • J Matlon Profile Picture
    1,465 on at

    I missed my _ExecuteRequest function. This doesn't use the depreciated endpoint.

    I should note that this code was originally written against CRM 2011. We've upgraded to every major version since then. This is the first time I've had an issue with the lookup field returned from a SOAP call.

    FetchUtil.prototype._ExecuteRequest = function (sXml, sMessage, fInternalCallback, fUserCallback) {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("POST", this.server + "/XRMServices/2011/Organization.svc/web", (fUserCallback != null));
        xmlhttp.setRequestHeader("Accept", "application/xml, text/xml, */*");
        xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        xmlhttp.setRequestHeader("SOAPAction", "schemas.microsoft.com/.../Execute");
        //try { xmlhttp.responseType = 'msxml-document'; } catch(e){}
    
        if (fUserCallback != null) {
            //asynchronous: register callback function, then send the request. 
            var crmServiceObject = this;
            xmlhttp.onreadystatechange = function () { fInternalCallback.call(crmServiceObject, xmlhttp, fUserCallback) };
            xmlhttp.send(sXml);
        }
        else {
            //synchronous: send request, then call the callback function directly
            xmlhttp.send(sXml);
            return fInternalCallback.call(this, xmlhttp, null);
        }
    }


  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Again not complete. There is no code for _FetchCallback method. I would suggest you instead of posting all the code try to troubleshoot your code using JavaScript debugger (I use Chrome for it). I'm pretty sure that your code incorrectly parses response and that's why you got wrong result.

  • J Matlon Profile Picture
    1,465 on at
    case "a:EntityReference":
                                    var entRef = new jsEntityReference();
                                    entRef.type = sType;
                                    entRef.guid = attr.childNodes[k].childNodes[1].childNodes[0].textContent;
                                    entRef.logicalName = attr.childNodes[k].childNodes[1].childNodes[1].textContent;
                                    entRef.name = attr.childNodes[k].childNodes[1].childNodes[2].textContent;
                                    obj[sKey] = entRef;
                                    break;


    Thanks, Andrii. This was a parsing issue. I was finally able to locate the issue.

    However, this same code parses the response just fine for CRM versions 2011/2013/2015. The response format changed in CRM 2016, but I wasn't able to find out where this was documented.

    In my code linked, I can fix the issue by incrementing the final childNodes reference to 2 and 3 for logicalName and name respectively. I wasn't able to find anything from MS documenting this change.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans