MS Dynamics CRM 4.0 – Adding Notes to a Workflow E-mail

In a recent post on the Microsoft Dynamics CRM Team blog, Jim Steger discuss how you can easily overcome some of the native limitations of the CRM workflow Send E-mail action with some simple workflow assembly classes. Recently, I have been implementing the new eService Accelerator for our own corporate web site and came across a need for another CRM workflow e-mail utility…the ability to include all the notes of a record into the body of a workflow e-mail.
To accomplish this, I created a new activity class within my workflow utilities project and used the following code:

using System;
using System.Workflow.ComponentModel;
using System.Workflow.Activities;
using System.Web.Services.Protocols;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Sdk.Query;
using Microsoft.Crm.Workflow;
using System.Collections;
using System.Text;

namespace SonomaPartners.Crm.Workflow.Utilities
{
[CrmWorkflowActivity(“Format Notes”, “Utilities”)]
public partial class FormatNotes : SequenceActivity
{
protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
{
IContextService contextService = (IContextService)executionContext.GetService(typeof(IContextService));
IWorkflowContext ctx = contextService.Context;
ICrmService service = ctx.CreateCrmService();

this.FormattedNotes = BuildNotesString(service, ctx.PrimaryEntityId);

return base.Execute(executionContext);
}

public static DependencyProperty FormattedNotesProperty = DependencyProperty.Register(“FormattedNotes”, typeof(string), typeof(FormatNotes));
[CrmOutput(“Formatted Notes”)]
public string FormattedNotes
{
get { return (string)base.GetValue(FormattedNotesProperty); }
set { base.SetValue(FormattedNotesProperty, value); }
}

private string BuildNotesString(ICrmService service, Guid id)
{
StringBuilder results = new StringBuilder();
results.Append(“Notes:
“);

BusinessEntityCollection notes = new BusinessEntityCollection();
notes = RetrieveNotesForId(service, id);

foreach (annotation note in notes.BusinessEntities)
{
results.Append(string.Format(“{0}
“, note.subject));
results.Append(string.Format(“{0}

“, note.notetext));
}

return results.ToString();
}

private BusinessEntityCollection RetrieveNotesForId(ICrmService service, Guid id)
{
QueryByAttribute query = new QueryByAttribute();
query.EntityName = “annotation”;
query.ColumnSet = new ColumnSet(new String[] { “subject”, “notetext”, “createdby”, “createdon” });

query.Attributes = new String[] { “objectid” };
query.Values = new Object[] { id };
query.Orders = new ArrayList();
query.Orders.Add(new OrderExpression(“createdon”, OrderType.Descending));

try
{
RetrieveMultipleRequest request = new RetrieveMultipleRequest();
request.Query = query;

RetrieveMultipleResponse returnedNotes = (RetrieveMultipleResponse)service.Execute(request);
return returnedNotes.BusinessEntityCollection;
}
catch (SoapException ex)
{
throw new Exception(ex.Detail.InnerText);
}
}
}
}

Now, your e-mail includes any notes in the body of the message

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: