3

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.

Guido Preite
  • 14,905
  • 4
  • 36
  • 65
keerz
  • 717
  • 1
  • 6
  • 21
  • Would it be possible to have `onstatuschanged` or whatever it's called, connected to every field so that when your user clicks on the ribbon, the previous field looses focus and fires the event? Then, you can store THAT element's id. Haven't tried that - just speculating. – Konrad Viltersten Nov 18 '12 at 19:45
  • @Konrad tried that. problem is the "onchange" event (assume this is the one you mean as its the only OOTB event for form fields) is not called when the ribbon button is pressed!. The focus stays in the field. I can change focus to an arbitrary field so the onchange event of the current field does fire. problem then is if the field validation detects an invalid value, i want the ribbon button code to be aware that this happened. I can try doing it setting flags etc etc but it's all very kludgy! – keerz Nov 19 '12 at 01:56
  • Sorry buddy - that's all I've got. If you've tried that already, I'll be of no further use to you. Maybe you could register a control's id upon the validation fault or something like that. Do tell how you did, because next month I'll be doing some GUI work myself, probably weeping over the same issues. Also, just to be clear - you'll be coding it in JavaScript => it **will** be kludgy. :) – Konrad Viltersten Nov 19 '12 at 05:31
  • @Konrad haha, ok. will update if I come across anything useful – keerz Nov 19 '12 at 14:44
  • 1
    @KonradViltersten apologies for delay in responding. have moved on to other work. didn't find a supported solution for what I needed but was able to make it work with some JS kludge. – keerz Nov 28 '12 at 07:46

1 Answers1

0

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.

Mike_Matthews_II
  • 722
  • 6
  • 15