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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

FetchXML issue

(0) ShareShare
ReportReport
Posted on by 2,512

Hi, I am trying to query incident through the below fetchxml, it seems working, but only "createdon",and "title" columns, the "statecodename" and "primarycontactidname"  are missing, I think coz they lookup values, how can I get the values then.

<fetch top="50" >
    <entity name="incident" >
        <attribute name="primarycontactidname" />
        <attribute name="statecodename" />
        <attribute name="title" />
        <attribute name="createdon" />
        <link-entity name="recoserv_participant" from="recoserv_incidentparticipantid" to="incidentid" link-type="inner" >
            <filter>
                <condition attribute="recoserv_participant" operator="eq" value="9D488E77-79AF-E711-A81C-005056B5FEFA" />
            </filter>
        </link-entity>
    </entity>
</fetch>


*This post is locked for comments

I have the same question (0)
  • Verified answer
    Ivan Ficko Profile Picture
    1,380 on at

    You need to use related entities to get primary contact full name via fetch XML. State code name cannot be directly fetched via fetch XML you can get only option set integer value.

    Example of FetchXML that returns full name of primary contact is:

    <fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
      <entity name="incident">
        <attribute name="title" />
        <attribute name="incidentid" />
        <attribute name="caseorigincode" />
        <order attribute="title" descending="false" />
        <link-entity name="contact" from="contactid" to="primarycontactid" visible="false" link-type="outer" alias="c">
          <attribute name="fullname" />
        </link-entity>
      </entity>
    </fetch>
  • Suggested answer
    Joe Gill Profile Picture
    on at

    Have a look at the FetchXml Builder in XrmToolBox https://www.xrmtoolbox.com/

    Joe

  • sdnd2000 Profile Picture
    2,512 on at

    but for statecodename, it still needs related entity?

  • Ivan Ficko Profile Picture
    1,380 on at

    You need to fetch it with metadata request, it's not accessible directly via FetchXML query. You can get it for example via Web API request with formatted values included.

  • Verified answer
    ashlega Profile Picture
    34,477 on at

    Hi sdnd,

    you can probably just leave primarycontactid and statecode there.. Though it depends on what you need it for. In SSRS, you'll get those names as added columns automatically. In javascript, you'll be able to access names as well.

    For example, here is how the resulting XML looks like in the FetchXml tester (XrmToolBox) - all the data is there:

    <result>
    <primarycontactid name="Gary Schare" yomi="Gary Schare" type="2">{5BA6E5B9-88DF-E311-B8E5-6C3BE5A8B200}</primarycontactid>
    <statecode name="Resolved">1</statecode>
    <title>Product feature information required</title>
    <createdon date="9/30/2017" time="2:45 PM">2017-09-30T14:45:39-04:00</createdon>
    </result>

  • Suggested answer
    Ivan Ficko Profile Picture
    1,380 on at

    Here is an example how you can fetch both via Web API call

    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: Xrm.Page.context.getClientUrl() + "/api/data/v8.2/incidents?$select=casetypecode&$expand=primarycontactid($select=fullname)",
        beforeSend: function(XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("OData-MaxVersion", "4.0");
            XMLHttpRequest.setRequestHeader("OData-Version", "4.0");
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
            XMLHttpRequest.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
        },
        async: true,
        success: function(data, textStatus, xhr) {
            var results = data;
            for (var i = 0; i < results.value.length; i++) {
                var statecode = results.value[i]["statecode"];
                var statecode_formatted = results.value[i]["statecode@OData.Community.Display.V1.FormattedValue"];
                var primarycontactid_fullname = results.value[i]["primarycontactid"].fullname;
            }
        },
        error: function(xhr, textStatus, errorThrown) {
            Xrm.Utility.alertDialog(textStatus + " " + errorThrown);
        }
    });

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans