Thursday, July 18, 2019

Save time building Flows using Copy and Paste for your Actions

Microsoft just release the preview some long-awaited functionality that allows users to easily copy and paste actions for re-use.  This functionality will save makers significant amount of time when reusing actions within a Flow or across different Flows.

Copying Actions

To copy an action to the clipboard, all you need to do is click the ellipses and select Copy to my clipboard.

Once an action has been saved, you can add it as a new step by selecting My clipboard from the action chooser and clicking the action you want to paste.


You can even have a peek at the code by hovering on top of the (i) to the right of the action name



Save even more time with scopes

Scopes are collections of actions.  They are great if you want to move around a group of actions without having to move one by one.  With the addition of the Copy to my clipboard functionality, you are now able to copy an entire group of actions.  The caveat here is that variables cannot be initialized inside a scope.  So you have to be careful when you want to paste a scope somewhere within the Flow before the variables it's referring to are declared.

Copy actions across Flows

Yes, you read correctly!  The clipboard works across Flows as well.  But as with the scopes, you need to carefully review the copied actions.  If you are consistent with your naming convention, you may save some time here, as they may include references to dynamic content not found in the new Flow.

Clearing the Clipboard

The copied actions remain in the clipboard until you exit Flow completely. 


Note that this functionality is still in Preview, but will undoubtedly change the way we create Flows!



Saturday, May 4, 2019

Building Enterprise-wide Multilingual PowerApps

Supporting multiple languages for corporations is a reality. It may be an organization operating in multilingual countries, such as Canada, Belgium, Italy, or others, or having a workforce that that is distributed across the globe. One common challenge in building solutions to support multilingualism is the creation and maintenance of such solutions as they evolve and more languages are added. In this article, I'll be discussing an architecture to create enterprise-wide multilingual PowerApps.

The challenges that are often encountered with multilingual solutions stem from the fact that business needs evolve over time.  This could mean that an existing application needs to be altered to include additional content.  Another common need that arises is that a new language may have to be added to an application.

Back in 2017, Anh-Thu Chang wrote a blog on Building multilingual apps in PowerApps.  Some of the things discussed in this article relate to her blog post and build on the general concept.

Overview

The enterprise-wide solution proposed in this article will address key elements for creating an architecture that can support a number of languages across all PowerApps with an organization.  These elements include:
  • Language Determination
  • Central Label Repository
  • Central List Repository
  • Performance

Language Determination

There are numerous ways to change languages in Office 365 as described in my previous article, Working in different languages in Office 365.  For the solution described in this article, I'm using the Office 365 Account settings for the user.  By doing so, I'm able to access a users preferred language in PowerApps using the Office 365 Users connector. 

Language settings are made up of two components.  The first two letters, representing the language, a hyphen, and then two more letters representing the locale.  For example, English for Canada is represented as en-ca, while English for US is en-us.  In my case, I only care about the language and not the locale.

If you choose to capture both, you can definitely do so.  However, keep in mind that users within the same region may not pay attention to the locale and only pick the language.  If that is the case, then they may not experience the PowerApp in the language that they expect to.

When the app is launched, the following expression is added to the OnStart() property for the app:

Set(preferredLanguage, Coalesce( Left(Office365Users.MyProfileV2().preferredLanguage, 2), "en"));

The expression gets the user's preferred language from their Office 365 account setting.  It picks the two left-most characters and stores it in a global variable called preferredLanguage.  I use the Coalesce expression to set the default to English (en) to avoid situations, where a user does not pick a language or other reasons that the preferredLanguage may be empty.

Central Label Repository

The Central Label Repository is used to store all the strings that are used across all the apps.  For simplicity of management, I have chosen to use a SharePoint list to achieve this.  If you go down this route, you need to make sure that all your users will have at least read access to this list. 

There are four pieces of information that are needed for each label:

  • Title - a group for the label.  For this, I typically use the App name
  • Field Name - the name of the control that this label applies to.  
  • Label - what is the string that should actually appear
  • Lang - the two-letter language code


