CRM 2011 Sub-Grid: Call JavaScript On-Click or On-Focus - dynamics-crm-2011

How can JavaScript execute when a sub-grid receives focus?
I have a sub-grid that shows all Cases associated with an Account.
I would like to disable some fields on the form when the sub-grid receives focus.
RibbonDiffXML action for a ribbon button:
<Actions>
<JavaScriptFunction Library="$webresource:mda_convertemailtocaselib" FunctionName="ConvertEmailToCaseLib.addToCase">
<CrmParameter Value="SelectedControlSelectedItemIds" />
</JavaScriptFunction>
</Actions>
This passes the sub-grid select items to a ribbon button, but I would like the JavaScript to execute before the ribbon button is pressed.

I haven't tried either of these out, but hopefully one of them should work for you.
Although it is a subgrid, there still is a control on the form. Unfortunately there is no "supported" way to know when a control gets focus since there aren't any OnFocus/OnBlur methods exposed via the CRM JS API. However you should be able to add your own JS event handler for that control and disabled the fields via the API Xrm.Page.getControl("fieldname").setDisabled(true)
I'm not 100% sure if this way would work, but it would be pretty sweet. Create a CustomRule EnableRule. In this CustomRule you call your own JS. In this create a function where you disable the fields and then return true to make the button enabled. It would look something like this:
function disableFieldsEnableRule() {
Xrm.Page.getControl().setDisabled(true);
...
...
return true; // so the button is enabled
}
The only reason I'm not sure if this will work is because I'm not sure if the Enable rules are checked each time you click on the subgrid or just the first time (I think it should be every time). Also I'm not sure if you want those fields re-enabled once you click off. If you do you might have to do something similar to this with a button on the native form, or something else.

Related

How can I add a bit of custom functionality to every CRM 2011 grid ribbon?

I have a small bit of custom functionality which I need to run from each and every grid ribbon in CRM 2011. The functionality is in a JS web-resource and I can attach it to a button enable rule and it all works fine.
The issue is that I need this to run on every grid in CRM, but I'm struggling to find something in the ribbon which appears on every screen. I did try attaching it to one of the buttons in the Jewel menu, but this only fires if the user clicks on the "File" tab.
Any thoughts...?
The jewel menu button is a good idea.
Add an anonymous JavaScript function to the library you are adding to the jewel button.
This will run when the library is loaded, just after the page loads:
var whatever = 30; // maybe you do not need a parameter
(function (what) {
/* Do what you need to do */
}(whatever));
If it’s on premise I‘d inject my script to the ribbon.js or global.js or some other js and ping from there.
Another option, less intrusive , is to use a resource that runs in each and every form i.e. myframewrok.js. This resource can check for the opener and inject the script to the opener dynamically.
A third options might be to ping a web service using a plug-in registered on execute or retrieve multiple messages which fire repeatedly for any grid in the system.
In the end we attached it to the "Advanced Find" button as this appeared on every ribbon that we needed the functionality on. The Jewel menu did not seem to work for us as it only fired when the user actually clicked the "File" button.

How can I know what form control had focus when ribbon button pressed?

Have a custom JS function that gets called when a ribbon button is pressed in the context of a form. In my custom JS function I need to know what form field had the focus just prior to the ribbon button being pressed. I've tried 2 ways (below) without success. Is there any way to do this reliably?
Way #1
According to this, I can get the control I want passed as a parameter to my JS. I've tried using both PrimaryControlId and PrimaryControl parameters.
<JavaScriptFunction FunctionName="OnCustomBtnFunc"
Library="$webresource:myJSfile.js">
<CrmParameter Value="PrimaryControlId" />
<CrmParameter Value="PrimaryControl" />
</JavaScriptFunction>
For both, I get an object passed to OnCustomBtnFunc() but it does not seem to enable me to determine which form control had the focus prior to the ribbon button being pressed.
Way #2
I call Xrm.Page.ui.getCurrentControl(). This works for form fields of some types but not others e.g. if it is a string field it works but I get null for a lookup.
you won't like this answer, but you can use JQuery to quickly grab every Control in your Form and assign it an OnBlur event. This event can either assign the ID or the control itself to global variable, which you will make accessible to your ribbon (which fires from a different scope).
People do not like this approach because of the different context scopes of the variables and because it involves playing with "unsupported" features of plain HTML. However, if you ask only the question "how" and not "should we", then this is an easy way for "how" you would do this.

JSF: Create a "dummy" button which does nothing?

In our JSF web application, we have an input field where the user can enter a numeric ID, which is then looked up by the app. To help the user, the lookup is bound to "onchange", thus it will be triggered as soon as the user tabs out of the field or clicks elsewhere.
So, user enters "123", presses tab (or clicks), lookup runs. This works fine; however, for usability reasons, we also want to provide a button that users can click on, for users who will otherwise wonder "where should I click to trigger a lookup?". To do this, we'd like to provide something that looks and feels like a HTML / JSF button, but does nothing (as the click will trigger the "onchange" event anyway).
Is there a way to make a JSF button that does nothing? I tried using h:commandButton without the "action" attribute, but it still fires a request.
p:commandButton type="button" will just provide a push button.
Since you tagged this question also as a usability issue, I would advise against a button in the first place if the onchange already triggers the lookup.
From a user's perspective it is confusing whether or not clicking the button is mandatory. After they have entered the field and skipped to the next, they see the lookup occur without clicking the button. If there is a button they will assume it's there for a reason.
The option that I favour in these cases is a onkeypress handler with a timeout of half a second, after which the value is looked up.

Modifying sharepoint edit dialog

I have successfully created a feature in sharepoint that modifies the existing edit dialog and adds a custom button to it like this.
and I am aware that I can pass back data when the user clicks the custom button like this.
<CommandUIHandlers>
<CommandUIHandler Command="ActivateUser" CommandAction="/_layouts/MyFeature/MakeUserActive.aspx?ListID={ListId}&ItemID={ItemId}&ItemUrl={ItemUrl}&ListUrlDir={ListUrlDir}" />
</CommandUIHandlers>
As detailed here
I can now handle the list item and perform my required actions on it BUT given that this button has been added in the modify context (IE: Inside the sharepoint edit item dialog) what if you want to save changes to the data itself?
To me it seems like using your custom button would always mean losing any changes the user has made to the data. Is there a way around this?
Good question!
You actually already linked to the solution: Right now you are simply redirecting the user by using a URL as your CommandAction: CommandAction="/_layouts/MyFeature/MakeUserActive.aspx?ListID={ListId}&ItemID={ItemId}&ItemUrl={ItemUrl}&ListUrlDir={ListUrlDir}"
This if course redirects the user to another page without saving the current entry. What you want to do is use Javascript as linked in the MSDN article:
CommandAction="javascript:alert('here be dragons');"
You can either work the the SharePoint Javascript object model here and use something like SP.ListOperation.Selection.getSelectedItems(); or you could use complete custom code.
From your aspx page name I can see you want to "make a use active" (btw: wouldn't "ActivateUser.aspx" be nicer?). If this simply means setting a property in another list you could do that with the SharePoint OM, if it is some custom stuff you would need a webservice which you can call from JavaScript and "activate the user" like that. You can of course always access the current form and pass on the values the user entered. Or you could create a custom save button which does some stuff (activate user) before saving.
Lastly: You can also have postbacks in your custom button where you could do anything you'd like.

moving from javascript window.open to Rich:ModalPanel

In my web application currently there are many pop up windows made by various JS function which i would like to replace with Rich:ModalPanel (I'm using Myfaces 2.0.12 and RichFaces 3.3.3). Below is a typical example i like to replace:
window.open("<%=basePath1%>jsp/custhistory.faces?userid="+pk);
where pk is the value retried from a hidden input such as <h:inputHidden id="userPk" value="#{1234}"/> inside the javascript function and then added to the end of the url above.
Looking at few RichFaces ModalPanel examples (and demos) i can't figure out how i can make the above work using ModalPanel. Can someone please provide an example or a link to a resource o
I have been using Richfaces 4, but the same theory should apply.
The modal panel is essentially a div that can be shown or hidden, so you could add a <ui:include> in there if you specifically need (or want) the panel to be a seperate file.
The modal panel is rendered as part of your page, but is styled as display:none I believe. If this is triggered by a client action you can call the Richfaces API to show the panel on command.
For instance instead of a command(Link|Button) triggering Richfaces.$('elName').show() it could trigger your js function which sets and values/params your modal panel needs and then call .show() itself.
Note: IE and firefox had a truly modal popup. This is not truly modal and javascript will process in the background, and if you do not give focus to the popup panel if the user types it will interact with the application in the background.
I had used a modal js popup to prompt a user with a yes/no question int his type of idiom
if(askUser("some question?")=='yes'){
//some code to do if yes
}else{
//some code to do if no
}
Depending on how generic you want this popup, that is not really possible without using an event that is fired and defining an event handler for that event to handle the rest of the function.

Resources