Associate/Disassociate in CRM using plugin
Senario : Let's assume i have one entity name is A and second entity name is B. In both of them N:N relationship is exits. B entity is exits on the form of A entity as a Tab now once i remove the the record of A entity that removed record lookup field name we need to take and compare with the field of different tab (Vistying)which is present in form of B entity.
Note : On CRM UI you can able to identify whether entity is having N:N relationship or not with other entity. Check if two of the entity does not contain +new button that means only having add existing button it means both of them having N:N relationship.
Screenshot of entity: In given figure Entity B is containing sub grid of Entity A and both of them is having N:N relationship.
Coding :Component:
Entity B field is :-
Entity A field is :-
Entity B Form:-Now code start from here:
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
namespace DissocitionOF_record_Entity_A
{
public class Class1 : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
try
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
EntityReference targetEntity = null;
EntityReference relatedEntity = null;
Entity entityA = null;
Entity entityB = null;
string relationshipName = string.Empty;
if(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference)
{
targetEntity=context.InputParameters["Target"] as EntityReference; //entityb
}
if(context.InputParameters.Contains("Relationship"))
{
relationshipName = ((Relationship)context.InputParameters["Relationship"]).SchemaName;
}
if(relationshipName != "new_new_entitya_new_entityb")
{
return;
}
if(context.InputParameters.Contains("RelatedEntities") && context.InputParameters["RelatedEntities"] is EntityReferenceCollection)
{
EntityReferenceCollection relatedEntityCol = context.InputParameters["RelatedEntities"] as EntityReferenceCollection;
relatedEntity = relatedEntityCol[0];
}
entityB = service.Retrieve(targetEntity.LogicalName, targetEntity.Id, new ColumnSet(true));
entityA = service.Retrieve(relatedEntity.LogicalName, relatedEntity.Id, new ColumnSet("new_entityaid", "new_lookupina"));
if(context.MessageName.ToLower() == "disassociate")
{
if(entityA != null && entityA.Attributes.Contains("new_lookupina"))
{
if(entityA.GetAttributeValue<EntityReference>("new_lookupina").Name == "Visit01")
{
entityB["new_visit01"] = new OptionSetValue(100000000);
}
else if (entityA.GetAttributeValue<EntityReference>("new_lookupina").Name == "Visit02")
{
entityB["new_visit02"] = new OptionSetValue(100000000);
}
else if (entityA.GetAttributeValue<EntityReference>("new_lookupina").Name == "Visit03")
{
entityB["new_visit03"] = new OptionSetValue(100000000);
}
service.Update(entityB);
}
}
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException("An error occured in : " + ex.Message.ToString(), ex);
}
}
}
}
You can also watch this scenario on my YouTube Channel:
https://www.youtube.com/watch?v=nBYj9LQiBC4&t=276s
Note : Please leave the comment.
In Microsoft Company Dynamics 365 Partner, AlphaBOLD is a top Microsoft dynamics 365 partner that works on Microsoft Dynamics Sales more. Choose the right Microsoft Dynamics 365 Consulting Providers in San Diego.
ReplyDelete