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.
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????
Hi Sohaib,
Thanks for correcting Typo mistake. Can you open the workflow and see any error.
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…
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.
i am doing my development in visual studio 2008, can this be an issue? can using visual studio 2005 fix this?
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.
an incident can’t be updated after it is closed.