Reuse of Labels

There are often labels that are commonly used across numerous forms, such as OK, Cancel, Yes, No, etc.  Rather than defining them for each form, you can create a general group and put them in there.  This will ensure consistency between the labels used across the various forms and reduce the overall SharePoint list size.

Loading the labels

After the preferred language has been determined, each app needs to load the string labels into its memory.  This is done after the preferredLanguage has been determined.  The expression below loads the strings from the SharePoint list into a collection called StringLabels.  

ClearCollect(StringLabels, GetStringLabels.Run("Update My Profile", preferredLanguage)); 

Loading of the strings from SharePoint into the app can be done by either loading the entire SharePoint list and then filtering the values during run-time or using a Flow to filter out the specific labels required and returning those to the PowerApp.  When the list of items gets long, as in the case of defining labels for an entire organization, loading and filtering the entire list within the app may have an implication on performance.  This is why I elected to go with the latter method.  In the expression shown above, I have a Flow called Get String Labels to which I pass the name of the label group (e.g. Update My Profile) and the language.  The Flow then filters the list on these two columns and returns all relevant items


Setting Control Labels

Once the labels have been loaded into a collection, you can now use them to set the text of your controls.  For example, if I have a label called lblCity, then I set the Text property as follows:

Coalesce(LookUp(StringLabels,FieldName = "lblCity",Label), "lblCity")

The Lookup expression searches for an item, where the FieldName is lblCity and returns the Label value.  Coalesce is used to provide a default value in case the label has not been defined for the specific language.  In my case, I make the default Coalesce value the same name of the label as it makes it easier to visually know what labels are missing in my SharePoint list.

Central List Repository

Similar to the central label repository for static labels, you can create a SharePoint list that will include related, such as in drop down lists, radio buttons, and other collection.  The values captured for each list entry are a bit different from the labels:
  • Title - a group for the list item
  • Field Name - the name of the control that this list applies to 
  • Label - what is the string that should actually appear for the list entry
  • Field Order - in what order should the labels appear in the app
  • Lang - the two-letter language code

Loading the list items

Loading o the list items occurs in a way similar to the labels.  However, each group of items is stored in its own collection.  The expression below loads the options for rooms with curtain rods for a drop down list called drpCurtains.  

ClearCollect(drpCurtains,GetListItems.Run("Account", "drpCurtains", preferredLanguage));

Here, I use a similar Flow that filters the list based on the app, list field, and preferred language and sorts the results based on the FieldOrder


Setting List Items

Once the list items have been loaded loaded into a collection, all you need to do is make that collection the source of your list control.  For my drpCurtain field, the :

Distinct(drpCurtain,Label)

The Distinct ensure that there are no duplicate fields in my list.  Label indicates that from within the collection, the Label field values should be displayed in the list.

Performance

I have implemented the architecture described above in an organization that uses around 40 different forms with over 2,000 labels and 15,000 list values successfully.  There are, however opportunities to improve the performance.  Labels and list items are elements that typically don't change too often.  Therefore, rather than loading the values from a SharePoint list each time the app is opened, it would be better to first perform a query to see if values have changed.  If there were changes, then the labels and list items should be loaded from SharePoint and also stored in the app's offline cache.  If labels have not changed, then the labels and list items should be loaded from the app's cache. 

One way to implement such a check is to keep a timestamp of the last update for the list and before loading the labels check if there were any additions or changes before that date.

Conclusion

The need for multilingual solutions is increasing as organizations are expanding.  With the proliferation of business productivity solutions, the need to provide a localized user experience is also growing.  I hope this article provided you with a good overview on making your enterprise PowerApps a multilingual success.

Saturday, April 27, 2019

Adding a Flic button trigger to a Flow triggered by PowerApps

