Changing Field Focus in Dynamics CRM 4.0 to Clean up the Invoice Product

Paul Nieuwelaar, 10 August 2010

In this blog post I will mainly be using two different pieces of JavaScript on the Invoice Product, to make the form more user friendly. The first piece of code is to change the field focus, and the second is to prefill the ‘Unit’ field, which I will be using from Nathan Eccles blog, called Populating Lookup Fields with JavaScript in CRM 4.0.

The first thing I will do is populate Unit, since we usually only have one unit called ‘Primary’ or ‘Primary Unit’. But to populate ‘Unit’, first you must enter a Product, so we can add the code from Nathans blog to the onChange of Product, so that after you select a product the unit will prefill.

The code to add to the Product onChange should look something like this:

if (crmForm.all.productid.DataValue != null) {

    if (crmForm.all.uomid.DataValue == null) {

        crmForm.all.uomid_ledit.focus();

        crmForm.all.uomid_ledit.value = "Primary Unit";

    }

}

Where ‘productid’ is the Products attribute name, ‘uomid’ is the Units attribute name, and ‘Primary Unit’ is the name of the Unit you are prefilling.

After selecting a Product and Unit, you would usually enter a Quantity next, so what we can do is add focus to the Quantity field when a Unit is selected. To do this, add the following code into the Unit onChange event:

if (crmForm.all.uomid.DataValue != null) {

    crmForm.all.quantity.SetFocus();

}

That code should work unchanged, but just in case note ‘uomid’ is the Unit attribute name, and ‘quantity’ is the Quantity attribute name.

The next thing I would like to do, is that when you select ‘Existing’ from the Select Product radio button, it adds focus to the Product lookup, and when you select Write In, it adds focus to the Product Description text box. To do this you will need to add the following code to the Select Product onChange:

if (crmForm.all.isproductoverridden.DataValue == true) {

    crmForm.all.ispriceoverridden.DataValue = true;

    crmForm.all.productdescription.SetFocus();

}

 

else {

    crmForm.all.ispriceoverridden.DataValue = false;

    crmForm.all.productid.SetFocus();

}

Again, this should work unchanged, ‘isproductoverridden’ is the Select Product radio button, ‘product description’ is the Product Description field, and ‘productid’ is the Product lookup.

Now, just add one final line of code to the forms onLoad event, to fire the onChange event of the Select Product radio button, so that when a new Invoice Product is created, the focus will be added to the first field you would want to enter into, be it the Product Description field, or the Product lookup. To do this, add the following line of code to the Forms onLoad:

crmForm.all.isproductoverridden.FireOnChange();

Publish the changes, and test that it all works as expected.

To recap on what the code should do:

It should add focus to the Product Description field or the Product lookup depending on which option is selected from the Select Product radio button when the form is loaded, and when the radio button is changed. Then when you select a Product, the Unit should prefill, and then focus should be given to the Quantity field.

Most of the code should work unchanged, apart from maybe the “Primary Unit” text that gets prefilled into Unit, the rest should work by default, as long as they are put in the right places.