Based on user Lookup, Give access using plugin In microsoft dynamic 365 crm
Q1: Create sales person lookup field in account and point to User entity.When ever user select the sales person lookup field value and save the account record, record need to share to the selected sales person. write a plugin to share the records
Then we need to write the plugin using C#. So I write the below plugin to give access on record
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = (IOrganizationService)factory.CreateOrganizationService(context.UserId);
ITracingService tracing = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
Entity Accountdata = (Entity)context.InputParameters["Target"];
Guid accountid = Accountdata.Id;
Guid Salesid = new Guid();
try
{
EntityReference salespeson = Accountdata.Contains("crc78_salesperson") ? (EntityReference)Accountdata["crc78_salesperson"] : null;
if (salespeson != null)
{
Salesid = salespeson.Id;
}
GrantAccessRequest grantAccess = new GrantAccessRequest
{
PrincipalAccess = new PrincipalAccess
{
Principal = new EntityReference("systemuser", Salesid),
AccessMask = AccessRights.ShareAccess
},
Target = new EntityReference(Accountdata.LogicalName, accountid)
};
service.Execute(grantAccess);
tracing.Trace("recrod is shared");
Above Code is to give share access on record.
After Writting the code we need to check the access shared or not then follow the give procedure.
Select the Check Acess button in Form level ribbon bar.
It will open
Comments
Post a Comment