web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
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

I have the same question (0)
  • Community Member Profile Picture
    on at

    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

  • Verified answer
    Community Member Profile Picture
    on at

    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.

  • rthompson Profile Picture
    1,532 on at

    Hi,

    I have run into this same issue using typescript.

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

    Thanks

  • Bahalzamon Profile Picture
    2 on at

    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.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans