web
You’re offline. This is a read only version of the page.
close
Skip to main content
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)
  • Bahalzamon Profile Picture
    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
    1,532 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
    Community Member Profile Picture
    on at
    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.

  • Community Member Profile Picture
    on at
    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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Aric Levin - MVP Profile Picture

Aric Levin - MVP 2 Moderator

#2
MA-04060624-0 Profile Picture

MA-04060624-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans