Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Suggested answer

Plug-in Step Filters not working

Posted on by 40

Hi experts,

I have made a Assembly with a couple of plugins for a Model Driven App in Power Apps. I'm using the Plugin Registration Tool to add plugins/debug them. Everything was working fine until I imported the solution I made to Production. In production Filters were not working. So I tried exporting without Plugin Profiler/ with Profiler... Now in Test environment the filters are also not working.

A example:

I have a plugin that will fire when Price field of  Training Entitie changes (Update: Filter attribute, Price).

I have another plugin that will fire when StartBudget field/StartDays field of  TraineeBudget Entitie changes  (Update: Filter attribute, StartBudget & StartDays).

When I change the comment field of Training Entitie (which is not included in Filter attributes) and save the Training record it fires the first Plugin. This plugin would make changes to the TraineeBudget Entitieremaining budget field and remaining days field. This causes the second plugin also to fire (while this plugins only would fire on startbudget, startdays).

2022_2D00_01_2D00_12-13_5F00_47_5F00_56_2D00_.png

I disabled all workflows to make sure these weren't causing it.. I also tried changing my Plugins from Postoperation to Preoperation to see if this would make any change but filters are still not working. I looked all over the internet to find a solution to my issue but nothing changes..

Best Regards,

Anthony

  • Mohsin Ali Profile Picture
    Mohsin Ali 3,610 on at
    RE: Plug-in Step Filters not working

    I am assuming either comment field is added somewhere in filters to trigger the plugin execution. Or at on change of Comment field price field is getting updated through workflow, business rules.

  • AnthonyD Profile Picture
    AnthonyD 40 on at
    RE: Plug-in Step Filters not working

    I only have 1 Business Rule in the application and I deactivated it. I only have a JavaScript file to open a new Extra Cost record as a centered dialog and a JavaScript file to add icons in a grid.

    Here's a code snippet of the Update TraineeBudget Record:

     public class UpdateTraineeBudgets : IPlugin
        {
            private static IOrganizationService _service;
            private IOrganizationServiceFactory _serviceFactory;
            private IPluginExecutionContext _context;
            public void Execute(IServiceProvider serviceProvider)
            {
                _context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    
                if (_context.Depth > 1)
                {
                    return;
                }
    
                _serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                _service = _serviceFactory.CreateOrganizationService(_context.UserId);
    
                if (_context.MessageName == "Update")
                {
                    UpdateBudgets();
                }
            }
            private void UpdateBudgets()
            {
                {
                    var targetEntity = _context.InputParameters["Target"] as Entity;
                    var preUpdate = EntityMapper.Map(_context.PreEntityImages["cref8_jaarlijkbudget"]);
                    var budget = EntityMapper.Map(_service.Retrieve(targetEntity.LogicalName, targetEntity.Id, new ColumnSet("cref8_jaarlijkbudgetid","cref8_overigebudget","cref8_overigedagen","cref8_startbudget","cref8_startdagen")));
    
                    var budgetToSave = new Entity("cref8_jaarlijkbudget");
                    budgetToSave["cref8_jaarlijkbudgetid"] = budget.Id;
    
                    if (budget.StartDays != 0)
                    {
                        double? newDays = budget.StartDays - (preUpdate.StartDays - budget.RemainingDays);
                        budgetToSave["cref8_overigedagen"] = newDays;
                    }
    
                    if (budget.StartBudget != 0)
                    {
                        double? newBudget = budget.StartBudget - (preUpdate.StartBudget - budget.RemainingBudget);
                        budgetToSave["cref8_overigebudget"] = newBudget;
                    }
    
                    _service.Update(budgetToSave);
                }
            }
        }

    Here's a code snippet of the Update TrainingCost:

     public class UpdateTrainingCost : IPlugin
        {
            private static IOrganizationService _service;
            private IOrganizationServiceFactory _serviceFactory;
            private IPluginExecutionContext _context;
            public void Execute(IServiceProvider serviceProvider)
            {
                _context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    
                if (_context.Depth > 1)
                {
                    return;
                }
    
                _serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                _service = _serviceFactory.CreateOrganizationService(_context.UserId);
    
                if (_context.MessageName == "Update")
                {
                    UpdateBudgets();
                }
            }
    
            private void UpdateBudgets()
            {
                var targetEntity = _context.InputParameters["Target"] as Entity;
                var preUpdate = EntityMapper.Map(_context.PreEntityImages["cref8_opleiding"]);
                var training = EntityMapper.Map(_service.Retrieve(targetEntity.LogicalName, targetEntity.Id, new ColumnSet("cref8_jaarstartopleiding", "cref8_opleidingid", "cref8_prijs")));
    
                var cursists = GetCursists("cref8_cursist", "cref8_opleiding_cref8_cursist", "cref8_cursistid", "cref8_opleidingid", training.Id);
    
                foreach (var cursist in cursists)
                {
                    var traineeBudget = GetTraineeBudget(cursist, training);
    
                    double nieuwBudget = traineeBudget.RemainingBudget   preUpdate.Price - training.Price;
    
                    Entity budget = new Entity("cref8_jaarlijkbudget");
                    budget["cref8_jaarlijkbudgetid"] = traineeBudget.Id;
                    budget["cref8_overigebudget"] = nieuwBudget;
    
                    _service.Update(budget);
                }
            }
            private static TraineeBudget GetTraineeBudget(Cursist cursist, Training training)
            {
                ConditionExpression ceYearlyBudgetFromTrainee = new ConditionExpression("cref8_cursist", ConditionOperator.Equal, cursist.Id);
                ConditionExpression ceYearlyBudgetYear = new ConditionExpression("cref8_jaar", ConditionOperator.Equal, (int)training.Year);
                FilterExpression filter = new FilterExpression();
                filter.Conditions.Add(ceYearlyBudgetFromTrainee);
                filter.Conditions.Add(ceYearlyBudgetYear);
                QueryExpression qeYearlyBudget = new QueryExpression("cref8_jaarlijkbudget");
                qeYearlyBudget.ColumnSet = new ColumnSet("cref8_jaarlijkbudgetid", "cref8_overigebudget");
                qeYearlyBudget.Criteria.AddFilter(filter);
                EntityCollection yearlyBudgetResult = _service.RetrieveMultiple(qeYearlyBudget);
    
                return EntityMapper.Map(yearlyBudgetResult.Entities.First());
            }
    
            private static List GetCursists(string entityName, string linkToEntityName, string linkAttributeName, string attributeName, Guid id)
            {
                var query = new QueryExpression()
                {
                    EntityName = entityName,
                    ColumnSet = new ColumnSet("cref8_cursistid", "cref8_fullname"),
                };
                var link = query.AddLink(linkToEntityName, linkAttributeName, linkAttributeName);
                link.LinkCriteria = new FilterExpression()
                {
                    Conditions =
                                        {
                                            new ConditionExpression(attributeName, ConditionOperator.Equal, id)
                                        }
                };
    
                return _service.RetrieveMultiple(query).Entities.Select(e => EntityMapper.Map(e)).ToList();
            }
        }

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,962 Moderator on at
    RE: Plug-in Step Filters not working

    Hi,

    When I change the comment field of Training Entitie (which is not included in Filter attributes) and save the Training record it fires the first Plugin

    Check if you have any Business Rule activated which is casuing this issue. Also, check if any javascript is setting the field value on change of Comment field which is causing this plugin to fire.

    This plugin would make changes to the TraineeBudget Entitie, remaining budget field and remaining days field. This causes the second plugin also to fire (while this plugins only would fire on startbudget, startdays).

    Can you please share code snippet which you used to update TrianingBudget Entity record. I hope you are only mapping required fields but not others.

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,620 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,884 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,150

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans