Answered by:
MS CRM 2011 Javascript - subgrid does not get focused

Question
-
Hi,
I am trying to make the subgrid for Activities to get focused when clicking on the tab for activities. I use the code below.
But the subgrid does not get focused. Why does this not work? I thought the method setFocus() would work on all types of controls.
Xrm.Page.ui.controls.get("Activities").setFocus();
Wednesday, November 30, 2011 2:06 PM
Answers
-
Why are you using an iFrame and not a Grid? The code supplied here works with the grid and sets focus to the grid when the tab is expanded. If you use an iFrame to display the associated entity I don't think that you'll get the cooresponding ribbon at the top - I can't say for sure because I haven't tested it. I have a N:N relationship between users and contacts and I was able to add a grid to the form for this relationship. In upgrading from 4.0 to 2011, I have been able to change all of my iFrames to Grid for all associated entities.
Christina Bayley | CRM Administrator
- Marked as answer by Andrii ButenkoMVP, Moderator Thursday, July 12, 2012 1:17 PM
Friday, July 6, 2012 12:52 PM
All replies
-
Rosanna -
I tested this and found that it does not appear to work for the subgrid control.
We documented that it works for all controls except those that are not visible, disabled, or can't accept focus. (see http://msdn.microsoft.com/en-us/library/gg334266.aspx#BKMK_setFocus)
Looks like this is either a bug or a problem with the documentation. I've asked some devs to review what the behavior is supposed to be. Unfortunately, it is probably a documentation bug. Once that is confirmed I'll fix it in the documentation.
In the meantime, please try using the setFocus method on the tab containing the subgrid.
For example, on the default account form, you can set focus on the Contacts tab using Xrm.Page.ui.tabs.get("contacts").setFocus().
Jim Daly Technical Writer Microsoft Dynamics CRM- Edited by Jim Daly [MSFT]Microsoft employee, Editor Monday, December 5, 2011 10:59 PM
Monday, December 5, 2011 10:55 PMAnswerer -
Has this been resolved? We are currently working on upgrading from CRM 4.0 to CRM 2011 and I need to set focus on the sub-grid when the tab is expanded (to get the toolbar). The tab is collapsed by default.
In 4.0, we used iFrames to show what are now called sub-grids and the toolbar came in with the information. I know that all a user has to do is click the grid to get the toolbar to change, but if it was 'easy' functionality before and now it's not so easy, there's going to be a lot of complaining. I want to use the sub-grid functionality and get away from iFrames and pulling the information in through code, so I hope that there's a resolution for this.
Wednesday, February 22, 2012 8:17 PM -
Hi Christina,
When you say "the toolbar came in with the information", are you referring to the form ribbon changing? Unfortunately, I don't think we support SetFocus() for subgrids, but there might be some other way to achieve what you're looking for. You could take a look at http://crmbusiness.wordpress.com/2011/05/19/crm-2011-javascript-and-subgrids-code-example/, for example - I can't say whether that code is "officially sanctioned" so to speak, but it might give you some options.
You can also open a support incident (If you go to the Resource Center in CRM there is a section for Support - "Technical Support Request") if you feel this functionality is badly needed.
Thanks!
David
Wednesday, February 22, 2012 10:04 PM -
There was no form ribbon in CRM 4.0. In 4.0, when we used iFrame to show associated views from other entities (such as Contacts on the Account screen), the toolbar would appear in the iFrame above the list of contacts.
In upgrading from 4.0 to 2011, I really don't want to take the toolbar away from the users. I realize that they only have to make one extra click to select the sub-grid and get the ribbon at the top for Contacts, but they one extra click is going to cause A LOT of phone calls and complaining.
Thursday, February 23, 2012 6:41 PM -
I see. So you want a form which only contains a subgrid, and which has the subgrid ribbon automatically open when the form opens?
This could be a little challenging, since subgrids load asynchronously so the onLoad event can't always capture them. Again, your best bet may be to go with the link I shared - I think that deals with polling for the subgrid to be loaded and also with interacting with it. Again, the code there isn't official, so I can't guarantee that it will work after future updates, but at least it's another option to consider.
Thursday, February 23, 2012 10:30 PM -
Unfortunately the link that you gave me doesn't help me.
I guess I wasn't 100% clear in what I was trying to explain so I will try again:
My account form has several tabs, sections, and fields (as it should). I have a tab called 'Contacts' that is collapsed (by default) when the account form is loaded. Within the 'Contacts' tab I have one section and that section contains a Sub-grid showing the Contacts for the Account. When a user expands the 'Contacts' tab, I want the focus to be set on the sub-grid so that the ribbon appears at the top of the screen for contacts.
In CRM 4.0, we had similar functionality. Through code, we created a collapsed section called 'Contacts' and within that section we had an iFrame. Through code, the iFrame was programmed to pull in the contacts for the account. In CRM 4.0, the toolbar for the Contacts would appear within the iFrame above the contact list (create new contact, add existing contact, delete, export to excel, etc). This was just the way it functioned for us.
Back to CRM 2011....with the 'out-of-the-box' functionality, what WAS a section in CRM 4.0 is now a tab (called Contacts) and what WAS an iFrame pointing to an associated contact view in CRM 4.0 is now a Sub-grid. I understand that I cannot add the toolbar within the sub-grid in CRM 2011 since it is now a 'ribbon' and I understand that. Based on the website (http://msdn.microsoft.com/en-us/library/gg334266.aspx) in Jim Daly's post, it shows that setFocus() can be used on 'Control Types: ALL'. According to Jim's post from December 2011, he wasn't sure if it was a bug or a documentation issue. He was going to look into it and if it was a documentation issue, he was going to have it fixed. Well, it's almost 3 months later and the documentation hasn't been fixed so I was asking if this issue has been addressed yet and if so, what is the resolution. Is it a bug that will be addressed in a future rollup or is it a documentation issue?
Wednesday, February 29, 2012 6:30 PM -
Christina -
I created a bug for this issue on 12/5. I'm still waiting for a response. I'll ping the dev team again to see why this issue didn't get addressed. (along with a link to this thread)
Jim Daly Technical Writer Microsoft Dynamics CRM
Wednesday, February 29, 2012 8:34 PMAnswerer -
UPDATED INFORMATION HERE: http://prodynamicscrm.com/blog/using-javascript-set-focus-subgrid-control
This JavaScript function, focusGrid, that I wrote should do the trick for you. Ensure that gridName matches the name you gave the SubGrid in the Form Designer.
Also, if you are doing this on the OnLoad event for the form, you must include timed delay since the SubGrid is loaded asynchronously. Below is an example of using the focusGrid function on the form's OnLoad event.
function onLoad() { try { setTimeout('focusGrid("Activities")', 300); } catch (err) { // alert('OnLoad exception: ' + err.Message); setTimeout("onLoad()", 300); } } function focusGrid(gridName) { try { document.getElementById(gridName).getElementsByTagName('a')[0].focus(); } catch (err) { // alert('focusGrid exception: ' + err.Message()); throw (err.Message()); } }
- Proposed as answer by RyanCorrigal Wednesday, March 7, 2012 7:54 PM
- Edited by RyanCorrigal Wednesday, March 7, 2012 9:29 PM Added additional information
Wednesday, March 7, 2012 7:53 PM -
Thank you, thank you, thank you Ryan!!! I made some changes to what you gave me as I was adding it to the tab state change. The sub-grid is in a tab by itself and the tab is collasped by default. My function looks like this:
function gFocusGrid(tabName, gridName)
{
var tabState = Xrm.Page.ui.tabs.get(tabName).getDisplayState();
if (tabState == 'expanded')
{
try
{
document.getElementById(gridName).getElementsByTagName('a')[0].focus();
}
catch (err)
{
throw (err.Message());
}
}
}And then I call gFocusGrid in the onStateChange event on the tab and pass my tabName and gridName like this: gFocusGrid('contacts', 'accountContactsGrid')
I didn't seem to have an issue with the sub-grid not loading and needing to use the code for the timeout. If I run into a problem, I will certainly keep the timeout in mind and just add it to my function.
IT WORKS PERFECTLY!!! This is EXACTLY what I needed. THANK YOU SO MUCH.
Friday, March 9, 2012 2:50 PM -
i have problems with ribbon:
i've created entity and relation N to N to systemuser
on the myEntity form (tab_6) i've added an iframe : iframe_all_users
onload of myEntity form i set dynamicaly url for the iframe
all works fine , the problem is: i can't see the ribbon of the users - so i only can see users,and i can't add or remove them
by using this:
function gFocusGrid(tabName, gridName)
{
var tabState = Xrm.Page.ui.tabs.get(tabName).getDisplayState();
if (tabState == 'expanded')
{
try
{
document.getElementById(gridName).getElementsByTagName('a')[0].focus();
}
catch (err)
{
throw (err.Message());
}
}
}document.getElementById(gridName).tagName = IFRAME
document.getElementById(gridName).readyState = complete
BUT
document.getElementById(gridName).getElementsByTagName('a') => count=0
Thursday, July 5, 2012 11:02 AM -
Why are you using an iFrame and not a Grid? The code supplied here works with the grid and sets focus to the grid when the tab is expanded. If you use an iFrame to display the associated entity I don't think that you'll get the cooresponding ribbon at the top - I can't say for sure because I haven't tested it. I have a N:N relationship between users and contacts and I was able to add a grid to the form for this relationship. In upgrading from 4.0 to 2011, I have been able to change all of my iFrames to Grid for all associated entities.
Christina Bayley | CRM Administrator
- Marked as answer by Andrii ButenkoMVP, Moderator Thursday, July 12, 2012 1:17 PM
Friday, July 6, 2012 12:52 PM -
Why are you using an iFrame and not a Grid? The code supplied here works with the grid and sets focus to the grid when the tab is expanded. If you use an iFrame to display the associated entity I don't think that you'll get the cooresponding ribbon at the top - I can't say for sure because I haven't tested it. I have a N:N relationship between users and contacts and I was able to add a grid to the form for this relationship. In upgrading from 4.0 to 2011, I have been able to change all of my iFrames to Grid for all associated entities.
Christina Bayley | CRM Administrator
Thanks!
Sub-Grid solved my problem.
Sunday, July 8, 2012 5:53 AM