Archive for March, 2008

Example of Custom Workflow Activity (CRM 4.0)

In this example, I will use two dates fields and then calculate the number of days . This can be used for different scenarios. In this example, I will be using scenario like:

When a case is registered, there are different life cycle –Test, In development, In Production.
When technician chooses “In Production” from drop down list, java script populates the “end date” and then the custom workflow assembly will calculate the number of days between both dates.

We have two custom fields on case entity

1 = Case start date
2= Case end date

We would like to calculate the number of days for a given case. I will be using the existing integer(actualserviceunits) field on case form to hold the value .

Result = Case end date –Case start date

I would like to use CustomWorkflowActivity and my input parameters will be :

1 = Guidid of Case (I would like to retrieve the given case and then update it)
2 = Start date
3 = End date

Code :

using System;
using System.IO;
using System.Net;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;

using Microsoft.Crm.Workflow;
using Microsoft.Crm.Workflow.Activities;
using Microsoft.Crm.Sdk.Query;

namespace Updatingincident
{
[CrmWorkflowActivity(“updating incident”, “cal”)]

public partial class calculate : SequenceActivity
{
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{

IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));
IWorkflowContext context = contextService.Context;

ICrmService crmService = context.CreateCrmService();

WhoAmIRequest systemUserRequest = new WhoAmIRequest();
WhoAmIResponse systemUser = (WhoAmIResponse)crmService.Execute(systemUserRequest);

// using the GUID of current case

ColumnSet cols = new ColumnSet();
Guid guidID = new Guid(this.objectID.Value.ToString());

incident ret = (incident)crmService.Retrieve(EntityName.incident.ToString(), guidID, new AllColumns());

DateTime a = DateTime.Parse(this.startdate.date);

DateTime b = DateTime.Parse(this.enddate.date);

TimeSpan tid = b – a;

CrmNumber tes = new CrmNumber();
tes.Value = tid.Days;
// i will use existing field on CRM incident to hold the integer value.

ret.actualserviceunits = tes;

// updating the case entity
crmService.Update(reto);

return ActivityExecutionStatus.Closed;
}

// getting the id of case

public static DependencyProperty objectIDProperty = DependencyProperty.Register(“objectID”, typeof(Lookup), typeof(calculate));

[CrmInput(” objectID”)]
[CrmReferenceTarget(“incident”)]
public Lookup objectID
{
get
{
return (Lookup)base.GetValue(objectIDProperty);
}
set
{
base.SetValue(objectIDProperty, value);
}

}

// start date as input parameter

public static DependencyProperty startdateProperty = DependencyProperty.Register(“startdate”, typeof(CrmDateTime), typeof(calculate));

[CrmInput(” startdate”)]

public CrmDateTime startdate
{
get
{
return (CrmDateTime)base.GetValue(startdateProperty);
}
set
{
base.SetValue(startdateProperty, value);
}
}

// End date as input parameter

public static DependencyProperty enddateProperty = DependencyProperty.Register(“enddate”, typeof(CrmDateTime), typeof(calculate));

[CrmInput(” enddate”)]

public CrmDateTime enddate
{
get
{
return (CrmDateTime)base.GetValue(enddateProperty);
}
set
{
base.SetValue(enddateProperty, value);
}
}

}
}

Register the assembly

Now we need to register the Custom assembly as pulgin . I would be using plugin developer to register this assembly.

Advertisements