I've optimized the code a little bit. But I see a few strange things. See the comments. And where is this part?
I was asked to create an auto-generated field in Work Order entity that sets its value based on its service account.
You retrieve the account but then nothing happens.
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Linq;
using System.ServiceModel;
namespace MG
{
public class WorkOrder : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Extract the tracing service for use in debugging sandboxed plug-ins.
// If you are not registering the plug-in in the sandbox, then you do
// not have to add any tracing service related code.
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parameters.
Entity entity = (Entity)context.InputParameters["Target"];
// Verify that the target entity represents an entity type you are expecting.
// For example, an account. If not, the plug-in was not registered correctly.
if (entity.LogicalName != "msdyn_workorder")
return;
// Obtain the organization service reference which you will need for
// web service calls.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
try
{
if (entity.Contains("msdyn_serviceaccount"))
{
var accountId = entity.GetAttributeValue<EntityReference>("msdyn_serviceaccount").Id;
//This line is not needed
var account = service.Retrieve("account", accountId, new ColumnSet(true)); //ColumnSet(true) :(
//What does this line?
var nameField = account.Id;
//Why retrieve the first workorder??
var workOrder = RetrieveWorkOrder(service, nameField);
var x = entity.GetAttributeValue<string>("new_workordernumber");
if (workOrder != null)
{
var y = workOrder.GetAttributeValue<string>("new_workordernumber");
if (y == null)
{
entity.Attributes.Add("new_workordernumber", "1");
}
else
{
entity.Attributes.Add("new_workordernumber", "2");
}
}
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occurred in MyPlug-in.", ex);
}
catch (Exception ex)
{
tracingService.Trace("MyPlugin: {0}", ex.ToString());
throw;
}
}
}
private Entity RetrieveWorkOrder(IOrganizationService service, Guid nameField)
{
var query = new QueryExpression("msdyn_workorder");
query.TopCount = 1;
query.ColumnSet.AddColumns("msdyn_serviceaccount", "new_workordernumber");
query.Criteria.AddCondition("msdyn_serviceaccount", ConditionOperator.Equal, nameField);
var result = service.RetrieveMultiple(query);
if (result != null && result.Entities.Any())
return result.Entities.First();
return null;
}
}
}