I have created Access team template for account entity and added a sub grid on Account entity that is used to assign users.
When any user is added in the grid , then it should also add its Manager in the grid .
For this, i have created a plugin to implement it, but the code is not able to fetch the manager field into the sub grid.
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
EntityReference id = null;
EntityReference targetEntity = null;
EntityReference relatedEntity = null;
string relationshipName = string.Empty;
EntityReferenceCollection relatedEntities = null;
if (context.MessageName == "Associate")
{
if (context.InputParameters.Contains("Relationship"))
{
relationshipName = context.InputParameters["Relationship"].ToString();
}
if (relationshipName != "user_accounts")
{
return;
}
else
{
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference)
{
targetEntity = (EntityReference)context.InputParameters["Target"];
tracingService.Trace("Target Entity " + targetEntity.LogicalName.ToString() + targetEntity.Id.ToString());
}
if (context.InputParameters.Contains("RelatedEntities") && context.InputParameters["RelatedEntities"] is EntityReferenceCollection)
{
relatedEntities = context.InputParameters["RelatedEntities"] as EntityReferenceCollection;
relatedEntity = relatedEntities[0];
tracingService.Trace("Related Entity Ref " + relatedEntity.LogicalName.ToString() + relatedEntity.Id.ToString());
}
Entity RetrieveUser = service.Retrieve("systemuser", targetEntity.Id, new ColumnSet("parentsystemuserid"));
if (RetrieveUser.Attributes.Contains("parentsystemuserid") && RetrieveUser.Attributes["parentsystemuserid"] != null)
{
id = RetrieveUser.GetAttributeValue<EntityReference>("parentsystemuserid");
tracingService.Trace("Parent SystemUser Id " + id.Id.ToString());
AssociateContactsToAccount(relatedEntity, id, service);
}
}
}
}
public static void AssociateContactsToAccount(EntityReference account, EntityReference user, IOrganizationService service)
{
// Creating EntityReferenceCollection for the Contact
EntityReferenceCollection relatedEntities = new EntityReferenceCollection();
// Add the related entity contact
relatedEntities.Add(account);
// Add the Account Contact relationship schema name
Relationship relationship = new Relationship("user_accounts");
// Associate the contact record to Account
service.Associate(user.LogicalName, user.Id, relationship, relatedEntities);
}
*This post is locked for comments