Skip to main content

Notifications

Microsoft Dynamics CRM forum
Answered

Fetch Query Expression

Posted on by 160

Hi

I am using this query in my plugin to fetch the data of a child table. But in this xml code the condition is set to a specific record(A). I want to use this code for all records associated with PlanType table record via Lookup. how i can do this.

<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='tpamm_plantyperequirement'>
<attribute name='tpamm_name' />
<attribute name='tpamm_limitbyage' />
<attribute name='tpamm_agecriteria' />
<attribute name='tpamm_age1' />
<attribute name='tpamm_age2' />
<attribute name='tpamm_limitmaxperson' />
<attribute name='tpamm_maxperson' />
<attribute name='tpamm_plantyperequirementid' />
<order attribute='tpamm_name' descending='false' />
<filter type='and'>
<condition attribute='tpamm_plantype' operator='eq' uiname='A' uitype='tpamm_plantype' value='{645ACDBA-8BC7-EC11-A7B5-002248D5CE0C}' />
</filter>
</entity>
</fetch>"

I am writing a plugin on Table A which has lookup field from "Plan Type" Table. "Requirement" Table also have a lookup field from table "plan Type". I used above mentioned Fetch query to get data from Requirement table. Please refer this info of my project to answer my question.

pastedimage1651642001239v1.png

Categories:
  • Suggested answer
    happysingh0009 Profile Picture
    happysingh0009 160 on at
    RE: Fetch Query Expression

    Thank you for your reply. I was passing the dynamic value to fetchquery but in the same way as we pass static value. that was the error. Thank u for suggestion to concatenate the Expression. its working now. :)

  • happysingh0009 Profile Picture
    happysingh0009 160 on at
    RE: Fetch Query Expression

    showing same  error.

  • Verified answer
    Guido Preite Profile Picture
    Guido Preite 54,063 Moderator on at
    RE: Fetch Query Expression

    if your fetchxml is stored inside a string you should do something like

    ...<condition attribute='tpamm_plantype' operator='eq' value='" + PlanType.Id +"' />...

  • happysingh0009 Profile Picture
    happysingh0009 160 on at
    RE: Fetch Query Expression

    PlanType.Id= Guid.NewGuid();

    <condition attribute='tpamm_plantype' operator='eq' value='PlanType.Id' />

    i tried above statements to set GUID in the condition, but it is giving error of {An exception System.FormatException was thrown while trying to convert input value 'PlanTypeRef.Id' to attribute 'tpamm_plantyperequirement.tpamm_plantype'. Expected type of attribute value: System.Guid. Exception raised: Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).}

  • Verified answer
    Guido Preite Profile Picture
    Guido Preite 54,063 Moderator on at
    RE: Fetch Query Expression

    ok, now your requirement is more clear.

    if your plugin runs on the tpamm_plantype entity, inside your plugin you probably will have this kind of code:

    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    Entity currentRecord = null;
    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
    {
        currentRecord = (Entity)context.InputParameters["Target"];
        if (currentRecord.LogicalName != "tpamm_plantype") { return; }
    }

    so inside currentRecord.Id you will have the GUID of the current tpamm_plantype

    after you edit your fetchxml to change the guid defined there with the guid coming from current record and you can fetch the related table (keep in mind that for the fetch you don't need uiname an uitype

    hope it helps

  • happysingh0009 Profile Picture
    happysingh0009 160 on at
    RE: Fetch Query Expression

    Please check my question again, I added some more information regarding my plugin.

  • happysingh0009 Profile Picture
    happysingh0009 160 on at
    RE: Fetch Query Expression

    if i remove condition then how my plugin (On parent table) will found the associated record in child table. As that condition is presenting a lookup relationship between 2 table , i am getting record associated with record in parent table.

    i tried without condition, in this case the plugin is providing any random record but i need a record associated with the parent record.

  • Guido Preite Profile Picture
    Guido Preite 54,063 Moderator on at
    RE: Fetch Query Expression

    you just put a fetchxml query, you didn't post your plugin code or what this plugin is achieving.

    right now that fetchxml return "tpamm_plantyperequirement" records when the "tpamm_plantype" lookup is equals to that guid, if you remove the whole filter the query will return all the "tpamm_plantyperequirement" records (up to 5000), what you do with the returned result (few records or all records) depends on your logic, not on this fetchxml query

  • happysingh0009 Profile Picture
    happysingh0009 160 on at
    RE: Fetch Query Expression

    hi sir

    thax for reply

    my table has a lookup field. if I delete this condition how this plugin work with lookup field.

  • Suggested answer
    Guido Preite Profile Picture
    Guido Preite 54,063 Moderator on at
    RE: Fetch Query Expression

    you need to remove the whole filter if you want to retrieve the records but you are limited to 5000 records

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

Dynamics 365 Community Update – Sep 16th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,522 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,441 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans