Hi,
I have written a birthday plugin, which fills 3 new fields birth day, birth month, birth year on create or update of a contact.
The plugin is registered via plugin registration tool as pre-operation for create and update contact. For the update step a pre-image for the "owningbusinessunit" is added.
The plugin runs fine, when I manually create or update a contact in the CRM 2011 GUI. But for all contacts created or update via the web service (daily import) the plugin fails with following error:
06-04-2015 13:09:01,883 ERROR [ 3] Unexpected exception from plug-in (Execute): BirthdayPlugin.BirthdayField: System.NullReferenceException: Object reference not set to an instance of an object. (File:131)
06-04-2015 13:09:01,883 ERROR [ 3] Code: -2147220956 (File:131)
06-04-2015 13:09:01,884 ERROR [ 3] Message: Unexpected exception from plug-in (Execute): BirthdayPlugin.BirthdayField: System.NullReferenceException: Object reference not set to an instance of an object. (File:131)
06-04-2015 13:09:01,884 ERROR [ 3] Plugin Trace:
[BirthdayPlugin: BirthdayPlugin.BirthdayField]
[xxxx: BirthdayPlugin.BirthdayField: Update of contact]
At first I thought it is related to this line in my code:
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
Because the contact is created/updated by the "SYSTEM" user, so probably no UserId is being returned. But now I changed the plugin to run as a specific user instead of calling user and the problem still persists.
I would be grateful for any help.
thx in advance Thomas
Source code:
using
System;
using
System.Diagnostics;
using
System.Linq;
using
System.ServiceModel;
using
Microsoft.Xrm.Sdk;
using
Microsoft.Xrm.Sdk.Query;
using
Microsoft.Crm.Sdk;
using
Microsoft.Crm.Sdk.Messages;
namespace
BirthdayPlugin
{
public class BirthdayField : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(
typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
try
{
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
tracingService.Trace(
"EntityName:" + entity.LogicalName);
// Verify that the target entity represents a contact.
// If not, this plug-in was not registered correctly.
if (entity.LogicalName == "contact")
{
// Image für Update Step --> enthält alle Felder des Contacts
Entity prebirthdayimage;
if (context.MessageName == "Create")
{
// Deklarierung des Namens der Owning Business Unit als Variable
//Check ob die Owning Business Unit AT ist --> für CZ passiert kein Update der neuen Geburtstagsfelder
EntityReference owningBusinessUnit = (EntityReference)entity.Attributes["owningbusinessunit"];
Entity owningBU = service.Retrieve(owningBusinessUnit.LogicalName, owningBusinessUnit.Id, new ColumnSet(true));
var atowner = owningBU.Attributes["name"].ToString();
//Check if the plugin context contains the attribute birthday (=create or update of birthdate in CRM). If yes --> copy to new birthday/month/year fields
if (entity.Attributes.Contains("birthdate") && atowner == "AT")
{
//Neue Variable die den Geburtstag eines Contacts enthält
DateTime birthday = (DateTime)entity["birthdate"];
//Update bzw. Befüllung der 3 neuen Geburtstagsfelder am Contact
entity[
"new_birthday"] = birthday.ToLocalTime().Day;
entity[
"new_birthmonth"] = new OptionSetValue(birthday.ToLocalTime().Month);
entity[
"new_birthyear"] = birthday.ToLocalTime().Year;
}
}
if (context.MessageName == "Update")
{
// Deklarierung des Namens der Owning Business Unit als Variable
prebirthdayimage = (
Entity)context.PreEntityImages["ImageBirthday"];
EntityReference UpdateOwningBusinessUnit = (EntityReference)prebirthdayimage.Attributes["owningbusinessunit"];
Entity UpdateowningBU = service.Retrieve(UpdateOwningBusinessUnit.LogicalName, UpdateOwningBusinessUnit.Id, new ColumnSet(true));
var UpdateAT = UpdateowningBU.Attributes["name"].ToString();
//Check if the plugin context contains the attribute birthday (=create or update of birthdate in CRM). If yes --> copy to new birthday/month/year fields
//Check ob die Owning Business Unit AT ist --> für CZ passiert kein Update der neuen Geburtstagsfelder
if (entity.Attributes.Contains("birthdate") && UpdateAT == "AT")
{
//Neue Variable die den Geburtstag eines Contacts enthält
DateTime birthday = (DateTime)entity["birthdate"];
//Update bzw. Befüllung der 3 neuen Geburtstagsfelder am Contact
entity[
"new_birthday"] = birthday.ToLocalTime().Day;
entity[
"new_birthmonth"] = new OptionSetValue(birthday.ToLocalTime().Month);
entity[
"new_birthyear"] = birthday.ToLocalTime().Year;
}
}
}
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occurred in Birthday plugin", ex);
}
}
}
}