Exercise - Write A Custom Workflow Extension
Exercise - Write A Custom Workflow Extension
Frequently, performing date formatting on Common Data Service data is required. While
Power Automate exposes some features around this process, it might occasionally be
appropriate to produce custom workflow extensions to achieve this objective (for example, if
the logic needs to run synchronously within your environment).
In this exercise, you will create a custom activity that will convert datetime to user local time,
format it as "Monday, December 25, 2019," and publish the formatted value as a string to the
consumer of the custom activity.
Each exercise consists of a scenario and learning objectives. The scenario describes the
purpose of the exercises, while the objectives are listed and have bullet points.
Enter WorkflowActivities in the Project name field, select .NET Framework 4.6.2 in the
Framework drop-down menu, and then select Create.
Right-click Class1.cs and then select Delete.
Add the using statements to the class as follows and then make it public.
Copy
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Workflow;
using System.Activities;
Inherit from CodeActivity and implement the abstract member. This action will override the
Execute method of the inherited class.
Add the following snippet to the class. This will add a required input argument of type
DateTime and an output argument of type String.
Copy
[RequiredArgument]
[Input("DateTime input")]
Copy
IWorkflowContext workflowContext =
context.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory =
context.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service =
serviceFactory.CreateOrganizationService(workflowContext.UserId);
Get the input datetime. Add the following snippet to the Execute method.
Copy
Check if the input datetime is UTC, if not, convert it to UTC. Add the following snippet to the
Execute method.
Copy
if (utcDateTime.Kind != DateTimeKind.Utc)
{
utcDateTime = utcDateTime.ToUniversalTime();
Your Execute method should now look like the following image.
Task 2: Get user settings and convert datetime to user local datetime
Get the user TimeZoneCode of the user settings entity. Add the following snippet to the
Execute method.
Copy
Build a time zone change request by providing the Utc time that you got from the input and
the TimeZoneCode from the user settings.
Copy
LocalTimeFromUtcTimeRequest()
UtcTime = utcDateTime,
TimeZoneCode = int.Parse(settings["timezonecode"].ToString())
};
Copy
LocalTimeFromUtcTimeResponse timeZoneResponse =
service.Execute(timeZoneChangeRequest) as LocalTimeFromUtcTimeResponse;
Format the LocalTime from the time zone response and set it to the output of the activity.
Copy
this.ForammtedDateTimeOutput.Set(context, String.Format("{0:f}",
timeZoneResponse.LocalTime));
Enter contoso.snk in the Key file name field, clear the Protect my key file with a password
check box, and then select OK.
Build the project and make sure that the build succeeds.
In this exercise, you will register the workflow activity, create and prepare a solution, create a
workflow, and then test the workflow and custom activity.
Go to your Dynamics 365 SDK folder and open the PluginRegistration folder.
Double-click PluginRegistration.exe.
Select Microsoft 365, provide your credentials, and then select Login.
Select Browse.
Select OK.
Task 2: Create and prepare a solution
Go to https://make.powerapps.com and make sure that you do not have the default
environment selected.
Enter Contoso WFA in the Display Name field, enter ContosoWFA in the Name field, and then
select the Publisher drop-down menu.
Select + Publisher.
Enter Contoso in the Display Name field, enter contoso in the Prefix field, and then select Save
and Close.
Select the Publisher drop-down menu again and select the publisher that you created.
Enter 1.0.0.0 in the Version field and then select Create.
Select the Forms tab, select the Task form, and then select Add.
Select Add again.
Select to open the Task entity that you just added to your solution.
Enter Formatted Datetime in the Display Name field, select Text from the Data Type drop-
down menu, and then select Done.
Select the Save Entity button.
Select the Forms tab and then select to open the Task form.
Drag the Formatted Datetime field to the form and then place it below the Subject field.
Select Save.
Select Done.
Enter Convert and Format Datetime in the Process Name field, select Workflow from the
Category drop-down menu, select Task from the Entity drop-down list, clear the Run this
workflow in the background (recommended) check box, and then select OK.
Select the As an on-demand process check box and select Organization from the Scope drop-
down menu.
Select Add Step and then select Check Condition.
Enter Check if Due Date has value as the description and then select Configure.
Set the condition as shown in the following image and then select Save and Close.
Select the Value field, select Due Date, and then select Add.
Select OK.
Select Save and Close.
Select the step that you just added, select Add Step, and then select Update Record.
Provide a description, make sure that Task is selected for the entity in the Update drop-down
list, and then select Set Properties.
Select the Formatted Datetime field, select Format Datetime from the Look for drop-down
menu, and then select Add.
Select OK.
Select the condition step, select Add Step, and then select Default Action.
Select below the Otherwise, select Add Step, and then select Stop Workflow.
Your workflow should now look like the following image. Select Save.
Select Activate.
Confirm activation.
Select Close.
Select Publish All Customizations and wait for the publishing to complete.
Close the Solution Explorer window.
Go to https://make.powerapps.com make sure that you are not in the default environment.
Enter Test Task for the Subject, select the Due date and time, and then select Save.
The custom workflow activity should run and populate the Formatted Datetime field. The Due
date and Formatted Datetime values should be the same.
Select Settings and then select Personalization Settings.
Change the Time Zone to a different one and then select OK.
The Task should reload. The Due date field value should change to the selected time zone
automatically, but the Formatted Datetime value should not change.
Select Flow and then select the workflow that you created.
Select OK.
Select Refresh.
The Due date and Formatted Datetime values should now be the same.