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

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? With Pre-Image concept?

Step 1: Create a look-up filed in account entity as Sales Person and in that look-up entity as user > next save and publish the account entity.

Step 2: Write a plugin for giving share privilege to particular user with the help of grant access and to remove share access from old user use pre image concept and revoke it .

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using Microsoft.Crm.Sdk.Messages;

using Microsoft.Xrm.Sdk;

namespace AcessSales

{

    public class salesACC : IPlugin

    {

        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("new_salespo") ? (EntityReference)Accountdata["new_salespo"] : null;

                    if (salesperson != null)

                    {

                        Salesid = salesperson.Id;

                    }

                    GrantAccessRequest grantAccess = new GrantAccessRequest

                    {

                        PrincipalAccess = new PrincipalAccess

                        {

                            Principal = new EntityReference("systemuser", Salesid),

                            AccessMask = AccessRights.ShareAccess,

                        },

                        Target = new EntityReference(Accountdata.LogicalName, Accountdata.Id)

                    };

                    service.Execute(grantAccess);

                    tracing.Trace("recrod is shared");

                    Entity preImage = (Entity)context.PreEntityImages["SALESpersonAccess"];

EntityReference newEnt = preImage.GetAttributeValue<EntityReference>("new_salespo");

                    if (newEnt != null)

                    {

                        var revokeUserAccessReq = new RevokeAccessRequest

                        {

                            Revokee = newEnt,

                            Target = new EntityReference(Accountdata.LogicalName, Accountdata.Id),

                        };

                        service.Execute(revokeUserAccessReq);

                    }

                    tracing.Trace("recrod");

                }

                catch (Exception ex)

                {

                    Console.WriteLine("This message: " + ex);

                }

            }

        }

    }

}

Step 3: Build the solution > Next Go to Register a new assembly in xrm tool or plugin registration > then find the .dll file and register the assembly .

 


Step 4: After Register the assembly then Register a new step

Taking all inputs to Register new step:


Step 5: Next Register an image for Pre-Image.


Step 6: Go to Dynamics CRM 365 Account entity and check the functionality is working according to the scenario or not.

 


Step 7: Select an user and check weather he as access of share record

 


Step 8: Now select another user and check he got share record access and then check old user shared record permission was removed or not 

 






Comments

Popular posts from this blog

If any case created, then check for the same user how many cases are created with in 30 days, if more than 2 and less than 5 send a mail to team lead, if more than 5 and less than 9 then send a mail to manager using power automate.

Create approve & reject ribbon buttons, once click on approve it should change the status field to approve.If clicked on reject it should change to Reject. Based on status field change trigger work flow to send a email to stating request is approved/Rejected.

How to get and set values in plugins?