简体   繁体   中英

Get lookup which causes retrieve multiple request - dynamics 365 plugin

I am using a pre-operation retrieve multiple plugin to add a condition to account subgrid lookups. This works fine, however it applies to all queries on account entities. I want it to only apply when the user accesses the lookup within one subgrid on one form. Is there any way to retrieve the lookup which fires the query? Alternatively is the any way to achieve what I want to do by other means? The purpose of this is to filter the accounts which can be added to the subgrid.

Here is my code:

public class FilterAversedSuppliers : IPlugin
    {

        public void Execute(IServiceProvider serviceProvider)
        {
            //Extract the tracing service for use in debugging sandboxed plug-ins.
            ITracingService tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = (IPluginExecutionContext)
                serviceProvider.GetService(typeof(IPluginExecutionContext));

            // The InputParameters collection contains all the data passed in the message request.
            if (context.InputParameters.Contains("Query") &&
                context.InputParameters["Query"] is QueryExpression)
            {

                try
                {
                    QueryExpression objQueryExpression = (QueryExpression)context.InputParameters["Query"];

                    ConditionExpression condition = new ConditionExpression()
                    {
                        AttributeName = "customertypecode",
                        Operator = ConditionOperator.Equal,
                        Values = { 4 }
                    };

                    objQueryExpression.Criteria.AddCondition(condition);

                    tracingService.Trace("Custom Filter Added");
                }
                catch (FaultException<OrganizationServiceFault> ex)
                {
                    throw new InvalidPluginExecutionException("An error occurred in the FollowupPlugin plug-in.", ex);
                }

                catch (Exception ex)
                {
                    tracingService.Trace("FollowupPlugin: {0}", ex.ToString());
                    throw;
                }
            }

        }
    }

On the criteria for the lookup view, add something like “Name” equals “FilterMe”.

Now in your plugin, inspect the incoming fetchxml query. If it contains your special criteria, you know to apply your special filtering. Don't forget to remove the special criteria from the query in your code.

Now all other queries should not trigger your special filter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM