Introduction
In the previous blog posts, I introduced the practice of Intentional Logging and the foundation you need to establish in your environment.
Now, one of the interesting parts starts which is creating the log. Indeed if you use flows in your environment, after you have this child flow, you can make troubleshooting a lot easier and interesting. So, let’s start.
Child Flow for creating the log
What you need to do is to create a child flow that is responsible for creating the log, and don’t bother yourself to create the log activity in your main flows.
When Power Apps calls a flow (V2)
The first step is to choose the “When Power Apps calls a flow (V2)” trigger.
Then, you need to add the parameters as the following picture shows.
As you see, I used the parameter description part to explain how the caller of this child flow should prepare the parameters and what value is needed to be filled.
Here is the explanation of the parameters:
- Subject and Description are text fields, and will be used directly in the activity creation.
- Workflow is the result of workflow() function and will be used to create the URL of the flow run.
- Regarding Logical Name will be the Logical name of the Table to which we want our log to be related.
- Regarding Id will be the Id of the record to which we want our log to be related.
- Regarding Logical Name – Plural would be the plural of the logical name of the Table to which we want our log to be related. We need this in order to compose the relationship name between the Table and the Log activity.
- Log Type is a number. It will be the number value of the choice. Your number might be different, and you need to check it in the Log table you have created.
- Priority is a number, and will be used to set priority choice column of the Log.
Parse Workflow
The first action is Parse Json, and we rename it to Parse Workflow. David Rivard explained in detail in his blog post Power Automate – Infer the Flow Run URL (xrmvision.com), how you can infer the flow run URL. Here, I have used the same idea. Please note, as he mentioned, you don’t need to create the schema manually. Instead, you can click on the “Use sample payload to generate schema” and then Paste a result of the workflow() function. You can do it, by creating another flow and compose the workflow() function, and run the flow once.
Compose URL
The next step is Compose. I put the expression here as well. If you used the same name as me for the previous Parse JSON action, you can copy and paste the following. Please note, that you need to paste it in the expression editor, not in the field itself, as the following picture shows.
concat(‘https://flow.microsoft.com/manage/environments/’, body(‘Parse_Workflow‘)?[‘tags’]?[‘environmentName’], ‘/flows/’, body(‘Parse_Workflow‘)?[‘name’], ‘/runs/’, body(‘Parse_Workflow‘)?[‘run’]?[‘name’])
Initialize varLogTypeText
The next action is Initialize Variable, configured as follows. Because of the limitation of the card form, we needed to have Log Type as text and its choice field. This variable will be used to set the same Log Type text as the Log Type choice.
Switch on Log Type Value
The next action is Switch. Indeed we switch on the Log Type number value, and based on the value set the proper text to the variable we created in the previous step.
As an example, for the Information, I used the value 763000000.
And then, set the varLogTypeText to Information. You need to repeat it for all Log Types. Please do not forget to use the numbers according to your system.
Add Log row
Now, we are ready to create the log. You should use “Add a new row” action from Dataverse and set the parameters as follows.
For Subject and Description, we use the input parameters of our trigger. For the Log Type choice field and Priority, we do the same except we should choose a custom value to be able to set the number.
For the Log Type text field, we use the variable varLogTypeText.
And for the URL, we set the outputs of our Compose URL action.
Compose relationship name
Here, we use the concat() function to concat the Regarding Logical Name we received as parameter (1 in the picture), to the plural name of our log table plus an underscore (2 in the picture). Probably your plural name of the log activity is different. In the next picture, I explain how to get the plural name.
In order to get the plural name, you need to open your log table and as the following picture depicts, click on Tools and Copy set name.
When I do this, “bzt_logs” is the result and I only added an underscore to compose the whole relationship name.
Relate rows – Set Regarding
Now, we are ready to do the relation between the log record and the parent table record. Here you add a Relate rows action from Dataverse connector. For the Table Name, you use the Regarding Logical Name – Plural trigger parameter. Row ID is Regarding Id trigger parameter. Relationship is the Outputs of Compose relationship action. And for the Relate With you should choose the OdataId of the created log record in previous steps.
Respond
The last step is a Respond action and you can keep the parameters empty.
Next Steps
Congratulations! You are done!
So far, you have a Dataverse environment together with this child flow and you are ready to create your logs from your main flows in your environment. In the next blog, I will show you how to use this whole setup.