Thursday, October 8, 2009

Make your code on forms adaptable for colleagues ...(And your self)

A lot of samples on scripting is found when you google (or bing) for a specific function. When implementing scripts other fellow developers has written might be hard for other colleagues to adapt and find the full function for, unless you comment and structure your code. Lots of MVP's has samples like this one from Donna Edwards, (see blog here). People like Donna is truely invaluable to the Microsoft Dynamics CRM communities and i have the deepest respect for the effort and time they put in sharing experiences and advising fellow colleagues.

Well, when I build up a set of functionality to an entity, the scripts on form are gathered in the OnLoad event of the form. By declaring the functions here, I get the full picture of all the functions on the form. And I might even reuse some bits and parts and have less code by using parameters when I call the functions.

The sample from Donna Edwards is pretty straight forward – Calculations of fields on the form. Calculations are done from the OnChange events on the form fields.

I would like to rewrite the code like this:

  • Create the two custom attributes that you need by going to Settings, Customization, Customize Entities, open the Opportunity Entity, select Attributes, create two new money attributes, and save the changes. Just like Donna Edwards.

  • Open the Opportunity form and add the two fields to the form.
    Double-click (Open) the Est. Booking field (estimatedvalue) on the form and add the following line of script to the OnChange event of the field:
    CalculateEstimatedValue();

  • Open the Total_Discount field on the form and add the same script to the OnChange event of the field:
    CalculateEstimatedValue();

  • Open the Total_Estimated_Value field on the form and add the following script to the OnChange event of the field:
    CalculateEstimatedValue();

  • Open the OnLoad event of the form and add the following script:
    /*BEGIN Calculate totalestimated value from fields
    EstimatedValue and TotalDiscount OnChange events
    Author: KEYZONE\bjj
    Date: 09102009*/
    CalculateEstimatedValue = function()
    {
    var _estimatedvalue = crmForm.all.estimatedvalue.DataValue ;
    var _totaldiscount = crmForm.all.Total_Discount.DataValue ;
    CrmForm.all.Total_Estimated_Value.DataValue = _estimatedvalue - _totaldiscount ;
    }
    //END Calculate totalestimated value


So here we get all relevant functions located in 1 place: The Onload Event of the form.
And can easily look through the functions and make changes if we need to.
Samples is simple and need some validation of the values for the fields.

NB! If you need a function from the OnLoad event to be run OnLoad (!) you would have to place the function itself before the call to the function.

TIP: To look through all fields trying to find calls from OnChange events is a hazel if you have a Form with many fields on it. You could download the customizations in a file and look through code for each form searching for “[CDATA”. But a tool to sort of document the scripts on forms in excel, can be found at
this link. And that saves me a lot of time, when I want to document customizations or simple need an overview.

For more complex functions that includes webservicecalls it comes in handy, as the webservice call is a lot of lines whether you are retrieving, updating, deleting or creating records, due to the query you have to build up and the iteration through the nodes in the response you get as result from the query.
I will bring some future posts on this issue.
Setting up parameters in the code makes it easy to reuse 1 function for all webservice calls on a specific form.

AGAIN if we want to have a global script because we use the same code on many entities, Adi Katz and others have described how to achieve that. Fully unsupported but very practical.