I want to create a new record in "Appointment" entity with the help of plug-in code. While i refreshing the page i got error
My code is:::::
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 CreateAppointment
{
public class CreateAppointmentPlugin : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
try
{
entity["subject"] = "Test Appointment";
entity["location"] = "patna";
entity["actualstart"] = "DateTime.Now.AddHours(1)";
entity["actualend"] = "DateTime.Now.AddHours(2)";
if (context.OutputParameters.Contains("id"))
{
entity["parentcustomerid"] = new EntityReference
{ Id = new Guid(context.OutputParameters["id"].ToString()), LogicalName = entity.LogicalName };
}
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService client = factory.CreateOrganizationService(context.UserId);
//client.Create(createappointment);
}
catch (InvalidPluginExecutionException ex)
{
throw new InvalidPluginExecutionException(ex.Message.ToString());
}
}
}
}
}