Hello Aileen,
i used the Early Bound generator XRMBOX tool.
My snippet
protected void ExecutePreInvoiceCreate(LocalPluginContext localContext)
{
if (localContext == null)
{
throw new ArgumentNullException("localContext");
}
IPluginExecutionContext context = localContext.PluginExecutionContext;
IOrganizationService service = localContext.OrganizationService;
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
if (context.MessageName == "Create")
{
//Code to be executed during Create event of an entity
try
{
Guid guidContactID = Guid.Empty;
guidContactID = ((EntityReference)entity["customerid"]).Id;
using (ServiceContext svcContext = new ServiceContext(service))
{
var registryNumber = (from m in svcContext.ContactSet
where m.ContactId == guidContactID
select new Contact
{
New_RegistryNumber = m.New_RegistryNumber
});
foreach (var c in registryNumber)
{
if (entity.Attributes.Contains("new_customernumber"))
{
entity.Attributes["new_customernumber"] = c.New_RegistryNumber;
}
else
{
entity.Attributes.Add("new_customernumber", c.New_RegistryNumber);
}
}
}
}
catch (FaultException ex)
{
throw new InvalidPluginExecutionException("An error occurred in the Invoice Create plugin:-", ex);
}
}
}
}