Monday, December 15, 2008

MS CRM 4.0 Plug-ins vs. CRM 3.0 Callouts

Plug-ins are a huge improvement over the callouts available in MS CRM 3.0. They are much more flexible, much easier to deploy, and have a many more triggers allowing a lot more access to the inner operation of your CRM system.

The following comparison shows some of the key differences:

Callout

Plug-in

Deployment

Requires Workflow service to be restarted to re-read the callout.config.xml which defines which assemblies and classes to call for each triggered event.

Requires assemblies be copied to each CRM server in your web server farm. Because those assemblies can be busy this can require:

  1. Draining each server in an NLB configuration.
  2. An IIS reset
  3. Stopping the Workflow service.
  • Allows deployment to the database in a single step for all CRM servers.
  • Or deployment to the GAC on each server.
  • Or deployment to a folder for debugging.

 

  • Requires a strongly typed assembly.

Flexibility

Requires a different interface for each Message. A PreCreate has a different interface from a PostCreate, or a PreUpdate, or a PostUpdate. Has the same interface for all messages.

Supports many more messages
Supported Messages for Plug-ins

  Has parent and child pipeline feature
  Has the ability to watch for endless loops.

Coding

Requires parsing Xml images to see the contents of the entity generating the message: preImageEntityXml, postImageEntityXml

Returns a Dynamic Entity or Moniker instead.

var entity = (DynamicEntity)context.InputParameters.Properties[ParameterName.Target];

var myMoniker = (Moniker)context.InputParameters.Properties[ParameterName.Target];

Allows validity checking and stopping a change to the database in a preCallout

errorMessage = "Validation Failed."; return PreCalloutReturnValue.Abort;
                

Allows validity checking and stopping an operation in a Pre message.

throw new InvalidPluginExecutionException(
"Validity Failed. {1}");

Allows modifying data before it is written to the database in a  preCallout.

Parse and Modify the entityXML               

Allows modifying data before it is written to the database in a Pre message.

Modify the Dynamic entity.

String nameProperty = entity.Properties["name"] as String;
nameProperty = "new name";


Resources

VS Plug-in template This template will create a basic Plug-in Project and is a good starting point.

  • The example it creates is for a Dynamic Entity Target. This is useful for Create and Update messages, but many other messages will return a Moniker which gives you the id of the calling entity.
  • The template includes code to call a customized crmservice which can be useful, although it is faster and a better practice to call your service from the context as shown below if you are comfortable with Dynamic Entities:

    ICrmService service = context.CreateCrmService(true);

Plugin Deployment Tool This tool will allow you a simple way to deploy your plug-ins. and has an easy to use UI.

With plug-ins it is important to learn about Dynamic Entities.

If you purchase David Yak's CRM as a Rapid Development Platform, it comes with a number of helper classes for Dynamic Entities, and plug-ins as well as other useful tools that are interesting. His chapters on plug-ins and dynamic entities are useful, and the code he provides is worth downloading and referring to. He also has chapters specific to using his plug-in framework for debugging and other tasks which are not CRM development generic.

I recently ordered a copy of Programming Microsoft Dynamics CRM 4.0, and I'll try to remember to update this post after I have looked over its coverage of Plug-ins and dynamic entities.

Wednesday, November 26, 2008

Interactive Webcast: The Top 5 Ways to Save Money with CRM

I've been invited back to be a guest speaker for an interactive Webcast on December 4th at 10:00 AM Pacific, 1:00 PM Eastern, 6:00PM GMT. This is through CBS interactive and Tech Republic.

http://webcasts.techrepublic.com.com/abstract.aspx?docid=390610&promo=100202

Thursday, November 20, 2008

Queues in MS CRM

There are a lot of misconceptions about queues. They only apply to cases and activities.  While the terminology is the same, assigning a case or activity to a queue is not the same as assigning an entity to a CRM user or team. Programmatically what is being used is a RouteRequest rather than an AssignRequest.

To get a better idea how this works we'll look at database. There are two entities storing queue information, the Queue and the QueueItem. These are not customizable.

Queue – (table) contains all queues

  • There are two queues automatically created for each CRM user. 
    • Assigned
    • In Progress
  • Any user defined queues that you create are also stored here.
    • example:  My New Public Queue

