using Microsoft.Xrm.Sdk;
using System;
using System.ServiceModel;
namespace its_ain_boring
{
public class Class1 : 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 trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
trace.Trace("optain the tracking service");
Entity entity = (Entity)context.InputParameters["target"];
if (entity.LogicalName == "account")
{
try
{
trace.Trace("inside try");
//latebound
Entity createcontact = new Entity("contact");
createcontact["firstname"] = "Minh";
createcontact["lastname"] = "Nguyen";
createcontact["emailaddress1"] = "nhatminhnguyen1122@gmail.com";
createcontact["description"] = "boss";
if (context.OutputParameters.Contains("id"))
{
createcontact["parentcustomerid"] = new EntityReference { Id = new Guid(context.OutputParameters["id"].ToString()), LogicalName = entity.LogicalName };
}
service.Create(createcontact);
}
catch (FaultException ex)
{
throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}
}
}
}
}