Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Ribbon button, Enable rule, Record exists

Posted on by 417

Hi,

is it possible to disable a ribbon button with an enable rule in the "Visual Ribbon Editor"? The rule should work as follows:

Entity: Mail

Button: Create request

IF

entity "request" does contain a related record

THEN

disable button

ELSE

enable button

*This post is locked for comments

  • bernhards Profile Picture
    bernhards 417 on at
    RE: Ribbon button, Enable rule, Record exists

    Thank you both for your answers. :) It will take some time to test it out but I will give you feedback.

  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: Ribbon button, Enable rule, Record exists

    Hi Bernhards,

    The following links give you sample code on how to retrieve the related records.

    https://community.dynamics.com/crm/b/crminogic/archive/2011/09/07/retrieve-related-entity-records-along-wih-the-primary-entity-using-retrieve-method

    http://congruentdynamics.blogspot.in/2013/05/retrieve-linked-entity-data-using-query.html

    To debug the plugin, you could use the plugin profiler in plugin registration tool and attach the visual studio solution to plugin registartion tool. The step by step process is described below in detail.

    https://dynamics365blocks.wordpress.com/2016/12/06/how-to-debug-a-plugin-in-dynamics-365-online-using-plugin-profiler/

    Hope this helps.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Ribbon button, Enable rule, Record exists

    You should register your plugin on account as you want to check the related cases of account. Below is the sample code:

    if (context.InputParameters.Contains("Target") &&

                   context.InputParameters["Target"] is Entity)

               {

                   //Obtain the target entity from the input parameters

                   Entity incident = (Entity)context.InputParameters["Target"];

                   //Verify that the target entity represents an account

                   //If not, this plug-in was not registered correctly

                   if (incident.LogicalName != "account")

                       return;

                   try

                   {

                       //Obtain the organisation service reference

                       IOrganizationServiceFactory serviceFactory =

                           (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

                       IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                       QueryExpression query = new QueryExpression("incident");

                       query.ColumnSet = new ColumnSet(false);

    query.Criteria.AddCondition(new ConditionExpression("customerid",ConditionOperator.Equal,context.PrimaryEntityId)

                       EntityCollection entityCollection = service.RetrieveMultiple(query);

                                           throw new InvalidPluginExecutionException(entityCollection.Entities.Count.ToString());

                   }

                   catch (Exception ex)

                   {

                       tracingService.Trace("Fail while counting: {0}", ex.ToString());

                       throw;

                   }

  • bernhards Profile Picture
    bernhards 417 on at
    RE: Ribbon button, Enable rule, Record exists

    That's great! :) I tried to write a plugin and it worked on retrieving and counting the records of an entity. But now I am stuck. How can I retrieve the records of a related entity? Also another question and a bit off-topic.. Is there a better way to debug the CRM plugin then using the Exception? (I am new to CRM Development and F5 in VS didn't do anything)

    public void Execute(IServiceProvider serviceProvider)
            {
                //Extract the tracing service for use in debuggin 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("Target") &&
                    context.InputParameters["Target"] is Entity)
                {
                    //Obtain the target entity from the input parameters
                    Entity incident = (Entity)context.InputParameters["Target"];
    
                    //Verify that the target entity represents an incident
                    //If not, this plug-in was not registered correctly
                    if (incident.LogicalName != "incident")
                        return;
    
                    try
                    {
                        //Obtain the organisation service reference
                        IOrganizationServiceFactory serviceFactory =
                            (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
    
                        int totalCount = 0;
    
                        QueryExpression query = new QueryExpression("incident");
                        query.ColumnSet = new ColumnSet();
                        query.Distinct = true;
                        query.ColumnSet.AddColumn("incidentid");
                        query.PageInfo = new PagingInfo();
                        query.PageInfo.Count = 5000;
                        query.PageInfo.PageNumber = 1;
                        query.PageInfo.ReturnTotalRecordCount = true;
    
                        EntityCollection entityCollection = service.RetrieveMultiple(query);
                        totalCount = entityCollection.Entities.Count;
    
                        while (entityCollection.MoreRecords)
                        {
                            query.PageInfo.PageNumber += 1;
                            query.PageInfo.PagingCookie = entityCollection.PagingCookie;
                            entityCollection = service.RetrieveMultiple(query);
                            totalCount = totalCount + entityCollection.Entities.Count;
                        }
    
                        totalCount -= 1; //just to give the right count in the invExp
                        throw new InvalidPluginExecutionException(totalCount.ToString());
    
                    }
                    catch (Exception ex)
                    {
                        tracingService.Trace("Fail while counting: {0}", ex.ToString());
                        throw;
                    }
                }
            }
  • Suggested answer
    Nithya Gopinath Profile Picture
    Nithya Gopinath 17,074 on at
    RE: Ribbon button, Enable rule, Record exists

    Hi Bernhard,

    You could enable or disable a ribbon button dynamically based on a form value (bool field value) using Ribbon Workbench.

    See: https://ribbonworkbench.uservoice.com/knowledgebase/articles/121427-enable-disable-a-ribbon-button-dynamically-based-o

    Hope this helps.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Ribbon button, Enable rule, Record exists

    This works too. In the plugin just query the cases with the current record's relationship (match the lookup of record on case). Then you can get the count from the response and update the field.

  • bernhards Profile Picture
    bernhards 417 on at
    RE: Ribbon button, Enable rule, Record exists

    My plan is to count the related incidents with a c# plugin every update/create/delete and set the value of the bool field on the mail form to TRUE/FALSE. (This would effect the enable rule of the button)

    Is this workaround a good idea?

    How can I count the related records from an entity with c#?

    Meaning:

    IF

    relatedIncidents > 1

    THEN

    Field = TRUE

    (pre/post image is not needed I think?)

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Ribbon button, Enable rule, Record exists

    Its a small query where you just need to check the count so it shouldn't make the form slow. Also, it will be working on button click to I guess there should not be a problem. However, in new versions MS has introduced WebAPIs which replaces oData and is much better as well.

  • bernhards Profile Picture
    bernhards 417 on at
    RE: Ribbon button, Enable rule, Record exists

    No problem we plan to migrate :)

    Is this oData query a stable way? I don't want to implement too much JS code. There is already alot of it which slows down everything.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Ribbon button, Enable rule, Record exists

    Sorry, My fault. I did not notice that you are working on 2011. Rollup fields are not present in CRM 2011. You can run oData query using JS to check if there are any related records.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans