Define different types of Input and Output arguments for custom workflow activity

As a MS Dynamics CRM Developer there are certain situations where you need to pass some of the entity field values as Input arguments to a Custom Workflow activity and some cases you may require to get some of the code execution results as Output arguments from a custom workflow activity.

In order to accomplish this you definitely need to have an idea on the Syntax of the Input and Output arguments for different types of fields.

Here i considered an entity called Patient which is having different types of fields and those are given below
1. Patient Name : String
2. Date Of Birth : Date and Time
3. Hospital : Lookup (to an entity called Hospital)
4. Patient Status : Option Set
5. Hospitalization Required : Two Option Set
6. Patient Age : Whole Number
7. Consultation Fee : Decimal Number
8. Estimated Amount : Floating Point Number
9. Treatment Cost : Currency
10. Patient Id : String

Types of arguments

  1. Input : Entity field value can be passed to Custom workflow activity
  2. Output : Execution result (if any) can be passed as output from Custom workflow activity
  3. Input/Output : It can be used as both Input and Output

The following Custom Workflow Activity code will explains how to declare Input and Output arguments.

using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using System.Activities;

namespace InputOutputParameters
    public class InputOutputParameters : CodeActivity
        //Data Type : String 
        public InArgument<string> PatientName { get; set; }

        //Data Type : Date and Time
        public InArgument DateOfBirth { get; set; }

        //Data Type : Lookup
        public InArgument Hospital { get; set; }

        //Data Type : Option Set
        [AttributeTarget("new_patient", "statuscode")]
        public InArgument PatientStatus { get; set; }

        //Data Type : Two Option Set
        public InArgument<bool> HospitalizationRequired { get; set; }

        //Data Type : Whole Number
        public InArgument<int> PatientAge { get; set; }

        //Data Type : Decimal Number
        public InArgument<decimal> ConsultationFee { get; set; }

        //Data Type : Floating Point Number
        public InArgument<decimal> EstimatedAmount { get; set; }

        //Data Type : Currency
        public InArgument TreatmentCost { get; set; }

        //Data Type : String
        public OutArgument<string> ShowPatientDetails { get; set; }

        //Data Type : String (Can be used as Input/Output)
        public InOutArgument<string> PatientInOut { get; set; }

        protected override void Execute(CodeActivityContext context)
                IWorkflowContext workflowContext = context.GetExtension();
                IOrganizationServiceFactory serviceFactory = context.GetExtension();
                IOrganizationService service = serviceFactory.CreateOrganizationService(workflowContext.UserId);
                ITracingService tracingService = context.GetExtension();

                tracingService.Trace("Patient Details using input and output parameters Workflow Started.");
                var patientName = PatientName.Get(context);
                var dateOfBirth = DateOfBirth.Get(context);
                var hospital = Hospital.Get(context)?.Name;
                var patientStatus = PatientStatus.Get(context).Value;
                var hospitalizationRequired = HospitalizationRequired.Get(context);
                var patientAge = PatientAge.Get(context);
                var consultationFee = ConsultationFee.Get(context);
                var estimatedAmount = EstimatedAmount.Get(context);
                var treatmentCost = TreatmentCost.Get(context).Value;
                var patientId = PatientInOut.Get(context);
                tracingService.Trace($"Patient Name : {patientName}, Date Of Birth : {dateOfBirth}, Hospital : {hospital}, Patient Status : {patientStatus}, Hospitalization Required: {hospitalizationRequired}, Patient Age: {patientAge}, Consultation Fee : {consultationFee}, Estimated Amount : {estimatedAmount}, Treatment Cost : {treatmentCost}, Patient ID : {patientId}");
                var patientDetails = $"Patient Name : {patientName}, Date Of Birth : {dateOfBirth}, Hospital : {hospital}, Patient Status : {patientStatus}, Hospitalization Required: {hospitalizationRequired}, Patient Age: {patientAge}, Consultation Fee : {consultationFee}, Estimated Amount : {estimatedAmount}, Treatment Cost : {treatmentCost}, Patient ID : {patientId}";
                PatientInOut.Set(context, PatientInOut.ToString());
                ShowPatientDetails.Set(context, patientDetails);
                tracingService.Trace("Patient Details using input and output parameters Workflow Ended.");
            catch(Exception ex)
                throw new InvalidPluginExecutionException(ex.Message);