When building PowerApps, you can use Flow to perform a lot of actions that may be more complex than what you can or want to do directly within the PowerApp.  Flows are invoked via any a number of imperative properties of your PowerApp, such as


  • OnStart() - App
  • OnVisible(), OnHidden() - Screen
  • OnSelect() - Button, Text, Label, Drop down
  • OnCheck(), OnUncheck() - Checkbox
  • and more
When you connect a Flow to these controls' properties, then the Flow gets triggered when that action is performed.  For example, when you press a button or label, check or uncheck a Checkbox, or launch the PowerApp.  In essence, PowerApp itself provides the trigger to all these Flows.  

However, there are time, where you want something in PowerApp to occur when there is an external trigger.  For example, you want to update information in your PowerApp when a Flic IoT button is pressed.

The way this can be accomplished is using a Timer control in your App and a dual-trigger Flow.  Here's an example of how I built it..

Timer Control

The timer control, flicTimer, is responsible to do three things:
  1. Call the Flow
  2. Wait
  3. When completed, perform an action inside the App and restart
Here's a description of the property settings I've used:


PropertyValueNotes
Autostarttrueunless you need to manually invoke the listener Flow, start automatically
Duration12000make it long enough so that it wont time out, but not too short to cause multiple Flows to fire
OnTimerStartUpdateContext({clicked: GetFlicButton.Run().response});
If(clicked = "clicked", <do something>);
flicTimer.Reset
here's where you call the Flow and wait for a response. Once the Flow returned, I would perform a specific action
Repeattrueif the Flow didn't finish in time, repeat the timer

Flow

The Flow in this case is fairly simplistic.  It begins with the PowerApp triggering it.  Once triggered, the secondary trigger, When a Flic is pressed, is called to wait until the button is pressed.  There are two possible outcomes to this:
  1.  The button was pressed.  In that case, the branch on the right is executed and returns the "clicked" response to the PowerApp.
  2. The button was not pressed.  Here, the Flow will eventually time out and return an empty result to PowerApp.  


A similar logic can be applied to other types of triggers that may not be easily accessible within your PowerApp.

Thursday, March 21, 2019

Blanking out date fields in Dynamics 365 using a Flow

Dynamics 365 supports a number of base content types naively that can be set via Flow.  However, date fields are a bit finicky.  When trying to set a date field, you can provide the date in a string format.  But what do you do if you need to clear out an existing value in a date field in a Dynamics 365 record?

Turns out that the Dynamics 365 connector is not behaving the way most of us would expect.  Leaving the field blank will not change the value.  If you add any value into the date field other than a date field, the action will fail.  

The way to do it is by using the following steps:
  1. Add a Convert time zone action.  If you're not actually converting the time, then set the Source time zone and Destination time zone to be the same.
  2. Assign the Base Time you want for the field.  If you want to clear it out, set it to the null expression.  null will cause the time conversion operation to fail, but still creates a time object.
  3. In the action immediately following the Convert time zone action, set the Configure run after to run after success and failure.

You will need to do this for every date field that you may want to reset.

Saturday, March 16, 2019

Building Responsive(ish) PowerApps

Providing users with the ability to access content on their mobile devices has become common place and somewhat of a necessity today.  In the best case, it would be good to have a single solution that can serve multiple devices and orientations.  This article discusses how you can achieve this for PowerApps.




App Settings

Screen size + orientation

Start with creating a PowerApp Canvas App using a Phone layout and orient it to landscape.  On the Screen size + orientation layout page, make sure you disable Scale to fit.  Disabling this setting will allow your app controls to remain the same size regardless of the resolution.  Also disable the Lock orientation so your app will work in both portrait and landscape orientation.  You will notice near the top of the page that the resolution of the app is set to 1136 x 640.   Remember this number as we'll get back to it soon.



Advanced Settings

Next, go to the Advanced Settings tab and enable the Try the enhanced Group control.  Not only does this control help with accessibility, but it also allows you to position your controls on the screen  as a group.


At this point, you will be able to build an app that will have consistent component sizes.  On to the fun part. 

Dealing with screen size and orientation

