Access Microsoft Dynamics CRM Quick View Form Attributes Using JavaScript

Colin Maitland, 16 November 2014

I recently wanted to use a Quick View form on a Main form to access attribute values from the related parent entity instead of using an asynchronous RetrieveMultiple statement. In this blog I will demonstrate how to do this using an example of accessing attribute values that are then used to filter Lookups.

Some problems using the asynchronous RetrieveMultiple statement to retrieve the parent entity attributes compared with retrieving the attributes from a Quick View form are:

•  A Retrieve or RetrieveMultiple statement has to be executed and this takes more time.

•  More programming is required.

•  The programming required is more complex.

In my example I have the following structure: Events, Event Registration Batches, Event Registration Groups, Registrations, Payments and Payment Allocations.

Access Microsoft Dynamics CRM Quick View Form Attributes Using JavaScript

•  Registrations must be related to an Event, must be related to an Event Registration Batch and may optionally be related to an Event Registration Group. These values are set when the Registration is created.

•  Payments, of Type Registration, must be related to an Event, may optionally be related to an Event Registration Batch and may optionally be related to an Event Registration Group. These values are set when the Payment is created. If the Payment is created from a Registration then these values are automatically mapped.

•  Payment Allocations can be added from the Payment, from the Registration or from the Payment Allocations area on the Site Map.

•  If creating a Payment Allocation by populating or selecting the Payment and then the Registration, the Registrations available to be searched, using the Registration Lookup on the Payment Allocation form, and selected must be filtered so that that they are for the same Event and, if provided on the Payment, for the same Event Registration Batch and Event Registration Group.

•  If creating a Payment Allocation by populating or selecting the Registration first and then the Payment, the Payments available to be searched and selected, using the Payment Lookup n the Payment Allocation form, must be filtered so that that they are for the same Event and, if provided on the Payment, also for the same Event Registration Batch and Event Registration Group.

The following image shows a Payment for an Event where the Event Registration Batch and the Event Registration Group have been selected, i.e. the Event is Pure Power 2014 Auckland, Event Registration Batch is 2 – Open and Event Registration Group is Smith Family. This image also highlights the Payment Allocations Subgrid on the Payment form.

Access Microsoft Dynamics CRM Quick View Form Attributes Using JavaScript

When we add Payment Allocations to this Payment we want to filter the Registration Lookup on the Payment Allocation form so that only Registrations for the same Event, Event Registration Batch and Event Registration Group as those selected on the Payment may be searched for and selected. The following two images demonstrate this:

Access Microsoft Dynamics CRM Quick View Form Attributes Using JavaScript

Access Microsoft Dynamics CRM Quick View Form Attributes Using JavaScript

The following describes how I have made the Payment attributes and the Registration attributes, used for programmatically filtering the Lookups on the Payment Allocation form, available on the Payment Allocation form and how these have been accessed using JavaScript. Microsoft also provides brief guidance under the heading .getAttribute().

In my example:

1.  I have created a Quick View form for the Payment entity that displays the Event, Event Registration Batch and Event Registration Group fields for the Payment.

Access Microsoft Dynamics CRM Quick View Form Attributes Using JavaScript

2.  I have created a Quick View form for the Registration entity that displays the Event, Event Registration Batch and Event Registration Group fields for the Registration.

 Access Microsoft Dynamics CRM Quick View Form Attributes Using JavaScript

3.  I have added both these Quick View forms to the Payment Allocation Main form. The name of the Quick View forms is important as this will be required by the JavaScript statement that accesses them. In this example they are named PaymentQuickViewForm and RegistrationQuickViewForm. The attributes they contain are named cpm_EventId, cpm_EventRegistrationBatchId and cpm_EventRegistrationGroupId.

4.  I have made the section on which the Quick View Forms are placed invisible to Users.

Access Microsoft Dynamics CRM Quick View Form Attributes Using JavaScript

5.  I have then added the following JavaScript:

•  Firstly a function on the Payment Allocation form’s OnLoad event to invoke a method to initialize the Lookup pre-search event handers.

Access Microsoft Dynamics CRM Quick View Form Attributes Using JavaScript

•  Then the function that initialises the Lookups pre-search event handlers. I have chosen add the pre-search event handler to the lookup only if it not already populated. If populated it will be programmatically set to read-only and so there is no need to add a pre-search event handler as it will never be used.

Access Microsoft Dynamics CRM Quick View Form Attributes Using JavaScript

•  Then in the preSearch event handler, such as the one used for the Registration lookup, named AddRegisterFilter I have the following statements which access the attributes on the Quick View form that are to be used for defining and adding the custom filter to the Lookup.

Access Microsoft Dynamics CRM Quick View Form Attributes Using JavaScript

The syntax is:

Xrm.Page.GetControl(“<QuickViewFormName>_<QuickViewFormName>_<EntityLogicalName>_<AttributeName>”).getAttribute();

E.g. Xrm.Page.getControl(“PaymentQuickViewForm_PaymentQuickViewForm_cpm_payment_cpm_eventid”).getAttribute();

Notice that the Quick View Form Name is repeated. In this example the Quick View Form Name is PaymentQuickViewForm, the Entity Logical Name is cpm_payment and the Attribute Name is cpm_eventid.

After retrieving the Attribute using .getAttribute() you can then retrieve it’s value using .getValue(). Don’t forget to include null checks where appropriate!

     •  For Lookups an Object is returned. You can then use [0].id to get the id, [0].entityType to get the entity logical name, and [0].name to get the name; e.g. eventId[0].id, eventId[0].entityType and eventId[0].name.

     •  For Option Sets the numeric Option Value is returned; e.g. 92308002.  You can then use .getText() to get the text Option Label or .getOptions() to get the Option Set options.

     •  For Currency and Number attributes the numeric value is returned; e.g. 100.

     •  For Date/Time attributes the Date and Time value is returned, e.g. Mon Nov 2 12:00:00 UTC+1300 2014.

     •  For Text attributes the text value is returned; e.g. John Smith.

     •  For Two Option attributes the boolean value is returned; i.e. true and false.

•  Finally, in my example the values retrieved from the Quick View form are used to defined and apply the customer filter to the Lookup as shown in this image. This code defines and adds the custom filter to the Lookup.

Access Microsoft Dynamics CRM Quick View Form Attributes Using JavaScript