Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Xrm.Page.getControl() returns null

(0) ShareShare
ReportReport
Posted on by

Using CRM 2015.

I'm creating a button that loops over the selected records in a view and performs a custom method.

Here's the setup:

Created a button on the main view menu bar in one of our custom entities.

Added javascript for the new button to call.

 try {
 var selectedEntityReferences = [];

 var selectedRows = Xrm.Page.getControl("<control name here>").getGrid().getSelectedRows();
 alert("selectedRows:" + selectedRows.getLength());

 selectedRows.forEach(function (selectedRow, i) {

 selectedEntityReferences.push(selectedRow.getData().getEntity().getEntityReference());
 alert(selectedRow.getData().getEntity().id);

 });
 } catch (ee) {
  alert(ee.message);
 }

I tried replacing "<control name here>" with what I thought the name of the control was - but it gives 'selectedRows is null' error.

This sent me on a search to find the proper control name.

In the MSD documentation I found 'Xrm.Page.getControl()' should return a list of controls - but when I use this method it returns a null object.

This leads me to believe there are no controls on the page - but how is this possible?

Also - where would I find the proper name for the control that holds the view grid?

*This post is locked for comments

  • Bahalzamon Profile Picture
    Bahalzamon on at
    RE: Xrm.Page.getControl() returns null

    Bit old but running Xrm.Page.getControl() in DevTools returned everything on the page and i went through there and found my field named "header_new_fieldname" and what I am assuming happened is when I first created the form I put it on the header and now its in the form but it kept the header tag some how.

    Updating my code to call the header worked. Or i could remove and re-add it or maybe even in classic there would be something to update its name.

  • rthompson Profile Picture
    rthompson 1,530 on at
    RE: Xrm.Page.getControl() returns null

    Hi,

    I have run into this same issue using typescript.

    Could you please show the example on how you fixed the problem.

    Thanks

  • Verified answer
    RE: Xrm.Page.getControl() returns null

    I found the answer to this. 

    In this case - I am not looking at a sub-grid.  When looking at a view - you just need to pass  the MSD Parameter 'SelectedControlSelectedItemIds' to your function.  This will give the GUID of the selected items in the view.

  • RE: Xrm.Page.getControl() returns null

    Hi CRM Novice,

    to get the right control name you need to enter form editor the double click on grid control and get name value.

    Please let me know if you solve.

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

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,401 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans