using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
namespace Test
{
public class Test : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
throw new NotImplementedException();
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.Depth == 1)
{
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
// Obtain the target entity from the input parmameters.
Entity entity = (Entity)context.InputParameters["Target"];
//EntityReference entity = (EntityReference)context.OutputParameters["BusinessEntity"];
tracingService.Trace("3 - entity");
var contact = service.Retrieve("contact", entity.Id, new ColumnSet(true));
tracingService.Trace("4 - entity");
if (contact != null)
{
if (contact.Attributes.Contains("address1_name") == false)
{
Random rndgen = new Random();
contact.Attributes.Add("address1_name", "first time value: " + rndgen.Next().ToString());
}
else
{
contact["address1_name"] = "i already exist";
}
//service.Update(contact);
}
}
}
}
}
Still not working base on your suggestion same error again,
i've register my plugin for "preoperation"