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?

  • Verified answer
    ajyendra Profile Picture
    ajyendra 1,732 on at
    RE: Ribbon Workbench Display Rule with JavaScript

    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.

  • MangeH73 Profile Picture
    MangeH73 5 on at
    RE: Ribbon Workbench Display Rule with JavaScript

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

  • MangeH73 Profile Picture
    MangeH73 5 on at
    RE: Ribbon Workbench Display Rule with JavaScript

    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.

  • Suggested answer
    Naveen Ganeshe Profile Picture
    Naveen Ganeshe 3,393 User Group Leader on at
    RE: Ribbon Workbench Display Rule with JavaScript

    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;  

  • Verified answer
    ajyendra Profile Picture
    ajyendra 1,732 on at
    RE: Ribbon Workbench Display Rule with JavaScript

    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
  • MangeH73 Profile Picture
    MangeH73 5 on at
    RE: Ribbon Workbench Display Rule with JavaScript

    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.

  • MangeH73 Profile Picture
    MangeH73 5 on at
    RE: Ribbon Workbench Display Rule with JavaScript

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

  • Suggested answer
    gdas Profile Picture
    gdas 50,089 on at
    RE: Ribbon Workbench Display Rule with JavaScript

    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

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

Announcing Our 2025 Season 1 Super Users!

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

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,321 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans