Thanks Bipin I used the associate on create its working fine but on update of division i am not able to do i checked in console its working but in plugin am getting error Object not set to reference an object .
Please find the below where i am going wrong, am checking null all where but not able to find out the issue as cant debug the code as its onpremise & in server no vs exists.
try
{
IPluginExecutionContext executionContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
organizationService = serviceFactory.CreateOrganizationService(executionContext.UserId);
organizationServiceContext = new OrganizationServiceContext(organizationService);
Entity incident = new Entity();
bool IsGridData = false;
if (executionContext.MessageName.ToUpper() == "UPDATE")
{
if (executionContext.InputParameters.Contains("Target") && (executionContext.InputParameters["Target"] is Entity))
{
if ((Entity)executionContext?.InputParameters["Target"] != null)
incident = (Entity)executionContext.InputParameters["Target"];
tracingService.Trace("Got Incident");
//EntityReference categoryType = null;
//Entity createPostImage = (Entity)executionContext.PostEntityImages["ConflictofInterestPostImage"];
//if (createPostImage.Contains("mdc_interactiontype"))
// categoryType = (EntityReference)createPostImage["mdc_interactiontype"];
ColumnSet attributes = new ColumnSet(new string[] { "mdc_relateddivision", "mdc_relatedasset" });
Entity entityCase = organizationService.Retrieve(incident.LogicalName, incident.Id, attributes);
EntityReference division = entityCase.Attributes.Contains("mdc_relateddivision") ? entityCase?.GetAttributeValue<EntityReference>("mdc_relateddivision") : null;
EntityReference asset = entityCase.Attributes.Contains("mdc_relatedasset") ? entityCase?.GetAttributeValue<EntityReference>("mdc_relatedasset") : null;
tracingService.Trace("Division got...." + division.Id);
tracingService.Trace("Asset got...." + asset.Id);
if (entityCase.Attributes.Contains("mdc_relateddivision") && division?.Id != null )
{
divisionId = (entityCase.GetAttributeValue<EntityReference>("mdc_relateddivision")).Id;
tracingService.Trace($"Got Division Id here....: " + divisionId);
//Retrieving Compliance Counsel grid records
ConditionExpression condition = new ConditionExpression();
condition.AttributeName = "mdc_compliancecounselid";
condition.Operator = ConditionOperator.Equal;
if (incident?.Id != null && incident?.Id != Guid.Empty)
condition.Values.Add(incident.Id);
//Create a column set.
ColumnSet columns = new ColumnSet(true);
QueryExpression query1 = new QueryExpression();
query1.ColumnSet = columns;
query1.EntityName = "systemuser";
query1.Criteria.AddCondition(condition);
List<Entity> relatedUsers = new List<Entity>();
EntityCollection resColl = organizationService.RetrieveMultiple(query1);
tracingService.Trace($"Subgrid records" + resColl.Entities.Count);
if (resColl?.Entities != null)
{
IsGridData = true;
tracingService.Trace($"subgrid has data");
foreach (var r in resColl.Entities)
{
relatedUsers.Add(r);
DisassociateRequest disassociateReq = new DisassociateRequest();
if (incident?.Id != null && incident.Id != Guid.Empty)
disassociateReq.Target = new EntityReference("incident", incident.Id);
disassociateReq.RelatedEntities = new EntityReferenceCollection();
if (r?.Id != null)
disassociateReq.RelatedEntities.Add(new EntityReference("systemuser", r.Id));
disassociateReq.Relationship = new Relationship("mdc_incident_systemuser");
if (disassociateReq != null)
organizationService.Execute(disassociateReq);
tracingService.Trace($"Disaasociated Users successfully");
}
}
if (IsGridData) //Check Compliance Counsel grid has no records then Associate Users
{
tracingService.Trace($"Grid has no records");
QueryExpression query = new QueryExpression();
query.EntityName = "systemuser";
query.ColumnSet = new ColumnSet(true);
Relationship relationship = new Relationship();
query.Criteria = new FilterExpression();
relationship.SchemaName = "mdc_mdc_division_systemuser";
tracingService.Trace($"Relationship exists");
RelationshipQueryCollection relatedEntity = new RelationshipQueryCollection();
if (relationship != null && query != null)
relatedEntity.Add(relationship, query);
RetrieveRequest request = new RetrieveRequest();
request.RelatedEntitiesQuery = relatedEntity;
request.ColumnSet = new ColumnSet("mdc_divisionid");
if (divisionId != null)
request.Target = new EntityReference { Id = divisionId, LogicalName = "mdc_division" };
RetrieveResponse response = (RetrieveResponse)organizationService.Execute(request);
tracingService.Trace($"Got Response");
List<Entity> relatedEntities = new List<Entity>();
if (((((RelatedEntityCollection)(response.Entity.RelatedEntities)))).Contains(new Relationship("mdc_mdc_division_systemuser")) &&
((((RelatedEntityCollection)(response.Entity.RelatedEntities))))[new Relationship("mdc_mdc_division_systemuser")].Entities.Count > 0)
{
foreach (var item in ((((RelatedEntityCollection)(response.Entity.RelatedEntities))))[new Relationship("mdc_mdc_division_systemuser")].Entities)
{
relatedEntities.Add(item);
AssociateRequest request1 = new AssociateRequest();
if (incident?.Id != null)
request1.Target = new EntityReference(incident.LogicalName, incident.Id);
request1.RelatedEntities = new EntityReferenceCollection();
if (item?.Id != null)
request1.RelatedEntities.Add(new EntityReference("systemuser", item.Id));
request1.Relationship = new Relationship("mdc_incident_systemuser");
organizationService.Execute(request1);
tracingService.Trace($"Associated Users successfully");
}
}
}
}
//else if(entityCase.Attributes.Contains("mdc_relatedasset") && asset != null)
//{
// assetId = (entityCase.GetAttributeValue<EntityReference>("mdc_relatedasset")).Id;
// tracingService.Trace($"Got Asset Id here....: " + assetId);
//}
}
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException("An error occurred in the Update GE Compliance counsel Plug-in: " + ex.Message, ex);
}
Please let me know it will be helpful.
Thanks