Introduction
PowerApps Component Framework has numerous methods that are useful to developers in several ways. In developing one of my PCF controls, I ran across a case in which entity colors were required in our control.
I obtained this using the conventional method of calling the getEntityMetadata() for the respective entity and obtaining the EntityColor from it.
let metadata = await context.utils.getEntityMetadata("account"); let entityColor = metadata.EntityColor;
However, this leads to sending requests to the CRM as it is asynchronous, which usually degrades the performance of our PCF control.
Solution
In this case, the getEntityColor method comes to the rescue, where no request is sent to CRM. Thus, enhancing the performance of our PCF control. This method can be found in theming under context.
context.theming.getEntityColor(entityName);
Using the above method, we can get the entity color in HEX format as we add them in customizations.
As shown in the below screenshot, we have set the color for the account entity as #794300.
Scenario
Let us consider, we are working on building a dropdown in PCF which shows the entities/tables of CRM. Users can select any of these entities from the dropdown for further operations.
However, just showing entity names will make it look dull and uninteresting. So, to lighten up this scenario we will add a colorful box, which will be colored with the respective entity colors.
Another scenario from this can be the user may need to know the color of the entity before selecting the entity option.
Here, we use a Dropdown component of Fluent UI and render the options in it.
return ( ); //method to render an option into the dropdown field const _renderOption = (option: any): any => { let entityName = option.key; return ( { //@ts-ignore } {/* text of an option set field */} {option.text} ); }
Note: This method is not documented so use a fallback for this method in terms of any future untoward.
Conclusion
In this way, we learned how to get the entity color without sending a request with getEntityMetadata()