How to Display Party List Fields

Requirement

Do you want to display Party List fields, such as To and From, in Microsoft Power Pages? If yes, this article will help you achieve that using a quick code.

Details

Recently, while working on a customer’s environment, we received a request to display email details in a Power Pages subgrid. The To and From fields were not displaying correctly.

The reason is that Party List attributes are currently not supported directly in Power Pages Entity Lists and Subgrids. When you try to display a Party List field, Power Pages may show [object Object] instead of the actual email address.

Initially, I thought about creating two additional text fields for the To and From attributes. The idea was to copy the values from the original Party List fields into these text fields and then display them in the subgrid.

However, before implementing this workaround, I decided to search for an existing solution. During my research, I came across below post:

How to Render Party List Fields on Entity Lists in Power Apps Portals

The solution worked well, but we had one additional requirement in our environment.

Our customer allowed users to send emails using unresolved email addresses. This means users could enter an email address that was not associated with any existing Contact, Account, or other Dataverse record.

With the original solution, these unresolved email addresses were displayed as blank values.

To handle this scenario, I made some modifications to the existing code so that both resolved and unresolved email addresses could be displayed correctly in Power Pages.

$(document).ready(function() {

    $("#GRIDNameChange .entity-grid").on("loaded", listLoad);

});

function listLoad() {

   
    var gridRows = $(this).children(".view-grid").find("tr[data-entity='email']");

    if (gridRows.length > 0) {

        gridRows.each((i, row) => {

	        var fromCell = $(row).find("td[data-attribute='from']");

            var toCell = $(row).find("td[data-attribute='to']");       

            var fromEntityCollection = fromCell.data('value').Entities;

            var toEntityCollection = toCell.data('value').Entities;

            var fromNames = "";

            var toNames = "";

            fromEntityCollection.forEach(entity => {

                var partyAttribute = entity.Attributes.find(x => x.Key == "partyid");

                if (partyAttribute == null) return;

                fromNames += partyAttribute.Value.Name + ',';

            });

            if (!fromNames.trim()) {

                fromEntityCollection.forEach(entity => {

                    var addressAttribute = entity.Attributes.find(
                        x => x.Key == "addressused"
                    );

                    if (addressAttribute && addressAttribute.Value) {
                        fromNames += addressAttribute.Value + ',';
                    }

                });

            }

           
            fromNames = fromNames.replace(/,\s*$/, '');

            toEntityCollection.forEach(entity => {

                var partyAttribute = entity.Attributes.find(x => x.Key == "partyid");

                if (partyAttribute == null) return;

                toNames += partyAttribute.Value.Name + ',';

            });
 if (!toNames.trim()) {

                toEntityCollection.forEach(entity => {

                    var addressAttribute = entity.Attributes.find(
                        x => x.Key == "addressused"
                    );

                    if (addressAttribute && addressAttribute.Value) {
                        toNames += addressAttribute.Value + ',';
                    }

                });

            }
            toNames = toNames.replace(/,\s*$/, '');

            $(toCell).text(toNames);

            $(fromCell).text(fromNames);

        });

    }

}

We can use this code in the Custom Javascript section under Advanced tab in your web page. Make sure to change name of the grid according to your grid.

Hope it will help someone!!
Keep learning and Keep Sharing!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you human? Please solve:Captcha