Monday, December 29, 2008

Adding Notes to a Workflow E-mail

Posted by Jim Steger on December 23, 2008

In a recent post on the Microsoft Dynamics CRM Team blog, I 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:<br>");

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

foreach (annotation note in notes.BusinessEntities)
{
results.Append(string.Format("{0}<br>", note.subject));
results.Append(string.Format("{0}<br><br>", 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 as shown here:



Feel free to download the Visual Studio 2008 workflow utilities assembly solution file.



Btw, I also wanted to thank Steve Kaplan and Jim Glass for allowing me to contribute on the Microsoft Dynamics CRM Team blog.





Posted by Jim Steger on December 23, 2008 | Permalink

No comments: