Tuesday, 8 September 2015

Show or hide 'Add New' or '+' button on form sub-grid based on the value on the form

Hi,
 
Today I got a requirement to Hide/Show "+" button on the Sub grid based on some conditions.
 
I used Ribbon workbench for doing it. But, unfortunately it was not working. After some search came to know that I am trying to customize wrong button. Actually, we should apply our commands on the highlighted button.
Here are the steps to be followed for hiding and showing the button.
 
Create a Webresource and write you condition in it. Make sure it returns True/False.

function EnableNewButton() {
    return true;
}

Create a solution in CRM and add the required entity and webresource.
Open the same solution in Ribbon Workbench.
Right-Click on the button and click Customize Command.
Its shows all the existing Command rules like Display rules, Enable rules etc..
Right click on the new customized command and click Edit Enable Rules.
Click on Add New button and Select Custom JavaScript Rule.
Change the Id if you wish and Click on Add Step
Select Custom JavaScript Rule.
Select the WebResource and Javascript function name.
Click on Publish.

Done, now the button will be hidden/shown based on the Boolean value that is returning from the JavaScript function.

Hope this helps.
 
--
Happy CRM'ing
Gopinath 

5 comments:

  1. Hi Gopinath

    I am getting the following exception while publishing this , could you please help me on this.

    Reasons:
    1:The "Show error message" and "Set value" actions can't be used in a business rule that doesn't have a condition.

    ReplyDelete
  2. Hi Siva,

    I think, its is related to the business rule on the entity. Check the business rule on the entity and then try to publish.

    ReplyDelete
  3. Hi Gopi

    the button is not hide .Even i am not get any error.

    My condition :


    function DisableNewButton()
    {
    var status = Xrm.Page.getAttribute("new_statusapprover").getValue();
    debugger;
    //if status reason is Approved or Approval Pending
    if (status == 100000002)
    {
    return false;
    }

    else
    {
    return true;
    }
    }

    Entity name:Account
    Attribute name :Statusapprover(Optionset)
    options:new 100000000, approve for submitted(100000001) ,approve and reject(100000002)
    Event: onchange



    help me


    ReplyDelete
  4. Hi Gopi

    the button is not hide .Even i am not get any error.

    My condition :


    function DisableNewButton()
    {
    var status = Xrm.Page.getAttribute("new_statusapprover").getValue();
    debugger;
    //if status reason is Approved or Approval Pending
    if (status == 100000002)
    {
    return false;
    }

    else
    {
    return true;
    }
    }

    Entity name:Account
    Attribute name :Statusapprover(Optionset)
    options:new 100000000, approve for submitted(100000001) ,approve and reject(100000002)
    Event: onchange



    help me


    ReplyDelete
  5. Thank you gopi :)
    Great Post. Its working for me.

    ReplyDelete