Some fancy facts about Command Bar in Model Driven Apps
Command Bar in Model-Driven App

Some fancy facts about Command Bar in Model Driven Apps

The orher day I had to build a request management app for one of my clients. Overal architecture was pretty simple:

  • Dataverse to host several entities;
  • MDA as front end.

To make life easier in terms of provide client with ability to have combined dashboards for all types of request it was decided to create main entity "Request" (to host all records about rised requests) and several related entities (e.g. "HR Request", "Copy Request", "Office Supply Request", etc.). All realted entites were linked to "Request" table using simple lookup column (relation many-to-one).

General logic was as following, user on web portal creates request by selecting needed request type and filling in respective request form. Under the hood, intially record to "Request" table was added (contains just simple coomon request data), new record with details was added to related target table and lookup field was updated with recently created record from "Request" entity.

Further business logic assumed some request management flow with assigning of Request Fullfiller and passing request throgh several statuses (New -> Assigned -> In Progress -> Completed).

To manage each request type I decided to build MDA displying forms to every target entity. So, this means that user was inteacting with target table record, but there was a need to update parent "Request" record accordingly.

For this I went for custom Command Bar buttons embedded into Dataverse form.

So, I created needed entity form, added it to MDA and created couple of simple buttons to manage request.

Article content
Custom commands for form's command bar

I assigned some simple Power Fx fomula to eah button (fromo nowon we will consider "Assign Fullfiller" button).

If(IsBlank(Self.Selected.Item.'Request Fullfiller'.'User Name'),
    Confirm("You have to specify fulffiler in order to assign this request!",{ConfirmButton:"Ok"}),
    Patch('Copying Requests', 
        Self.Selected.Item,
        {
            'Request Assigned On': Today(), 
            'Request Status': 'Request Status Choice'.Assigned   
        }
    );
    Patch(Requests, 
        LookUp(Requests, Request = Self.Selected.Item.Request.Request),
        {
            'Request Fullfiller': Self.Selected.Item.'Request Fullfiller',
            'Request Assigned On': Today(), 
            'Request Status': 'Request Status Choice'.Assigned
        }
    );
);        

this code simply validates "Request Fullfiller" wild and in case it is not blank updates current record in target entity "Copying Request" and parent record in "Request" entity.

Looks really straightforward and should not causes any issues. But, this is Microsoft!!!

Once testing started the issue related to assigning the request was recorded. The user opened request form (which is "Copying Request" entity) and hit "Assign Fullfiller" button - request record in "Copying Request" entity was updated with status, but linked record in "Request" entity was not.

So, to me that looked reqlly wired, cause code was simple and obvious. Thus I added couple of notifications just to debug and see what causes the issue.

    Notify("Test - "&Self.Selected.Item.Request.Name);
    Notify("Request Id - " & LookUp(Requests, Name = Self.Selected.Item.Name).Name);        

And tese result were pretty interesting

Article content
Test results

Which means it was not possible to retrieve lookup column for this item. But I was pretty sure the lookup value exists (I checked that via direct access to entity records via Dataverse)

Article content
Copying Request entity
Article content
Request entity

I spent couple of hours trying to get lookup field value one way or another but no success. But the fact was, that the other request was working perfectly and this was very embarassing because all looks the same. So, I compared all the component and pieces of code used in "Copying Request" and "Metro Card Request". And I did find the difference

Article content
Metro Card Request form

As this is clearly seen from images I have populated "Request" lookup column to the item form for "Metro Card Request" but did not for "Copying Request".

Article content
Copying Request form

On "Copying Request" form I left lookup column unpopulated instead primary name column "Request" is displayed (on "Metro Card Request" it is hidden).

So, after adding lookup column to "Copying Request" the issue was resolved.

I was thinking about this situation and concluded, that it is possible to access record's field only been populated on the form (if the filed is not on the form canvas its value would not be accessible within Self.Selected.Item object despite you would be able to have it stated without any error message). From one side it looks a bit strage and confising. However, it could be a case that MS decided to treat entity's form object in the same way as views - with view you are not able to sort it by the field which is not with the view.

The only point I am not happy with - there is not MS documentation on this matter. Let's say, having some sort of KB article it would save me couple of hours my time. However, now this article existis and might help one of you.

Be safe and happy coding/making :)

To view or add a comment, sign in

More articles by Pavel Rudenko

Insights from the community

Others also viewed

Explore topics