简体   繁体   中英

CRM 4.0 - FetchXML to retrieve data from related entity

I'm trying to use FetchXML to pull in a list of courses with an 'entry year' related entity. What I would like to do is only return a single record for each course (could return multiple courses) with the latest year (eg I would want it to pick the last year out of 2012, 2013, 2014 - so in this case 2014). So I currently have:

<fetch mapping="logical" distinct="true">
    <entity name="course">
        <all-attributes/>
        <order attribute="name" />
        <link-entity name="course_entryyear" from="courseid" to="courseid">
            <link-entity name="entryyear" from="entryyearid" to="entryyearid">
                <attribute name="year" />
            </link-entity>
        </link-entity>
    </entity>
</fetch>

Is this possible to do within FetchXML and if so how can I amend the above?

Cheers

What about adding count="1" and inner join to the linked entities along with some ordering:

So it would look like:

<fetch mapping="logical" distinct="true">
    <entity name="course">
        <all-attributes/>
        <order attribute="name" />
        <link-entity name="course_entryyear" from="courseid" to="courseid" link-type="inner">
            <link-entity name="entryyear" from="entryyearid" to="entryyearid" link-type="inner" count="1">
                <attribute name="year" />
                <order attribute="year" descending="true"/>
            </link-entity>
        </link-entity>
    </entity>
</fetch>

Depending on how you are displaying the course list you might want to change the link-type for the "course-entryyear" from inner to outer , so that all courses get displayed even if they haven't been served yet.

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