Association in mscrm
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
namespace Commissions
{
public class User_Commissions:IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
#region Context
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
ITracingService Tracing = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
Tracing.Trace("Depth= " + context.Depth);
if (context.Depth > 1)
return;
#endregion
Entity CommissionRoleEnt=null;
Relationship relationship = new Relationship();
if (!context.InputParameters.Contains("Relationship"))
{
//throw new Exception("return");
Tracing.Trace("return");
return;
}
else
{
relationship=(Relationship)context.InputParameters["Relationship"];
Tracing.Trace("relationship" + relationship);
}
//if(context.PrimaryEntityName.ToLower()=="systemuser"&&context.SecondaryEntityName.ToLower()=="apy_commissionroles")
// User = service.Retrieve(context.PrimaryEntityName,context.PrimaryEntityId,new ColumnSet(true));apy_apy_commissionroles_systemuser
//throw new Exception("firing1 " + User);
if (relationship != null && relationship.SchemaName == "apy_apy_commissionroles_systemuser")
{
EntityReference CommissionRole = (EntityReference)context.InputParameters["Target"];
EntityReferenceCollection UseR = (EntityReferenceCollection)context.InputParameters["RelatedEntities"];
Tracing.Trace("logicalname= " + UseR[0].LogicalName);
if (UseR[0].LogicalName == "systemuser" && CommissionRole.LogicalName == "apy_commissionroles")
{
//Guid UserId = ef[0].Id;//service.Retrieve(ef[0].LogicalName, ef[0].Id, new ColumnSet(true));
CommissionRoleEnt = service.Retrieve(CommissionRole.LogicalName, CommissionRole.Id, new ColumnSet(true));//target Entity
Tracing.Trace("CommissionRoleEnt" + CommissionRoleEnt);
bool TopLevelRole = CommissionRoleEnt.Contains("apy_toplevelrole") ? CommissionRoleEnt.GetAttributeValue<Boolean>("apy_toplevelrole") : false;
//throw new Exception("TopLevelRole " + TopLevelRole);
Tracing.Trace("apy_toplevelrole exists");
bool ResultRole = false;
if (TopLevelRole == true)
{
EntityCollection coll = GetCommissionRoles(service, UseR[0].Id, CommissionRole.Id,Tracing);
Tracing.Trace("Name= " + UseR[0].Name + "Id= " + UseR[0].Id);
//throw new Exception("count="+coll.Entities.Count.ToString());
if (coll != null && coll.Entities.Count > 0)
{
ResultRole = true;
//foreach (Entity comm in coll.Entities)
//{
// ResultRole = comm.Contains("apy_toplevelrole") ? comm.GetAttributeValue<bool>("apy_toplevelrole") : false;
// if (ResultRole == true)
// {
//break;
// }
//}
}
}
if (ResultRole == true)
{
throw new Exception("You may only have one top level commission role");
}
}
}
}
public EntityCollection GetCommissionRoles(IOrganizationService Service, Guid userId, Guid CommissionId,ITracingService Tracing)
{
string fetch = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
"<entity name='apy_commissionroles'>" +
"<attribute name='apy_commissionrolesid' />" +
"<attribute name='apy_name' />" +
"<filter type='and'>"+
"<condition attribute='apy_toplevelrole' operator='eq' value='1' />"+
"<condition attribute='apy_commissionrolesid' operator='ne' uitype='apy_commissionroles' value='" + CommissionId + "' />" +
"</filter>"+
"<order attribute='apy_name' descending='false' />" +
"<link-entity name='apy_apy_commissionroles_systemuser' from='apy_commissionrolesid' to='apy_commissionrolesid' visible='false' intersect='true'>" +
"<link-entity name='systemuser' from='systemuserid' to='systemuserid' alias='aa'>"+
"<filter type='and'>" +
"<condition attribute='systemuserid' operator='eq' uitype='systemuser' value='" + userId + "' />" +
"</filter>" +
"</link-entity>" +
"</link-entity>" +
"</entity>" +
"</fetch>";
EntityCollection collection = Service.RetrieveMultiple(new FetchExpression(fetch));
if (collection.Entities.Count > 0)
{
return collection;
}
return null;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
namespace Commissions
{
public class User_Commissions:IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
#region Context
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
ITracingService Tracing = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
Tracing.Trace("Depth= " + context.Depth);
if (context.Depth > 1)
return;
#endregion
Entity CommissionRoleEnt=null;
Relationship relationship = new Relationship();
if (!context.InputParameters.Contains("Relationship"))
{
//throw new Exception("return");
Tracing.Trace("return");
return;
}
else
{
relationship=(Relationship)context.InputParameters["Relationship"];
Tracing.Trace("relationship" + relationship);
}
//if(context.PrimaryEntityName.ToLower()=="systemuser"&&context.SecondaryEntityName.ToLower()=="apy_commissionroles")
// User = service.Retrieve(context.PrimaryEntityName,context.PrimaryEntityId,new ColumnSet(true));apy_apy_commissionroles_systemuser
//throw new Exception("firing1 " + User);
if (relationship != null && relationship.SchemaName == "apy_apy_commissionroles_systemuser")
{
EntityReference CommissionRole = (EntityReference)context.InputParameters["Target"];
EntityReferenceCollection UseR = (EntityReferenceCollection)context.InputParameters["RelatedEntities"];
Tracing.Trace("logicalname= " + UseR[0].LogicalName);
if (UseR[0].LogicalName == "systemuser" && CommissionRole.LogicalName == "apy_commissionroles")
{
//Guid UserId = ef[0].Id;//service.Retrieve(ef[0].LogicalName, ef[0].Id, new ColumnSet(true));
CommissionRoleEnt = service.Retrieve(CommissionRole.LogicalName, CommissionRole.Id, new ColumnSet(true));//target Entity
Tracing.Trace("CommissionRoleEnt" + CommissionRoleEnt);
bool TopLevelRole = CommissionRoleEnt.Contains("apy_toplevelrole") ? CommissionRoleEnt.GetAttributeValue<Boolean>("apy_toplevelrole") : false;
//throw new Exception("TopLevelRole " + TopLevelRole);
Tracing.Trace("apy_toplevelrole exists");
bool ResultRole = false;
if (TopLevelRole == true)
{
EntityCollection coll = GetCommissionRoles(service, UseR[0].Id, CommissionRole.Id,Tracing);
Tracing.Trace("Name= " + UseR[0].Name + "Id= " + UseR[0].Id);
//throw new Exception("count="+coll.Entities.Count.ToString());
if (coll != null && coll.Entities.Count > 0)
{
ResultRole = true;
//foreach (Entity comm in coll.Entities)
//{
// ResultRole = comm.Contains("apy_toplevelrole") ? comm.GetAttributeValue<bool>("apy_toplevelrole") : false;
// if (ResultRole == true)
// {
//break;
// }
//}
}
}
if (ResultRole == true)
{
throw new Exception("You may only have one top level commission role");
}
}
}
}
public EntityCollection GetCommissionRoles(IOrganizationService Service, Guid userId, Guid CommissionId,ITracingService Tracing)
{
string fetch = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +
"<entity name='apy_commissionroles'>" +
"<attribute name='apy_commissionrolesid' />" +
"<attribute name='apy_name' />" +
"<filter type='and'>"+
"<condition attribute='apy_toplevelrole' operator='eq' value='1' />"+
"<condition attribute='apy_commissionrolesid' operator='ne' uitype='apy_commissionroles' value='" + CommissionId + "' />" +
"</filter>"+
"<order attribute='apy_name' descending='false' />" +
"<link-entity name='apy_apy_commissionroles_systemuser' from='apy_commissionrolesid' to='apy_commissionrolesid' visible='false' intersect='true'>" +
"<link-entity name='systemuser' from='systemuserid' to='systemuserid' alias='aa'>"+
"<filter type='and'>" +
"<condition attribute='systemuserid' operator='eq' uitype='systemuser' value='" + userId + "' />" +
"</filter>" +
"</link-entity>" +
"</link-entity>" +
"</entity>" +
"</fetch>";
EntityCollection collection = Service.RetrieveMultiple(new FetchExpression(fetch));
if (collection.Entities.Count > 0)
{
return collection;
}
return null;
}
}
}
Comments
Post a Comment