Dynamics CRM Javascript · Dynamics CRM Ribbon Customization · Javascript

Dynamics 365 CRM Hide Subgrid Ribbon Button based on Parent Entity Field value

Today I was going through Dynamics Community Forum and one of the question which was asked is to hide subgrid button based on parent entity field value.

As we all know, we can not directly use executioncontext in ribbon button. We have to use PrimaryControl to get form context from ribbon. We will have to pass PrimaryControl from ribbon workbench as a parameter.

I decided to implement this in my trial instance. So I want to hide Add Product on Invoice subgrid Invoice Line Items when “Status Reason” field value is not ‘New’.

See below screen on how to pass PrimaryControl from ribbon workbench.

CQ6

Step 1:

Create new solution and add invoice line entity into it. Open ribbon workbench and choose the solution you created.

Step 2:

Navigate to Subgrid ribbon and select Add Product->right click and select customize command

Step 3:

Add new enable rule as custom rule to call below javascript code and pass primary control as a first parameter. Provide function name and choose web resource file.


function hideSub(primaryControl)
{
	var formContext = primaryControl;
	return (formContext.getAttribute(“statuscode”).getValue() === 1);
}

Step 4:

Publish your changes and navigate to Incoive form. Change Status reason from New to Partially Shipped and you will see Add Prouct button is not visible now.

CQ7

Hope this helps!

5 thoughts on “Dynamics 365 CRM Hide Subgrid Ribbon Button based on Parent Entity Field value

  1. Isn’t it easier to use

    return (formContext.getAttribute(“statuscode”).getValue() === 1);

    instead of
    if(formContext.getAttribute(“statuscode”).getValue() == 1)
    return true;
    else
    return false;
    ?

    Liked by 1 person

  2. You are loading the invoice entity into ribbon workbench?
    In step 2, how can you find the Product subgrid?
    Related entity subgrids do not appear for me.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s