HTML Dropdown

Wednesday 8 June 2016

Create Custom Workflow Activities – CRM 2013


The Problem:
Create a Custom Workflow Activity, that creates a (follow up-)Task after an Account was created. (This is just an example. You can use default workflow functionality to reach this target.)

1. Create Solution with Project
The first step is to create a Visual Studio 2012 Project.

  • Select Visual C# -> Workflow -> Activity Library
  • Select .NET Framework 4 .5
  • Select Name for Solution and Project
  • Select Location

 
2. Check Project Settings
check that the Framework-Type is NOT .NET Framework Client Profile. The correct one is .NET Framework 4.5

3. Create the Class
Delete the automatically created class Activity1.xaml and create a class called CreateTask.cs.

4. Adding References and Using
Add references to the following assemblies (Right click References in the project and add...).

  • microsoft.xrm.sdk.dll
  • microsoft.xrm.sdk.workflow.dll

Add the reference for System.Runtime.Serialization, which is part of the .NET Framework.

 Add the following using-Statements to your class.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Activities;

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Workflow;
 
5. Inheritance

Make our class inherit from CodeActivity (System.Activities) and implement the method Execute. You also have to make the class public.

 
6. Input Parameters

We would like to put a parameter in our Workflow. So every customer can define the subject of the task by it's own.

        #region Input Properties

        [Input("Task Subject")]

        [Default("Empty Subject”)]

        public InArgument<string>TaskSubject{ get; set; }

        #endregion

 7. Create Context and Service

The context we need for retrieving the ID of the current record, the service is required for communication with CRM-Webservice (IOrganizationService).

The following code has to be added into the Execute-Method.
//Create the IWorkflowContext and the IOrganizationService for communication with CRM

IWorkflowContext workflowContext =

    context.GetExtension<IWorkflowContext>();

IOrganizationServiceFactory serviceFactory =

    context.GetExtension<IOrganizationServiceFactory>();

IOrganizationService service =

    serviceFactory.CreateOrganizationService(workflowContext.UserId);

8. Business Logic

Now we have to add the logic which does the following.

The following code has to be added into the Execute-Method.

 
  • Read Text from "TaskSubject" Input Parameter

·         string newSubject = TaskSubject.Get<string>(context);

  •     Create a new task object

·         Entity newTask = new Entity("task");

  •     Set subject of the task to TaskSubject value

·         newTask["subject"] = newSubject;

  •     Set regardingobjectid of task to PrimaryEntityId of context (this is the id of the account).

·         newTask["regardingobjectid"] =

·         new EntityReference("account", workflowContext.PrimaryEntityId)

·         Create the new task in CRM

              Guid taskId = service.Create(newTask);

9. Sign the assembly

 First of all, go to project settings and sign your assembly.

 Right Click the Project -> Properties -> Signing -> Sign the assembly -> New -> Enter Key file name "testkey" -> no password (for testing purposes).

.snk file will be generated to your solution.
 
10. Compile and deploy the assembly

 Rebuild your solution and deploy the assembly to the CRM System. This can be done by the PluginRegistrationTool 2011 of Microsoft. You can find this as part of the current CRM-SDK. You have to compile it by your own. Choose the following settings.

 Deployment Type =On-premise

Server=******************

Port=******************

UseSSL=Yes

 


 Authentication Source=Active Directory

Username=*************

Password=************


Click on register New Assembly



Choose dll file of VS solution from location of your project. Load assembly and specify the things as below
And click on Register Selected Plugin Button.

 
11. Create a Workflow with the new step
 
Create a default Workflow in CRM. Choose the following settings.

Solution -> Processes->  Create New Process

 


Push the Set Properties-Button of the step and select a default name for the Input Parameter (TaskSubject). Because the Owner of the new task should see which account was created, we add the name of the account as input parameter.

 Save and activate the process.

12. Testing

 Create a new Account in CRM.

 Setting -> Service-> Accounts -> Create new account

 

 Processing the workflow can take some seconds. Open the Activity List of the account and select Filter on "All". You should see our created task. The subject contains some static text and the name of the created account.

 Go To advance find

Click on Results

 
13. Code Overview

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Activities;

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Workflow;

 

namespace customworkflowstep

    {

public sealed partial class CreateTask : CodeActivity

{

           

          protected override void Execute(CodeActivityContext context)

         {

 

        // throw new NotImplementedException();

 

       // Create the IWorkflowContext and IorganizationService for Communicationwith CRM

 

IWorkflowContext workflowContext =  context.GetExtension<IWorkflowContext>();

IOrganizationServiceFactory serviceFactory = context.GetExtension<IOrganizationServiceFactory>();

IOrganizationService service = serviceFactory.CreateOrganizationService(workflowContext.UserId);

 

 

                    //Retrive data from Inputparameter

                    string newSubject = TaskSubject.Get<string>(context);

 

 

                    //Create the new task object (in memory)

                      Entity newTask = new Entity("task");

                      newTask["subject"] = newSubject;

                      newTask["regardingobjectid"] =

         new EntityReference("account",workflowContext.PrimaryEntityId);

 

 

                    //Create task in CRM

                      Guid taskId = service.Create(newTask);

 

                }

                #region Input Properties

                [Input("Task Subject")]

                [Default("Empty Subject")]

                public InArgument<string> TaskSubject { get; set; }

                #endregion

 

     }

  }

 

1 comment:

  1. I have read many article here and learn many things from that, this was really helpful for me. Thank you so much for sharing this info with us and share your ideas with us.
    CRM Software in Dubai
    CRM Software
    CRM Software in UAE
    CRM Software for Small Business

    ReplyDelete