The below screen shot will show you the representation of the Input arguments once after you successfully build and deploy the above custom workflow code activity.


Download Dynamics CRM Tools for v9.x

Steps to be followed:

  1. Go to Windows, Search for “Windows Power Shell” and Open it. (Shown below)

Windows PowerShell

2. Navigate to the folder where you want to download the tools, for example if you want to download in a D365Tools folder in  C drive, Type  cd D365Tools\ (Shown Below)

Windows PowerShell

3. Copy  and paste  the following Power Shell script in to the Power Shell window and Press Enter. (Shown Below)

$sourceNugetExe = ""
$targetNugetExe = ".\nuget.exe"
Remove-Item .\Tools -Force -Recurse -ErrorAction Ignore
Invoke-WebRequest $sourceNugetExe -OutFile $targetNugetExe
Set-Alias nuget $targetNugetExe -Scope Global -Verbose

##Download Plugin Registration Tool
./nuget install Microsoft.CrmSdk.XrmTooling.PluginRegistrationTool -O .\Tools
md .\Tools\PluginRegistration
$prtFolder = Get-ChildItem ./Tools | Where-Object {$_.Name -match 'Microsoft.CrmSdk.XrmTooling.PluginRegistrationTool.'}
move .\Tools\$prtFolder\tools\*.* .\Tools\PluginRegistration
Remove-Item .\Tools\$prtFolder -Force -Recurse

##Download CoreTools
./nuget install Microsoft.CrmSdk.CoreTools -O .\Tools
md .\Tools\CoreTools
$coreToolsFolder = Get-ChildItem ./Tools | Where-Object {$_.Name -match 'Microsoft.CrmSdk.CoreTools.'}
move .\Tools\$coreToolsFolder\content\bin\coretools\*.* .\Tools\CoreTools
Remove-Item .\Tools\$coreToolsFolder -Force -Recurse

##Download Configuration Migration
./nuget install Microsoft.CrmSdk.XrmTooling.ConfigurationMigration.Wpf -O .\Tools
md .\Tools\ConfigurationMigration
$configMigFolder = Get-ChildItem ./Tools | Where-Object {$_.Name -match 'Microsoft.CrmSdk.XrmTooling.ConfigurationMigration.Wpf.'}
move .\Tools\$configMigFolder\tools\*.* .\Tools\ConfigurationMigration
Remove-Item .\Tools\$configMigFolder -Force -Recurse

##Download Package Deployer 
./nuget install Microsoft.CrmSdk.XrmTooling.PackageDeployment.WPF -O .\Tools
md .\Tools\PackageDeployment
$pdFolder = Get-ChildItem ./Tools | Where-Object {$_.Name -match 'Microsoft.CrmSdk.XrmTooling.PackageDeployment.Wpf.'}
move .\Tools\$pdFolder\tools\*.* .\Tools\PackageDeployment
Remove-Item .\Tools\$pdFolder -Force -Recurse

##Download Package Deployer PowerShell module
./nuget install Microsoft.CrmSdk.XrmTooling.PackageDeployment.PowerShell -O .\Tools
$pdPoshFolder = Get-ChildItem ./Tools | Where-Object {$_.Name -match 'Microsoft.CrmSdk.XrmTooling.PackageDeployment.PowerShell.'}
move .\Tools\$pdPoshFolder\tools\*.* .\Tools\PackageDeployment.PowerShell
Remove-Item .\Tools\$pdPoshFolder -Force -Recurse

##Remove NuGet.exe
Remove-Item nuget.exe

Windows PowerShell

4. You will find the Tools in the folder C:\D365Tools\Tools (The folder you mentioned before)

5. To get the latest version of the tools, Repeat the same steps.

The most used code snippets in Dynamics365