Tuesday, November 24, 2009

Clearing fields in Microsoft Dynamics CRM 4.0

I've used following code to clear fields (set properties of entities to null):

account acc = new account();
acc.accountid = new Key(new Guid("249FB2CF-31CD-DE11-AB59-005056B30605"));

Lookup lookup = new Lookup();
lookup.IsNull = true;
lookup.IsNullSpecified = true;

acc.primarycontactid = lookup;
crmservice.Update(acc);


This code works but there is more elegant and readable way to clear fields:

account acc = new account();
acc.accountid = new Key(new Guid("249FB2CF-31CD-DE11-AB59-005056B30605"));
acc.primarycontactid = Lookup.Null;
crmservice.Update(acc);


Similar approach also will work for CrmDateTime, CrmNumber, Picklist, Customer, CrmMoney, CrmFloat, CrmDecimal fields.

Tuesday, November 03, 2009

Custom workflow action which returns current DateTime value

Here is the code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Workflow.Activities;
using Microsoft.Crm.Workflow;
using System.Workflow.ComponentModel;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.Sdk.Query;
using Microsoft.Crm.SdkTypeProxy;

namespace CurrentDateTimeWorkflowStep
{
[CrmWorkflowActivity("Current Date Time", "Utiles")]
public class CurrentDateTime : SequenceActivity
{
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
Value = CrmDateTime.Now;

return base.Execute(executionContext);
}

public static DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(CrmDateTime), typeof(CurrentDateTime));

[CrmOutput("Value")]
public CrmDateTime Value
{
get
{
return (CrmDateTime)base.GetValue(ValueProperty);
}
set
{
base.SetValue(ValueProperty, value);
}
}
}
}


And here is the source project and built assembly.