Each screen has two properties that can be used to determine its size and orientation - Width and Height

Determining how to deal with screen sizes

When you query the Width and Height, it's easy to determine what orientation you are in.  If the Height is greater than the Width, then the orientation is portrait.  Otherwise, it's landscape.  For simplicity, you can store the orientation in a context variable for reference.

If(Screen1.Width < Screen1.Height,
UpdateContext({orientation:"portrait"}),
UpdateContext({orientation:"landscape"}));

Now to the device and screen size.  Responsive design uses something called Break Points and Media Queries to determine how to render a web page based on the browser size.  We can do something similar in PowerApps.  In my example, I decided that anything with a Width or Height greater than 2000 should be considered a tablet while anything smaller is a phone.  The exception is that if the width is exactly 1136 and the height is 640 then it's a Desktop.  The reason for this last scenario is that PowerApps renders the app using these dimensions in the browser.  So, I determined the device type as follows:

If(Or(Screen1.Width > 2000,Screen1.Height > 2000),
UpdateContext({device:"tablet"}),
If(And(Screen1.Width = 1136,Screen1.Height = 640),UpdateContext({device:"Desktop"}),UpdateContext({device:"phone"}))


This approach is conceptual and you can define other rules to determine how the app should render. 

What happens when a device changes orientation

So far, I've discussed how to determine the orientation and device type.  But when are these determined?  One way is to set them using the OnVisible property for the screen.  This will work, but will not change when the phone or tablet is changing orientation.  What you can do to address this is add a timer to your screen and add these settings to the OnTimerEnd property.  Set the Duration to be short (e.g. 100 ms) and make sure the AutoStart and Repeat properties are set to true so that the timer will begin as soon as the screen is displayed and will continue to check these settings.

Organizing your controls on a screen

Group components

Earlier in the article, I mentioned that the experimental Group components control should be used.  This control is great for developing responsive apps as it allows you to easily move groups of controls around on the screen rather than treating each one separately.

Make everything relative

Every control has a number of properties that are used to determine where it is positioned on a screen and how big it is.  As well, some controls that are text based have settings to indicate the font family, size, and weight.   When you add controls to a screen, these settings have default fixed values.  In order to allow you to better control the overall layout of your app, its better to use relative values.  This way, when one control changes, all other controls relative to it will change.  For example, in the image below, I am forcing the left group control to be 10px from the top and left of the screen border.  Likewise, I'm setting the Device and Screen Width labels to be 10px from the top and left inside their Group components controls.  All other controls are set to be spaced 10px apart across and down the page.  

For the Group components control on the right, I'm determining its location based on the orientation.  If it's a landscape orientation, then I set the X positions to be 10px after the end of the right Group components control.  Otherwise, I set it at 10px and below the right group control.  


So, for the example above, the Group component on the left is grp1 and the one on the right is grp2.  The positions for grp2 are determined as follows:

  • X: If(orientation = "portrait",10,grp1.X+grp1.Width+10)
  • Y:  If(orientation = "portrait",grp1.Y+grp1.Height+10,10)
Building on this approach, I also defined context variables for the font size and control widths and heights so that smaller devices will have larger fonts and boxes to make them easier to read.  The full expressions for the resizing in my case are:

If(Screen1.Width < Screen1.Height,
UpdateContext({orientation:"portrait"}),
UpdateContext({orientation:"landscape"}));
If(Or(Screen1.Width > 2000,Screen1.Height > 2000),
UpdateContext({device:"tablet",fontsize:30,height:50,width:300,width2:200}),
UpdateContext({device:"phone",fontsize:40,height:70,width:400,width2:300}));
If(And(Screen1.Width = 1136,Screen1.Height = 640),
UpdateContext({device:"Desktop",fontsize:14,height:25,width:140,width2:100,orientation:"landscape"}))

Conclusion

Using the techniques discussed above can, you can build apps that will resize their controls based on the device used and the way it's oriented.  The approach can be further expanded on to show/hide certain controls.