Hi Guys, getting this error while trying to trigger this plugin on associate of n:n relation. Plugin message "Associate" post Sync. Here for every n:n dcu record associated with Req plugin should create a new Door record with.
using Microsoft.Xrm.Sdk; using Microsoft.Xrm.Sdk.Query; using System; namespace new.newCreatedoor { public class newCreatedoor : IPlugin { public void Execute(IServiceProvider serviceProvider) { ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = factory.CreateOrganizationService(context.UserId); try { Entity entity = (Entity)context.InputParameters["Target"]; if (entity.LogicalName.ToLower() != "new_req") { return; } var entityId = (entity != null) ? entity.Id : Guid.Empty; Relationship relationship = (Relationship)context.InputParameters["Relationship"]; if (relationship.SchemaName != "new_dcu_req") { return; } var ccRecordType = entity.GetAttributeValue<OptionSetValue>("new_recordtype").Value; if (ccRecordType == 800000000 || ccRecordType == 800000001 || ccRecordType == 800000002 || ccRecordType == 800000003) { var ec = GetdcuIds(service, entityId); if (ec != null && ec.Entities.Count > 0) { foreach (var dcu in ec.Entities) { var dcuId = dcureq.GetAttributeValue<Guid>("new_dcuid"); if (dcuId != null) { var dcuEntity = Getdcu(service, dcuId); if (dcuEntity != null) { var doorEntity = new Entity("new_doors"); doorEntity["new_recordtype"] = new OptionSetValue(800000000); if (dcuEntity.GetAttributeValue<string>("new_address") != null) doorEntity["new_accountphysicaladdress"] = dcuEntity.GetAttributeValue<string>("new_address").ToString(); var newManager = dcuEntity.GetAttributeValue<EntityReference>("new_manager"); if (newManager != null) doorEntity["new_accountmanagername"] = new EntityReference(newManager.LogicalName, newManager.Id); var newOperationManager = dcuEntity.GetAttributeValue<EntityReference>("new_operationmanager"); if (newOperationManager != null) doorEntity["new_operationsmanager"] = newOperationManager.Name; doorEntity["new_dcuid"] = new EntityReference(dcuEntity.LogicalName, dcuEntity.Id); var newAccount = entity.GetAttributeValue<EntityReference>("new_account"); if (newAccount != null) doorEntity["new_account"] = new EntityReference(newAccount.LogicalName, newAccount.Id); var newOpportunity = entity.GetAttributeValue<EntityReference>("new_opportunity"); if (newOpportunity != null) doorEntity["new_opportunity"] = new EntityReference(newOpportunity.LogicalName, newOpportunity.Id); doorEntity["new_req"] = new EntityReference(entity.LogicalName, entity.Id); if (dcuEntity.Attributes.Contains("new_shipperreceiverdoor") && dcuEntity["new_shipperreceiverdoor"] != null) doorEntity["new_shipperreceiverdoor"] = dcuEntity["new_shipperreceiverdoor"].ToString(); doorEntity["new_solicitordoor"] = "WCBH"; if (dcuEntity.Attributes.Contains("new_costcenter") && dcuEntity["new_costcenter"] != null) doorEntity["new_costcenter"] = dcuEntity["new_costcenter"].ToString(); service.Create(doorEntity); } } } } } } catch (Exception e) { throw new InvalidPluginExecutionException(e.Message); } }
public EntityCollection GetdcuIds(IOrganizationService service, Guid reqid) { var fetch = "<fetch>" + " <entity name='new_dcu_req' >" + " <all-attributes/>" + " <filter type='and' >" + " <condition attribute='new_reqid' operator='eq' value='" + reqid + "' />" + " </filter>" + " </entity>" + "</fetch>"; var erl = service.RetrieveMultiple(new FetchExpression(fetch)); return erl; } public Entity Getdcu(IOrganizationService service, Guid dcuId) { var entity = service.Retrieve("new_dcu", dcuId, new ColumnSet("new_address", "new_manager", "new_subregion", "new_operationmanager", "new_city", "new_state", "new_shipperreceivercode", "new_costcenter")); return entity; } } }
*This post is locked for comments