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 :
Customer experience | Sales, Customer Insights,...
Answered

Ribbon Workbench Display Rule with JavaScript

(0) ShareShare
ReportReport
Posted on by 5

Hello. Since xrm.page has been depricated I try to move over to using ExecutionContext instead but I run into problem.

In the display rule I have added a crm parameter with primaryControl

The javascript fails on:  var formContext = parentControl.getFormContext();   

And the beginning of the javascript looks like:

function IsCurrentUserOwner(parentControl)

{

  var formContext = parentControl.getFormContext();   

 

  var owner= formContext.getAttribute("ownerid").getValue();

  var ownerID = owner[0].id;

  var userId = Xrm.Page.context.getUserId();

  if (ownerID == userId ) {

     return true;

  } else { 

                return false;

  }

 

}

 

 Can someone point out what I have missed?

I have the same question (0)
  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hello ,

    You should use Enable rule , as far as  know display rule does not support Client Side script.. Check here how to pass primary control-

    www.magnetismsolutions.com/.../getting-dynamics-365-formcontext-from-ribbon-workbench

  • MangeH73 Profile Picture
    5 on at

    Sorry. I mean that I have a Enable Rule,  not a display rule.

  • MangeH73 Profile Picture
    5 on at

    Sorry I menat that I have a  enable rule.  It looks like the link you have .    But the row  var formContext = parentControl.getFormContext();     fails all the time.

  • Verified answer
    ajyendra Profile Picture
    1,738 on at

    Hi,

    Now you can get formContext from this parameter as easily as if it were the executionContext.

    If you are calling this for a button on the Home or SubGrid ribbons, then your code will look like this:

    function run(primaryControl) 
    { 
    var formContext = primaryControl.getFormContext(); // get formContext 
    
    var firstName = formContext.getAttribute("firstname").getValue();  
    var lastName = formContext.getAttribute("lastname").getValue(); 
    console.log(firstName + " " + lastName); 
    }
    

    And if you are calling this for a button on the Form ribbon, you can use primaryControl as if it were the formContext:

    function run(primaryControl) 
    { 
    var formContext = primaryControl; // rename as formContext 
    
    var firstName = formContext.getAttribute("firstname").getValue();  
    var lastName = formContext.getAttribute("lastname").getValue(); 
    console.log(firstName + " " + lastName); 
    } 

    You can see how it work Check below link
    https://www.magnetismsolutions.com/blog/alfwynjordan/2019/10/02/getting-dynamics-365-formcontext-from-ribbon-workbench
  • Suggested answer
    Naveen Ganeshe Profile Picture
    3,397 User Group Leader on at

    Hello,

    To pass in the execution context (form context or grid context) information to JavaScript function for your ribbon actions, specify PrimaryControl for the form context, or SelectedControl for the grid context as the <CrmParameter> value in your ribbon definition. SelectedControl will pass in the grid context, for both subgrids and homepage grids. The passed in PrimaryControl or the SelectedControl value is used as an argument in your JavaScript function for form context or grid context respectively.

    So you do not need to get form context from primary control object. PrimaryControl itself is a formContext

    All you need to do is the below:

    var formContext = parentControl;  

  • MangeH73 Profile Picture
    5 on at

    3583.CRM.png

    THis is how it looks in ribbon workbench.,

    If I have:


    function IsCurrentUserOwner(primaryControl)
    {
    alert("1");
    var formContext = primaryControl;
    alert("2");
    var owner= formContext.getAttribute("ownerid").getValue();
    alert("3");
    var ownerID = owner[0].id;

    var userId = formContext.context.getUserId();

    if (ownerID == userId ) {
    return true;
    } else {
    return false;
    }

    }

    it does an alert("2"); but not an alert("3);

    If I have

    function IsCurrentUserOwner(primaryControl)
    {
    alert("1");
    var formContext = primaryControl;
    alert("2");
    var owner= formContext.getAttribute("ownerid").getValue();
    alert("3");
    var ownerID = owner[0].id;

    var userId = formContext.context.getUserId();

    if (ownerID == userId ) {
    return true;
    } else {
    return false;
    }

    }

    It does an alert("1") but never an alert("2");


    An alert(primaryControl) shows undefined So I guess that javascript is not the main source of the problem.

  • MangeH73 Profile Picture
    5 on at

    I removed the parameter in Ribbon workbench and added it back, and suddenly I started to get the parameter to not be undefined.  

  • Verified answer
    ajyendra Profile Picture
    1,738 on at

    Might be not published Properly . That might be issue because same configuration I set in Ribbon Workbench and Webresoucre.

    Boom.. IT works in one go.

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 137 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 57

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans