Manipulate date fields by adding or subtacting days to a DateTime fields in Microsoft Dynamics CRM using workflow activity
Source: ManipulationLibrary
In this blog I am going to explain how to manipulate a date field in Dynamics CRM 2013 using plugin workflow activity.
This workflow allows days to be added/ subtracted to date field in CRM.
1. For creating this Workflow activity using plugin Open Visual studio and just proceed similarly as you do for creating any plugin i.e add necessary references, etc.
2. Now add a class file for and name it accordingly and add below code for Adding days to date::
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 AddSubtractDate
{
public sealed class AddDays : CodeActivity
{
protected override void Execute(CodeActivityContext executionContext)
{
var result = new DateTime();
var start = StartDate.Get<DateTime>(executionContext);
if (start != DateTime.MinValue)
result = start;
if (start != DateTime.MinValue) // && MinutesToAdd.Get<int>(executionContext) != 0
{
result = start.AddYears(YearsToAdd.Get<int>(executionContext))
.AddMonths(MonthsToAdd.Get<int>(executionContext))
.AddDays((7 * WeeksToAdd.Get<int>(executionContext)) +
DaysToAdd.Get<int>(executionContext))
.AddHours(HoursToAdd.Get<int>(executionContext))
.AddMinutes(MinutesToAdd.Get<int>(executionContext));
}
Result.Set(executionContext, result);
}
[Output("Result")]
[Default("1900-01-01T00:00:00Z")]
public OutArgument<DateTime> Result { get; set; }
[Input("Start Date")]
[Default("1900-01-01T00:00:00Z")]
public InArgument<DateTime> StartDate { get; set; }
[Input("Years To Add")]
[Default("0")]
public InArgument<int> YearsToAdd { get; set; }
[Input("Months To Add")]
[Default("0")]
public InArgument<int> MonthsToAdd { get; set; }
[Input("Weeks To Add")]
[Default("0")]
public InArgument<int> WeeksToAdd { get; set; }
[Input("Days To Add")]
[Default("0")]
public InArgument<int> DaysToAdd { get; set; }
[Input("Hours To Add")]
[Default("0")]
public InArgument<int> HoursToAdd { get; set; }
[Input("Minutes To Add")]
[Default("0")]
public InArgument<int> MinutesToAdd { get; set; }
}
}
3. Now, for subtracting the days from date do all the steps similarly and code is also almost same, just have a look on below code for subtracting days from a date field::
using System;
using Microsoft.Xrm.Sdk.Workflow;
using System.Activities;
namespace AddSubtractDate
{
public sealed class SubstractDays : CodeActivity
{
protected override void Execute(CodeActivityContext executionContext)
{
var result = new DateTime();
var start = StartDate.Get<DateTime>(executionContext);
if (start != DateTime.MinValue)
result = start;
if (start != DateTime.MinValue && DaysToSubtract.Get<int>(executionContext) != 0)
{
result = start.AddYears(-1*YearsToSubtract.Get<int>(executionContext))
.AddMonths(-1*MonthsToSubtract.Get<int>(executionContext))
.AddDays((-7*WeeksToSubtract.Get<int>(executionContext))
+ (-1*DaysToSubtract.Get<int>(executionContext)))
.AddHours(-1*HoursToSubtract.Get<int>(executionContext))
.AddMinutes(-1*MinutesToSubtract.Get<int>(executionContext));
}
Result.Set(executionContext, result);
}
[Output("Result")]
[Default("1900-01-01T00:00:00Z")]
public OutArgument<DateTime> Result { get; set; }
[Input("Start Date")]
[Default("1900-01-01T00:00:00Z")]
public InArgument<DateTime> StartDate { get; set; }
[Input("Years To Subtract")]
[Default("0")]
public InArgument<int> YearsToSubtract { get; set; }
[Input("Months To Subtract")]
[Default("0")]
public InArgument<int> MonthsToSubtract { get; set; }
[Input("Weeks To Subtract")]
[Default("0")]
public InArgument<int> WeeksToSubtract { get; set; }
[Input("Days To Subtract")]
[Default("0")]
public InArgument<int> DaysToSubtract { get; set; }
[Input("Hours To Subtract")]
[Default("0")]
public InArgument<int> HoursToSubtract { get; set; }
[Input("Minutes To Subtract")]
[Default("0")]
public InArgument<int> MinutesToSubtract { get; set; }
}
}
4. After creating your class file, just build it and open Plugin Registration and add the assembly to your CRM.
5. Now, goto your CRM solution and add create a new process(Add/ Subtract). In Add Step choose the assembly and the plugin you want to add to this process, as shown in image below
6. Goto Add Step and in Set properties, set what value you want to add. Save and Close
7. Now if you want to perform this workflow on Updation of record click on Update record or whatever you want, just select the step and in the Set Properties goto your Date field which you want to manipluate and click on that field and on the RHS goto Find For and you will find the custom workflow as shown in image below,select it and click on Result in the dropdown below to that of Look For dropdown. CLick Ok. Save and activate workflow.
8. Goto your entity and chck. Your workflow is working or not.
In this blog I am going to explain how to manipulate a date field in Dynamics CRM 2013 using plugin workflow activity.
This workflow allows days to be added/ subtracted to date field in CRM.
1. For creating this Workflow activity using plugin Open Visual studio and just proceed similarly as you do for creating any plugin i.e add necessary references, etc.
2. Now add a class file for and name it accordingly and add below code for Adding days to date::
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 AddSubtractDate
{
public sealed class AddDays : CodeActivity
{
protected override void Execute(CodeActivityContext executionContext)
{
var result = new DateTime();
var start = StartDate.Get<DateTime>(executionContext);
if (start != DateTime.MinValue)
result = start;
if (start != DateTime.MinValue) // && MinutesToAdd.Get<int>(executionContext) != 0
{
result = start.AddYears(YearsToAdd.Get<int>(executionContext))
.AddMonths(MonthsToAdd.Get<int>(executionContext))
.AddDays((7 * WeeksToAdd.Get<int>(executionContext)) +
DaysToAdd.Get<int>(executionContext))
.AddHours(HoursToAdd.Get<int>(executionContext))
.AddMinutes(MinutesToAdd.Get<int>(executionContext));
}
Result.Set(executionContext, result);
}
[Output("Result")]
[Default("1900-01-01T00:00:00Z")]
public OutArgument<DateTime> Result { get; set; }
[Input("Start Date")]
[Default("1900-01-01T00:00:00Z")]
public InArgument<DateTime> StartDate { get; set; }
[Input("Years To Add")]
[Default("0")]
public InArgument<int> YearsToAdd { get; set; }
[Input("Months To Add")]
[Default("0")]
public InArgument<int> MonthsToAdd { get; set; }
[Input("Weeks To Add")]
[Default("0")]
public InArgument<int> WeeksToAdd { get; set; }
[Input("Days To Add")]
[Default("0")]
public InArgument<int> DaysToAdd { get; set; }
[Input("Hours To Add")]
[Default("0")]
public InArgument<int> HoursToAdd { get; set; }
[Input("Minutes To Add")]
[Default("0")]
public InArgument<int> MinutesToAdd { get; set; }
}
}
3. Now, for subtracting the days from date do all the steps similarly and code is also almost same, just have a look on below code for subtracting days from a date field::
using System;
using Microsoft.Xrm.Sdk.Workflow;
using System.Activities;
namespace AddSubtractDate
{
public sealed class SubstractDays : CodeActivity
{
protected override void Execute(CodeActivityContext executionContext)
{
var result = new DateTime();
var start = StartDate.Get<DateTime>(executionContext);
if (start != DateTime.MinValue)
result = start;
if (start != DateTime.MinValue && DaysToSubtract.Get<int>(executionContext) != 0)
{
result = start.AddYears(-1*YearsToSubtract.Get<int>(executionContext))
.AddMonths(-1*MonthsToSubtract.Get<int>(executionContext))
.AddDays((-7*WeeksToSubtract.Get<int>(executionContext))
+ (-1*DaysToSubtract.Get<int>(executionContext)))
.AddHours(-1*HoursToSubtract.Get<int>(executionContext))
.AddMinutes(-1*MinutesToSubtract.Get<int>(executionContext));
}
Result.Set(executionContext, result);
}
[Output("Result")]
[Default("1900-01-01T00:00:00Z")]
public OutArgument<DateTime> Result { get; set; }
[Input("Start Date")]
[Default("1900-01-01T00:00:00Z")]
public InArgument<DateTime> StartDate { get; set; }
[Input("Years To Subtract")]
[Default("0")]
public InArgument<int> YearsToSubtract { get; set; }
[Input("Months To Subtract")]
[Default("0")]
public InArgument<int> MonthsToSubtract { get; set; }
[Input("Weeks To Subtract")]
[Default("0")]
public InArgument<int> WeeksToSubtract { get; set; }
[Input("Days To Subtract")]
[Default("0")]
public InArgument<int> DaysToSubtract { get; set; }
[Input("Hours To Subtract")]
[Default("0")]
public InArgument<int> HoursToSubtract { get; set; }
[Input("Minutes To Subtract")]
[Default("0")]
public InArgument<int> MinutesToSubtract { get; set; }
}
}
4. After creating your class file, just build it and open Plugin Registration and add the assembly to your CRM.
5. Now, goto your CRM solution and add create a new process(Add/ Subtract). In Add Step choose the assembly and the plugin you want to add to this process, as shown in image below
6. Goto Add Step and in Set properties, set what value you want to add. Save and Close
7. Now if you want to perform this workflow on Updation of record click on Update record or whatever you want, just select the step and in the Set Properties goto your Date field which you want to manipluate and click on that field and on the RHS goto Find For and you will find the custom workflow as shown in image below,select it and click on Result in the dropdown below to that of Look For dropdown. CLick Ok. Save and activate workflow.
8. Goto your entity and chck. Your workflow is working or not.
Comments
Post a Comment