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


If you want to solve this, then follow the below steps

Create sales person lookup field in account and point to User entity.



 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 



Change the lookup of the User and select the which user selected on Record and check the access

Don't Confuse, I write the code to share but how the read will come. Because without read access on record we can't share record


Comments

Popular posts from this blog

create HTML page show all contact related to specific account, add this webresource as a subgrid in the form

Connect MS Crm To Postman For Webapi Calls.

create one PCF control and embed into crm