web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to apply retrieve multiple plugin for only an entity?

(0) ShareShare
ReportReport
Posted on by

I have to create a plugin to modify query for 'contact' subgrid in 'account' form. I created a Plugin with Retrieve Multiple message for it and it's work for me but there is a problem for the plugin. The plugin will be called when I open a record (without account record) which contains a contact subgrid. I don't know how to apply the plugin for only account. Please help me to fix that.

Thanks.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi Toten,

    RetrieveMultiple plugin will trigger every time CRM loads any Contact subgrid. This could be the contact home page grid, subgrid in any other entity, advance find query or any other associated view etc.

    You cannot restrict this based on any specific entity. However, you can have the following workaround to achieve your requirement -

    On Contact Entity:

    1. Create a new field say 'new_viewforaccount'.

    2. Create a new view say 'Account Subgrid View'

    3. Customize it as per your needs and in addition to that, add another condition as "View For Account Field" does not contain data.

    On Account Entity

    4. Use this view on the Contact subgrid.

    Once done, publish your customization.

    Update your plugin code as follows

    ----------------------------

    if (context.MessageName == "RetrieveMultiple" && context.InputParameters.Contains("Query") && context.InputParameters["Query"] is QueryExpression)

               {

                   QueryExpression qe = (QueryExpression)context.InputParameters["Query"];

                   if (qe.EntityName == "contact")

                   {

                       var runLogic = false;

                       var CE = qe.Criteria.Conditions.ToList();

                       foreach (var condition in CE)

                       {

                           if (condition.AttributeName == "new_viewforaccount")

                           {

                               runLogic = true;                            

                           }

                       }

                       if (runLogic)

                       {

                           Entity ent = new Entity("task");

                           ent["subject"] = "From Plugin";

                           service.Create(ent);

                       }          

                   }

               }

    -----------------------------

  • rath.amit38@gmail.com Profile Picture
    2 on at

    Hi Toten,

    Could you please paste your code where the logic is applied so that i can modify it for you.

    Thanks,

    Amit Kumar Rath

  • Community Member Profile Picture
    on at

    Hi Ravi Kashyap,

    I followed your suggestion but I always see 2 conditions: 'statecode' & 'contactid'. I don't know why. Can you send me some suggestions.

    Thanks

  • RaviKashyap Profile Picture
    55,410 Moderator on at

    Where do you see these condition? Can you send a screenshot please.

  • Community Member Profile Picture
    on at

    Below are my code & the picture:

    public void Execute(IServiceProvider serviceProvider)
    {
    ITracingService tracer = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    tracer.Trace("Stage:" + context.Stage);
    if (context.MessageName != "RetrieveMultiple" || context.Stage != 20 || context.Mode != 0 ||
    !context.InputParameters.Contains("Query") ||
    !(context.InputParameters["Query"] is QueryExpression))
    {
    tracer.Trace("Not expected context");
    return;
    }

    var query = (QueryExpression)context.InputParameters["Query"];
    if (query != null && query.EntityName == "contact")
    {
    query.Criteria = null;
    context.InputParameters["Query"] = query;
    }
    }

    8640.crm.png

  • RaviKashyap Profile Picture
    55,410 Moderator on at

    Have you completed the other steps (create field, create new view, update new view on account entity).

    The query criteria should return the filter condition you have added on the view

  • RaviKashyap Profile Picture
    55,410 Moderator on at

    Also, can you tell.me whats the final outcome you are expecting.

    from the code you shared, it looks like you wanted to remove all the conditions from the query ?

  • Community Member Profile Picture
    on at

    Thanks Ravi Kashyap,

    I will try to follow that again.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans