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 :
Customer experience | Sales, Customer Insights,...
Answered

Xrm.WebApi challenge (retrieveRecord)

(0) ShareShare
ReportReport
Posted on by 25

Good evening everyone,

Alright. I'm facing a Xrm.WebApi challenge regarding retrieveRecord. I'm in an entity describing a report. This report entity is related to a main location, say a house. This house is divided in floors. The floors are divided in rooms. The report covers one or more rooms.

Now, a form allows users to select one or more rooms to report on. This is done by means of a subgrid. This subgrid has a QuickView/LookUp view connected to it. This LookUp can be filtered. I had a success with the method described here for filtering on floor, so that users can select rooms for the floor the report is linked to;
https://blog.magnetismsolutions.com/blog/paulnieuwelaar/2018/05/17/filter-n-n-add-existing-lookup-dynamics-365-v9-supported-code

Floor has a direct link to report, so it can be grabbed using the WebApi. With that, a simple filter can be returned. On Success of the WebApi call that is.

However, users must be able to report on rooms on different floors. So, now I need to do an API call that:

  1. retrieves the current floor from the report
  2. retrieves the house from the current floor
  3. with that house retrieve all floors
  4. and than create a filter that retrieves all rooms on all floors in that house

Can this be done using the "$expand=" option? It seems impossible to retrieve entities one by one. The promises resolve in their own time, I wouldn't know how to handle first grabbing the current floor from the report, than the house, than the floors in that house and lastly the floors, create a filter, set that filter and lastly make the Quick View pop up.

The filter I want to generate can be done in XrmToolBox, and that filter works. It can be set using the method described in the linked article. It looks as follows:

<fetch>
<entity name="room">
<filter>
<condition attribute="floor" operator="in">
<value>b48137a9-b80c-ec11-b6e6-000d3a5bddf1</value>
<value>097c09e9-6be9-eb11-bacb-000d3a5aa426</value>
... more values
</condition>
</filter>
</entity>
</fetch>

So, hard-coding this filter works, but the challenge is to dynamically generate it. How would I go about creating such a WebApi $select$expand statement?

Enjoy the weekend,
- Alex.

I have the same question (0)
  • Verified answer
    Pradeep Rai Profile Picture
    5,489 Moderator on at

    Hi Alex,

    please find below solution as per below relationship:

    1. Report Entity Has Lookup of Floor entity and Subgrid of Rooms
    2. Relationship: House 1 --> * Floor and Floor 1 --> * Rooms

    One House Multiple Floor

    One Floor Multiple Rooms

    1. retrieves the current floor from the report.
    Answer: From FormContext, we can get the Floorlookup value and Store it in Variable.

    2. retrieves the house from the current floor.

    Answer: Once Floor retrieved from formcontext then we can retrieved the house using Xrm.WebAPI.retrieveRecord.

    3. with that house retrieve all floors

    Answer: Now in Success call back of above function we need to retrieve the all floors using Xrm.WebAPI.retrieveMultiple.

    4. and than create a filter that retrieves all rooms on all floors in that house

    Answer: Now in Success call back of above function we need to retrieve the all rooms using Xrm.WebAPI.retrieveMultiple.

    below is sample code snippet for your reference

    function onChange(executionContext){
        var floor= formContext.getControl('floorlogicalName').getValue();
        //get house lookup from floor
        Xrm.WebApi.retrieveRecord("floorentitylogicalName", floor.id, "?$select=housefieldlogicalName").then(
            function success(result) {
                var houselookup=result.entities[0]['houselookuplogicalname'];
    
    
            },
            function (error) {
                console.log(error.message);
                // handle error conditions
            }
        );
        
    }
    
    function getAllFloors(houselookupid)
    {
        var  fetchXMl=""   
        ""    
           ""    
           ""    
            ""    
                 ""    
               ""       
        ""    
     "";
    var flooridList=[];
    Xrm.WebApi.retrieveMultipleRecords("floorentitylogicalname", "?fetchXml=" fetchXMl).then(
        function success(result) {
            for (var i = 0; i < result.entities.length; i  ) {
               flooridList.push( result.entities[i]['floorentitylogicalnameid'])
            }      
            getAllRooms(flooridList);              
            // perform additional operations on retrieved records
        },
        function (error) {
            console.log(error.message);
            // handle error conditions
        }
    );
    }
    
    function getAllRooms(flooridList)
    {
    
        let filterCondition="";
    
        for (let index = 0; index < flooridList.length; index  ) {
            filterCondition  ="" array[index] "";
        }
    
        let fetchXMl=""   
        ""    
           ""    
           ""    
            ""    
                 ""    
                 filterCondition 
                 "" 
               ""       
        ""    
     "";
    
     Xrm.WebApi.retrieveMultipleRecords("roomentitylogicalname", "?fetchXml=" fetchXml).then(
        function success(result) {
            for (var i = 0; i < result.entities.length; i  ) {
                console.log(result.entities[i]);
            }                    
            // perform additional operations on retrieved records
        },
        function (error) {
            console.log(error.message);
            // handle error conditions
        }
    );
    }
        

    Thanks,
    pradeep.
    Please mark this as VERIFIED if it helps.
        
  • akersk Profile Picture
    25 on at

    Hi Pradeep,

    Thanks for your elaborate reply! This works! I'll mark it as verified.

    In my case, it works, but with a twist that makes it unusable. I'm using the linked method to set the filter on the lookup control, after a click on a button in the ribbon of the subgrid. The lookup control is in a quick view. Setting a filter works great, as long as no promise is involved. Everything gets resolved as it should, but always too late to be used for setting the filter on the lookup control. To get around this, I pre-render the filter as soon as I know the house entity. This way, it is ready for use, and everything works flawlessly.

    I'm curious to learn about any techniques to make rendering the lookup wait for the filter to be ready to be applied. If anyone had a success with this, please tell about it.

    Thanks again Pradeep! Best regards,

    - Alex.

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 81 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans