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

13 Responses to “Example of Custom Workflow Activity (CRM 4.0)”


  1. 1 sohaib ahmad January 28, 2009 at 10:28 am

    i’ve tried your code. In my opinion, it has an error:

    crmService.Update(reto);

    should be replaced with:

    crmService.Update(ret);

    I correct the reto to ret and workflow works fine, but no value gets updated. According to your code:

    ret.actualserviceunits = tes;

    actualserivceunits field should be updated. But there wasn’t any update in any value. I even tried using:

    ret.description = tes.value.tostring();

    But nothing gets updated. Do we need to commit something? pleas respond????

  2. 2 Aamir Bashir January 28, 2009 at 11:07 am

    Hi Sohaib,

    Thanks for correcting Typo mistake. Can you open the workflow and see any error.

  3. 3 sohaib ahmad January 28, 2009 at 11:18 am

    there isn’t any error in workflow. Its status is succeeded. But i can’t see any values in the related incident. i’ve even tried to update the code and have hard-coded the incidentid, even then i see the status to be succeeded and no value is updated. here i write my updated code:

    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());
    Guid guidID = new Guid(“148ba329-73ec-dd11-9342-0014225b89e4”);
    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;
    CrmNumber tes = new CrmNumber();
    tes.Value = 123;
    // i will use existing field on CRM incident to hold the integer value.
    //ret.actualserviceunits = tes;
    ret.productserialnumber = tes.Value.ToString();
    // updating the case entity
    crmService.Update(ret);
    return ActivityExecutionStatus.Closed;
    }

    line Guid guidID = new Guid(“148ba329-73ec-dd11-9342-0014225b89e4”); shows a valid incidentid. Please help…

  4. 4 Aamir Bashir January 28, 2009 at 11:30 am

    un-publish the custom worflow
    make a new project for custom workflow and pubolish the solution. I have seen issue with custom workflow when you try to publish them few times.

  5. 5 sohaib ahmad January 28, 2009 at 11:33 am

    i am doing my development in visual studio 2008, can this be an issue? can using visual studio 2005 fix this?

  6. 6 Aamir Bashir January 28, 2009 at 11:40 am

    i have only written plugin in VS 2008 and Custom workflow in VS 2005. You can try creating solution and pubolishing again with VS 2008 to see if it is issue with VS 2008.

  7. 7 T Jason Mayo August 26, 2011 at 8:27 pm

    an incident can’t be updated after it is closed.

  8. 8 New Project December 21, 2012 at 4:40 am

    I’m not that much of a internet reader to be honest but your sites really nice, keep it up!
    I’ll go ahead and bookmark your website to come back down the road. All the best

  9. 9 credit unions vs banks January 13, 2013 at 11:59 am

    Generally I don’t read post on blogs, however I would like to say that this write-up very forced me to try and do so! Your writing taste has been amazed me. Thanks, very great article.

  10. 10 Anyoption review February 25, 2013 at 10:47 pm

    These are genuinely enormous ideas in about blogging.
    You have touched some good points here. Any way keep
    up wrinting.

  11. 11 stock options explained April 14, 2013 at 6:51 pm

    It’s the best time to make a few plans for the future and it’s time to be happy.
    I’ve learn this publish and if I may just I desire to recommend you some attention-grabbing things or tips. Perhaps you could write next articles relating to this article. I desire to read even more issues approximately it!

  12. 12 Michell May 12, 2013 at 2:40 pm

    Very good article. I am going through many of these
    issues as well..

  13. 13 toner cartridge June 18, 2013 at 10:05 pm

    I always used to study post in news papers but now as I am a user of internet so from now I am using net for posts, thanks to web.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s





%d bloggers like this: