Showing posts with label Dynamics 365 Input Output argument for Workflow Activities. Show all posts
Showing posts with label Dynamics 365 Input Output argument for Workflow Activities. Show all posts

Tuesday, May 18, 2021

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

How to use different types of Input and Output arguments for custom workflow activity 👈

There are certain situations where we require different types of Input arguments to pass to Custom Workflow activity and in some cases, we might require to get some of the code execution results as Output arguments from a custom workflow activity.

In order to accomplish this, we need Syntax to implement 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

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

Below is the Custom Workflow Activity code with syntax to declare Input and Output arguments.👇

using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Workflow;
using System.Activities;
 
namespace WorkflowEmailAttachment
{
    public class InputOutputParameters : CodeActivity
    {
        //Data Type : String
        [Input("patientname")]
        [RequiredArgument]
        public InArgument<string> PatientName { get; set; }
 
        //Data Type : Date and Time
        [Input("dateofbirth")]
        [RequiredArgument]
        public InArgument<DateTime> DateOfBirth { get; set; }
 
        //Data Type : Lookup
        [Input("hospital")]
        [ReferenceTarget("new_hospital")]
        [RequiredArgument]
        public InArgument<EntityReference> Hospital { get; set; }
 
        //Data Type : Option Set
        [Input("patientstatus")]
        [RequiredArgument]
        [AttributeTarget("new_patient", "statuscode")]
        public InArgument<OptionSetValue> PatientStatus { get; set; }
 
        //Data Type : Two Option Set
        [Input("hospitalizationrequired")]
        [RequiredArgument]
        public InArgument<bool> HospitalizationRequired { get; set; }
 
        //Data Type : Whole Number
        [Input("patientage")]
        [RequiredArgument]
        public InArgument<int> PatientAge { get; set; }
 
        //Data Type : Decimal Number
        [Input("consultationfee")]
        [RequiredArgument]
        public InArgument<decimal> ConsultationFee { get; set; }
 
        //Data Type : Floating Point Number
        [Input("estimatedamount")]
        [RequiredArgument]
        public InArgument<decimal> EstimatedAmount { get; set; }
 
        //Data Type : Currency
        [Input("treatmentcost")]
        [RequiredArgument]
        public InArgument<Money> TreatmentCost { get; set; }
 
        //Data Type : String
        [Output("showpatientdetails")]
        public OutArgument<string> ShowPatientDetails { get; set; }
 
 
        //Data Type : String (Can be used as Input/Output)
        [Input("patientinput")]
        [Output("patientoutput")]
        [RequiredArgument]
        public InOutArgument<string> PatientInOut { get; set; }
 
 
        protected override void Execute(CodeActivityContext executionContext)
        {
            try
            {
                // Get workflow context
                var tracingService = executionContext.GetExtension<ITracingService>();
                var context = executionContext.GetExtension<IWorkflowContext>();
                var serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
                var service = serviceFactory.CreateOrganizationService(context.UserId);
 
                tracingService.Trace("Patient Details using input and output parameters Workflow Started.");
                var patientName = PatientName.Get<string>(executionContext);
                var dateOfBirth = DateOfBirth.Get<DateTime>(executionContext);
                var hospital = Hospital.Get<EntityReference>(executionContext)?.Name;
                var patientStatus = PatientStatus.Get<OptionSetValue>(executionContext).Value;
                var hospitalizationRequired = HospitalizationRequired.Get<Boolean>(executionContext);
                var patientAge = PatientAge.Get<Decimal>(executionContext);
                var consultationFee = ConsultationFee.Get<Decimal>(executionContext);
                var estimatedAmount = EstimatedAmount.Get<float>(executionContext);
                var treatmentCost = TreatmentCost.Get<Money>(executionContext).Value;
                var patientId = PatientInOut.Get<string>(executionContext);
                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(executionContext, PatientInOut.ToString());
                ShowPatientDetails.Set(executionContext, patientDetails);
                tracingService.Trace("Patient Details using input and output parameters Workflow Ended.");
            }
            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(ex.Message);
            }
        }
    }
}

 ------------------------------------------------------------------------------------------------------

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



Click here to learn more about workflow activity.

Thank you!!
Happy CRMing..

Keep Learning....!!!!