5 reasons to use Peek code in Microsoft Flow.

Have you used the peek code option in Microsoft Flow yet? As a low-code development environment the first response might be why would I use the peek code option. Does this option help? The peek code option has been there for quite a while in Logic Apps but it is still quite new to Flow. In this post I’m giving a number of reasons when you might want to use it.

1. Different connectors doing the same or maybe not?

Within Microsoft Flow there are two connectors that do exactly the same. The Common Data Services and Dynamics 365, but how do you know what the differences are if there are any.

When looking at the Create a new record 2 (Preview) action in the Dynamics 365 connector and the Create a new record action in the Common Data Services connector then the differences are clear very quickly.

Dynamics 365:

{
"inputs": {
"host": {
"connection": {
"name": "@json(decodeBase64(triggerOutputs().headers['X-MS-APIM-Tokens']))['$connections']['shared_dynamicscrmonline']['connectionId']"
}
},
"method": "post",
"body": {
"name": "",
"merged": false,
"_accountratingcode_label": "Default Value",
"_address1_addresstypecode_label": "",
"_address2_addresstypecode_label": "Default Value",
"_businesstypecode_label": "Default Value",
"_accountcategorycode_label": "",
"_accountclassificationcode_label": "Default Value",
"creditonhold": false,
"_customersizecode_label": "Default Value",
"donotbulkemail": false,
"donotbulkpostalmail": false,
"donotemail": false,
"donotfax": false,
"donotpostalmail": false,
"donotphone": false,
"followemail": true,
"_industrycode_label": "",
"marketingonly": false,
"_ownerid_type": "",
"_ownershipcode_label": "",
"participatesinworkflow": false,
"_paymenttermscode_label": "",
"_preferredappointmentdaycode_label": "",
"_preferredcontactmethodcode_label": "Any",
"_preferredappointmenttimecode_label": "",
"_customertypecode_label": "",
"donotsendmm": false,
"_shippingmethodcode_label": "Default Value",
"_statuscode_label": "",
"_territorycode_label": "Default Value"
},
"path": "/v2/datasets/@{encodeURIComponent(encodeURIComponent('org0688189e.crm11'))}/tables/@{encodeURIComponent(encodeURIComponent('accounts'))}/items",
"authentication": {
"type": "Raw",
"value": "@json(decodeBase64(triggerOutputs().headers['X-MS-APIM-Tokens']))['$ConnectionKey']"
}
}
}

Common Data Services:

{
"inputs": {
"host": {
"connection": {
"name": "@json(decodeBase64(triggerOutputs().headers['X-MS-APIM-Tokens']))['$connections']['shared_commondataservice']['connectionId']"
}
},
"method": "post",
"body": {
"name": "",
"merged": false,
"creditonhold": false,
"donotbulkemail": false,
"donotbulkpostalmail": false,
"donotemail": false,
"donotfax": false,
"donotpostalmail": false,
"donotphone": false,
"followemail": true,
"marketingonly": false,
"participatesinworkflow": false,
"donotsendmm": false,
"_ownerid_type": ""
},
"path": "/v2/datasets/@{encodeURIComponent(encodeURIComponent('org0688189e.crm11'))}/tables/@{encodeURIComponent(encodeURIComponent('accounts'))}/items",
"authentication": {
"type": "Raw",
"value": "@json(decodeBase64(triggerOutputs().headers['X-MS-APIM-Tokens']))['$ConnectionKey']"
}
}
}

Both these connectors call version 2 (v2) of the API however the CDS connector only sends a minimum amount of data to this service. As I complete the configuration of the action more fields are added added to the code but it looks like the data sent is more optimised.

Therefor if you find that one action works and the other doesn’t work for you then the differences can be identified by comparing the code

2. Understand the input data

Microsoft Flow is all based on JSON. When you use characters with a special a meaning in JSONyou need to make sure that this is handled properly. So for example setting a text variable to a double quote (“) you will find that Flow adds an escape character.

Or if you set the variable to \”

this might not be immediately interesting to the citizen developer just wanting things to work, but in the past we have seen some Flow bugs with escaping characters. By looking at the code you can now find out which characters you might need to be careful with. Also Did you know that the @ sign is escaped with two @@ signs? Where the quotes and slashes are escaped with a slash?

3. Check which licence you are on

When you look at the code for the SharePoint When an item is created trigger you will see that the interval is set to 1 Minute when you are on a P2 plan.

{
"inputs": {
"host": {
"connection": {
"name": "@parameters('$connections')['shared_sharepointonline']['connectionId']"
}
},
"method": "get",
"path": "/datasets//tables//onnewitems",
"authentication": "@parameters('$authentication')"
},
"recurrence": {
"interval": 1,
"frequency": "Minute"
},
"splitOn": "@triggerBody()?['value']"
}

When you are running Free flow however, you will find the 15 minute triggers.

{
"inputs": {
"host": {
"connection": {
"name": "@parameters('$connections')['shared_sharepointonline']['connectionId']"
}
},
"method": "get",
"path": "/datasets//tables//onnewitems",
"authentication": "@parameters('$authentication')"
},
"recurrence": {
"interval": 15,
"frequency": "Minute"
},
"splitOn": "@triggerBody()?['value']"
}

4. Understand triggers

In the above example we saw that the When an item is created trigger actually runs on a schedule. Did you ever ask yourself why doesn’t my flow start immediately?

5. Because you are a code developer

Many developers starting with Flow simply like to have a look at the code behind the low code actions. In the example in my reason #1 for using the peek code option the API call used  can be helpful for developers. Even if you decided to not use flow and for whatever reason you would want to call the same end point from some code, understanding how flow does things could help.

/v2/datasets/@{encodeURIComponent(encodeURIComponent(‘org0688189e.crm11’))}/tables/@{encodeURIComponent(encodeURIComponent(‘accounts’))}/items

 

Have you found any other reasons to use the peek code? Please do leave a comment below.

Advertisements

Please leave a comment or feedback

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.