简体   繁体   中英

Dynamics CRM web api FetchXml returns linked Entities naming with '2_x002e_' and similar in between

Is there any way to bypass that returned values of fetchXML returns correct naming of linked entity logical name, not build naming, like :

customer_contact1_x002e_contactid
customer_contact1_x002e_fullname  

I removed all aliases from requests. Is it something, that can not be fixed in request? Or is it a 'tail' putted by Microsoft CRM to specify entities relationships and all I can do is to work with that in response?

The _x002e_ represents the dot (.) which separates the prefix from the attribute name. (It's the character code in hexadecimal form.) In JSON the unencoded dot would lead to conversion errors.

You could consider to convert the hex code to any text using a JSON reviver function. (See MDN .)

As @Henk van Boeijen stated already correctly:

The _x002e_ represents the dot (.) which separates the prefix from the attribute name. (It's the character code in hexadecimal form.) In JSON the unencoded dot would lead to conversion errors.

TL;DR

The easiest way to handle this is to just hardcode the entityname (alias), the hexadecimal dot and the attribute name. Then concatenate them all together.

obj['myopportunityalias'+ '_x002e_' + 'closeprobability'];

Example

This example shows how to remap the properties of the parsed object. Even for this approach it is needed to determine the original property by concatenating the three parts as seen above. Therefore, I do not see any benefit in doing so.

Another approach would be to check the properties with a regular expression similar to this: /entityalias_.+_attributename/

FetchXML

    <fetch>
      <entity name="account" >
        <attribute name="name" />
        <link-entity name="opportunity" from="parentaccountid" 
             to="accountid" link-type="outer" alias="myopportunityalias" >
          <attribute name="closeprobability" />
        </link-entity>
      </entity>
    </fetch>

JS

 var json = '{"account": "Contoso", "myopportunityalias_x002e_closeprobability": 99 }'; obj = JSON.parse(json); var alias = 'myopportunityalias' var attribute = 'closeprobability' var fullKey = alias + '_x002e_' + attribute; console.log(obj[alias + '_x002e_' + attribute]); // just concatenate or hardcode it var newObj = {}; for (var prop in obj) { if (obj.hasOwnProperty(fullKey)) { obj[alias + attribute] = obj[fullKey]; delete obj[fullKey]; } } console.log(obj.myopportunityaliascloseprobability); console.log(obj[alias + attribute]);

Bit late in the day but I found this article looking for an answer to this problem.

There's actually a very easy way to deal with it rather than developing a reviver. If you alias your attributes in the FetchXML, you'll remove the dot notation from the results and you can reference the result's column directly through the alias name. The following returns the depth of privilege for the current user for Creating Notes.

<fetch>
  <entity name='role' >
    <link-entity name='systemuserroles' from='roleid' to='roleid' alias='userroles' intersect='true' >
      <filter>
        <condition attribute='systemuserid' operator='eq-userid' />
      </filter>
    </link-entity>
    <link-entity name='roleprivileges' from='roleid' to='roleid' intersect='true' >
      <attribute name='privilegedepthmask'  alias='privdepth' />
      <link-entity name='privilege' from='privilegeid' to='privilegeid'>
        <filter>
          <condition attribute='name' operator='eq' value='prvCreateNote' />
        </filter>
      </link-entity>
    </link-entity>
  </entity>
</fetch>`

Yields the result:

[{ 
    roleid: "b2d0dc99-81cf-e711-9669-00155d0e5f01", 
    privdepth: 8 
}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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