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.)
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.
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
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.
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...).
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.
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;
Make our
class inherit from CodeActivity (System.Activities) and implement
the method Execute. You also have to make the class public.
We would
like to put a parameter in our Workflow. So every customer can define the
subject of the task by it's own.
[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
.snk file will be generated to your solution.
Server=******************
Port=******************
UseSSL=Yes
Username=*************
Password=************
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
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
}
}
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.
ReplyDeleteCRM Software in Dubai
CRM Software
CRM Software in UAE
CRM Software for Small Business