QueueItem – (link table)  Contains an entry for each entity assigned to a queue.

  • An item can only belong to one Queue at a time,
  • The only entities that can below to a Queue are:
    • Activities ( email, tasks, etc. )
    • Cases

An activity or case retains its owner and is not modified in any way when it is assigned to a queue. It is just added to the QueueItem list.  An entity can ONLY belong to one queue at a time.

Normal Lifecycle

When created a case or activity is added to the Assigned queue of the owner and it will stay there until it is completed/closed/canceled.

Sometimes a workflow will assign an entity to a user defined queue. (Below is an example that just puts all created Cases into a Support queue.) You might want logic with timers to move Cases around, or you might have different queues by subject, or related account territory. The important thing is that the queue is used as a natural part of the customer's business process.

image

When someone selects an entity in the queue and clicks  Accept... that entity it is then moved into that person's "In Progress" queue.

image

Example code working with Queues

Creating a Queue

var newQueue = new queue
{
    name = "My New Public Queue",
    businessunitid = new Lookup { Value = BusinessUnitId, type = EntityName.businessunit.ToString() },
    primaryuserid = new Lookup { Value = UserId, type = EntityName.systemuser.ToString() },
    queuetypecode = new Picklist { name = "Public", Value = 1 }
};

service.Create(newQueue);

Assigning an incident to a Queue

// Target the incident var target = new TargetQueuedIncident { EntityId = incidentId }; // Create a RouteRequest ( you would need to query for your Queue Id's ) var route = new RouteRequest { Target = target, RouteType = RouteType.Queue,
SourceQueueId = currentQueueId.Value EndpointId = finalQueueId, }; // Execute the route var routed = (RouteResponse)service.Execute(route);

Tracking Queue Assignment in a Plugin

One of the related improvements to MS CRM 4.0 is the inclusion of the Route Message for Plug-ins.  For example, you can now trigger based on a Case being routed to a queue even though the case entity is not modified in any way.

Your code can inspect the SourceQueueId to see what queue it is coming from and the EndpointId to see the destination queue.

