Sei sulla pagina 1di 28

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).

Exercise 1: Create a custom activity

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.

Task 1: Create a custom activity

Start Visual Studio 2019.

Select File > New > Project.

Select Class Library (.NET Framework) and then select Next.

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.

Right-click the project and select Manage NuGet Packages.


Select the Browse tab, search for and select Microsoft.CRMSDK.CoreAssemblies, and then
select Install.

Select I Accept if you agree to the terms.

Search for and select Microsoft.CRMSDK.Workflow and then select Install.

Select I Accept if you agree to the terms.

Right-click the project, select Add, and then select Class.


Enter ConvertFormatDatetime in the Name field and then select Add.

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")]

public InArgument<DateTime> DateToEvaluate { get; set; }

[Output("Formatted DateTime output as string")]

public OutArgument<String> ForammtedDateTimeOutput { get; set; }


Replace NotImplementedException with the following snippet.

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

DateTime utcDateTime = this.DateToEvaluate.Get(context);

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

var settings = service.Retrieve("usersettings",

workflowContext.UserId, new ColumnSet("timezonecode") );

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 timeZoneChangeRequest = new

LocalTimeFromUtcTimeRequest()

UtcTime = utcDateTime,
TimeZoneCode = int.Parse(settings["timezonecode"].ToString())

};

Run the time zone change request.

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));

Your class should now look like the following image.

Right-click the project and select Properties.


Select the Signing tab, select the Sign the Assembly check box, and select New in the Choose a
strong name key file drop-down menu.

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.

Exercise 2: Register and consume

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.

Task 1: Register the workflow activity

Go to your Dynamics 365 SDK folder and open the PluginRegistration folder.
Double-click PluginRegistration.exe.

Select Create New Connection.

Select Microsoft 365, provide your credentials, and then select Login.

Select Register and then select Register New Assembly.

Select Browse.

Browse to the debug folder of your WorkflowActivities project, select WorkflowActivities.dll,


and then select Open.
Select Register Selected Plugins.

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.

Select Solutions and then select New Solution.

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.

Open the solution that you created.

Select Add Existing and then select Entity.


You will use the Task entity to text the workflow activity. Select Task and then select Next.

Select the Select components option.

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.

Select the Fields tab and then select + Add field.

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 Publish and wait for the publishing to complete.

Close the form editor browser tab or window.

Select Done.

Task 3: Create a workflow

Select Solutions and open the solution that you created.

Select the ellipsis (...) button and select Switch to classic.


Select Process and then select New.

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 below the condition statement.


Select Add Step and then select the Custom Workflow Activity that you created.

Provide a description and then select Set Properties.

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 Save and Close.


Select the step that you just added, select Add Step, and then select Stop Workflow.

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.

Task 4: Test the workflow and custom activity

Go to https://make.powerapps.com make sure that you are not in the default environment.

Select Apps and select to open the CRM Hub application.

Select Activities and then select Task.

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.

Potrebbero piacerti anche