This link shows a complete list of the Plug-in Message Input Parameters. You can see that the Route Message has 3 input parameters, two which I mentioned (SourceQueueId and EndpointId) as well as RouteType which has three values ( Auto = 0 ( automatic route) , User = 1 (route to a user's private queue), Queue =  2 (route to a public queue) ) You could use the RouteType to filter out a chunk of queue routes that wish to ignore.

The fact that the dynamic entity of an incident contains no information about the queue it is assigned to means that you need to examine the context.InputParameters.Properties to find out which queue the case is coming from and which queue it will finally call home.

public void Execute(IPluginExecutionContext context)
   {
      DynamicEntity entity = null;

    if (context.InputParameters.Properties.Contains(ParameterName.Target) &&
       context.InputParameters.Properties[ParameterName.Target] is DynamicEntity)
    {
        entity = (DynamicEntity)context.InputParameters.Properties[ParameterName.Target];
        
        if (entity.Name != EntityName.incident.ToString()) { return; }
        if (context.MessageName != MessageName.Route.ToString()) { return; }
        
        Guid SourceQueueId = ((Moniker)context.InputParameters.Properties["SourceQueueId"]).Id;
        Guid EndpointId = ((Moniker)context.InputParameters.Properties["EndpointId"]).Id;
    }

Wednesday, November 19, 2008

A Couple Data import issues

I thought these items might be of help to other people populating data in MS CRM systems. The first is documented, but for the second I found no information on web.

Issue 1: When importing email activities, like all other activities in the vast majority of instances you need to complete them so they show up in history and not in the current Activities of a user's Workplace or against the items to which they are regarded.

If you import an email message with a From email address that is not a CRM system user, you can create the activity without a problem, but will get a Soap Exception "The specified sender type is not supported." When you try to set the state of the email to "Received". The following hot fix will allow you to Complete those incoming emails and set them to "Received".

http://support.microsoft.com/kb/947860/en-us

Issue 2:  While importing account information after a customer of mine added additional customertypecode picklist items I ran into a Soap Exception.

<code>0x8004431a</code>
<description>A validation error occurred.  The value of 'customertypecode' on record of type 'account' is outside the valid range.</description>

The customertypecode picklist starts at 1 and progresses linearly with the default values out of the box, and you can always rename the existing items, but any new picklist items start with values of 200001 and progress linearly from there.

Workaround: Edit an existing account or add a dummy account and select a new pick list item with a higher ranged value and save that account record. Now the problem magically disappears.

Hypothesis: I'm thinking that the CRM webservices are using reflection like Excel does on a spreadsheet column but against the database and it initially ranges the pick list field as a short, but only ranges it up when a larger value exists.

If anyone has a better understanding of why this behavior occurs, I would love to hear about it.

Wednesday, November 12, 2008

Interactive Webcast: Using CRM to Drive Sales in a Slowing Economy

I've been invited to be the guest speaker for an interactive Webcast on November 13th at 1:00 PM Pacific, 4:00 PM Eastern, 9:00PM GMT. This is through CBS interactive and Tech Republic.

http://webcasts.techrepublic.com.com/abstract.aspx?docid=390611

Friday, November 7, 2008

Annotation Import web service error

During a recent import I was getting some exceptions while adding annotations to Accounts and Contacts. The exceptions being generated were not SoapExceptions, but HTTP 400 malformed http request errors.

This was a data driven problem and I assumed that it was an illegal character messing up the SOAP. The annotation.notetext attribute was the only one being set to a large chunk of unknown text. Debugging this showed some illegal characters in my source data. Initially I found an occasional "\0" which is easily removed, but there was another character that showed up as a box symbol.

So how do you filter that out?

First you need to know what you are looking at, so while debugging copy the offending character into the clipboard and paste it into a text file. In Visual Studio there is a binary editor.

File -> Open File
After selecting your file click notice the small pick list to the right of Open.
Select Open With...

evilcharacter

Viewing the text file revealed that the offending character was a hex 12. I had copied three of them into NotePad. For the curious among you a hex 12 = DC2 or Device Control 2.

hex_editor

Armed with that information the following statement filtered out the offending characters and the import completed without error.

newAnnotation.notetext = note.Replace('\0', ' ').Replace('\u0012', ' ');

Below is a code example using this filter to generate contact annotations.

var newAnnotation = new annotation
       {
          objectid = CrmTypes.CreateLookup(EntityName.contact.ToString(), contactId),
          objecttypecode = CrmTypes.CreateEntityNameReference(EntityName.contact.ToString()),
          isdocument = CrmTypes.CreateCrmBoolean(false),
          ownerid = newContact.ownerid,
          subject = "GoldMine notes",
          notetext = note.Replace('\0', ' ').Replace('\u0012', ' ')
       };

try
{
    service.Create(newAnnotation);
}

This could have been solved a number of other ways like a regex that only allows valid characters for a SOAP request, but I haven't run into this issue enough to create one.

Sunday, October 26, 2008

Creating an ActivityParty in MS CRM 4.0

In an earlier Blog I covered creating activities, but did not cover the activityparty in depth. Each activity has a number of activityparty arrays containing lookups to the CRM systemuser, contact, lead, and account entities that are involved in that activity.

For many purposes the only attributes that need to be set in each ActivityParty are the participationtypemask and partyid. The name participationtypemask is a little misleading since it needs to be set to a picklist with a value from the ActivityPartyType rather than actually being a bitmask like the SecurityPrincipal AccessMask.

There are numerous ActivityPartyTypes used for the different ActivityParty arrays in each activity. A complete list of  these types is located here MSDN ActivityPartyType Class. The partyid is just a lookup to the appropriate systemuser, contact, lead or account.

Typically when importing activities you will be matching against an email address and/or contact or lead name. I'd included some example code that will creates an activityparty array for use in creating an activity based on email addresses. A few examples of how I would call this are the following:

thisEmail.to = CreateActivityParty(to_rfc882, ActivityPartyType.ToRecipient);
thisEmail.from = CreateActivityParty(from_rfc882, ActivityPartyType.Sender);
thisEmail.cc = CreateActivityParty(cc_rfc882, ActivityPartyType.CcRecipient);
thisEmail.bcc = CreateActivityParty(bcc_rfc882, ActivityPartyType.BccRecipient);

This shows an email entity having a number of attributes assigned an appropriate activityparty array.

This code takes the emailAddresses string from a given email and creates a list of email addresses that will be part of the ActivityParty array. Then it looks for a match for each email address against systemusers, contacts, leads and finally accounts. Notice that an ActivityParty array can consist of any combination of these entities.

/// <summary>
/// Create that list of Parties associated with each email address in the email list
/// </summary>
/// <param name="emailAddresses"></param>
/// <param name="addressType"></param>
/// <returns></returns>
private activityparty[] CreateActivityParty(string emailAddresses, int addressType)
{
    var activityPartiesPerEmail = new List<activityparty>();

    List<string> addresses = CleanEmail(emailAddresses);  // Get a clean email list from the rfc822 address line.

    //Find All Activity parties 
    
    // Pecking order System Users, Contacts, Leads, Accounts

    foreach ( var emailAddress in addresses)
    {
        BusinessEntityCollection users = FindEntitiesByAttribute(emailAddress, EntityName.systemuser.ToString(), "internalemailaddress");

        if (users.BusinessEntities.Length > 0)
        {
            foreach (systemuser user in users.BusinessEntities)
            {
                activityPartiesPerEmail.Add( new activityparty
                    {
                        participationtypemask = CrmTypes.CreatePicklist(addressType),
                        partyid = CrmTypes.CreateLookup(EntityName.systemuser.ToString(), user.systemuserid.Value)
                    }
                  );
                break;
            }
        }
        else
        {
            BusinessEntityCollection contacts = FindEntitiesByAttribute(emailAddress, EntityName.contact.ToString(), "emailaddress1");

            if (contacts.BusinessEntities.Length > 0)
            {
                foreach (contact aContact in contacts.BusinessEntities)
                {
                    activityPartiesPerEmail.Add(new activityparty
                          {
                              participationtypemask = CrmTypes.CreatePicklist(addressType),
                              partyid = CrmTypes.CreateLookup(EntityName.contact.ToString(), aContact.contactid.Value)
                          }
                        );
                    break;
                }
            }
            else
            {
                BusinessEntityCollection leads = FindEntitiesByAttribute(emailAddress, EntityName.lead.ToString(), "emailaddress1");

                if (leads.BusinessEntities.Length > 0)
                {
                    foreach (lead aLead in leads.BusinessEntities)
                    {
                        activityPartiesPerEmail.Add(new activityparty
                            {
                                participationtypemask = CrmTypes.CreatePicklist(addressType),
                                partyid = CrmTypes.CreateLookup(EntityName.lead.ToString(), aLead.leadid.Value)
                            }
                          );
                        break;
                    }
                }
                else
                {
                    BusinessEntityCollection accounts = FindEntitiesByAttribute(emailAddress, EntityName.account.ToString(), "emailaddress1");

                    if (accounts.BusinessEntities.Length > 0)
                    {
                        foreach (account anAccount in accounts.BusinessEntities)
                        {
                            activityPartiesPerEmail.Add(new activityparty
                                {
                                    participationtypemask = CrmTypes.CreatePicklist(addressType),
                                    partyid = CrmTypes.CreateLookup(EntityName.account.ToString(), anAccount.accountid.Value)
                                }
                            );
                            break;
                        }
                    }
                }
            }
        }
    }
    
    var theseParties = new activityparty[activityPartiesPerEmail.Count];

    activityPartiesPerEmail.CopyTo(theseParties);
    
    return theseParties;
}

/// <summary>
/// Returns all entities of a specified type filterd on a specified attribute and value
/// </summary>
/// <param name="emailAddress">attribute filter value</param>
/// <param name="entity">entityName</param>
/// <param name="attributeName">attribute to filter on</param>
/// <returns></returns>
public BusinessEntityCollection FindEntitiesByAttribute(string emailAddress, string entity, string attributeName)
{
    var condition = new ConditionExpression
    {
        AttributeName = attributeName,
        Operator = ConditionOperator.Equal,
        Values = new[] { emailAddress }
    };


    var filter = new FilterExpression
    {
        FilterOperator = LogicalOperator.And,
        Conditions = new[] { condition }
    };


    var query = new QueryExpression
    {
        EntityName = entity,
        ColumnSet = new AllColumns(),
        Criteria = filter
    };

    try
    {
        return service.RetrieveMultiple(query);
    }

    catch (SoapException ex)
    {
        string errormsg = String.Format("Error for FindSystemUserByEmailAddress {0} error {1}", emailAddress, ex.Detail.InnerText);

        WriteErrorFile(errormsg);
        throw;
    }
}

If you have any questions please let me know.