Friday, February 25, 2011

Using generic functions – fieldnames- in CRM 2011

With the new webresources structure in Microsoft Dynamics CRM 2011, it has become a lot easier, or at least a lot more self evident, to use generic javascript functions instead of hard-coded scripts. Off course with CRM 3.0 and 4.0 one could write generic functions and call those functions when needed. But reality is that in a lot of situations fieldnames were hardcoded in the javascript.

For example, to add the value of Field A to Field B and set the result in Field C, both the onchange of Field A and Field B would contain the following javascript:

crmForm.all.new_fieldc.DataValue = crmForm.all.new_fielda.DataValue + crmForm.all.new_fieldb.DataValue;

With CRM 2011 it is not possible write the javascript directly on the onchange of the field, but you will have to call a function from a webresource.

Now let’s assume that you have an entity with 3 sets of 3 fields (2 source fields and 1 result field) on the form and you want to populate the 3 result fields with the result outcome of field 1 plus field 2. If you follow on the CRM 4.0 structure, the result would be a webresource with 3 similar javascript functions containing the fieldnames and the onchange events of the 6 source fields calling 1 of those 3 functions.

So how to do this in a CRM 2011 way using the ‘pass parameters to the function’? Start with creating a new javascript webresource and give it a name, in this example called ‘new_testscripts’. In this webresource, write a javascript function that contains 3 parameters. In this example is the function name ‘calcAddValues’ and the parameter names ‘sResultField’, ‘sFieldA’ and ‘sFieldB’, that represent the fieldnames to be used and write the logic as you would with hardcoded fieldnames like this:

function calcAddValues(sResultField,sFieldA,sFieldB)
{
if((Xrm.Page.getAttribute(sFieldA).getValue() != null)||(Xrm.Page.getAttribute(sFieldB).getValue() != null))
{
Xrm.Page.getAttribute(sResultField).setValue(Xrm.Page.getAttribute(sFieldA).getValue() + Xrm.Page.getAttribute(sFieldB).getValue());
}
else
{
Xrm.Page.getAttribute(sResultField).setValue(null);
}
}

Save and publish the webresource. Now you can use this function on any CRM form and call it as many times as you like. To do so, first add the webresource as a library to the form. When included, go to the properties of a field and set the OnChange event by selecting the library, set the name of the function and add the fieldnames to be used in the function as a comma separated list of parameters that will be passed to the function.

Here’s an example on how to do so:

Jscript_Parameters

Advantages

Using the new Microsoft Dynamics CRM 2011 structure creates the possibility to write generic javascript functions and have them collected in one place. Calling a function is easy, just using the function name and providing some parameters when necessary will suffice. No duplicated code is required. Maintenance and future adjustments will be a lot easier. Fixing one piece of javascript code will result in a change for every instance where the code is called.

The catch: make sure that if you change an existing function for one specific requirement that there’s no other part within your Microsoft Dynamics CRM solution that breaks.

1 comment:

  1. I tried this code and get the following error msg when I load the form.

    Error:the value of the property 'calcAddValues'is null or undefined. Not a function object.

    Below is your code that i changed. I'm sure the field names are correct.


    function calcAddValues(estimatedvalue,new_esthwrev,new_estswrev,new_consultingrev)

    {

    if((Xrm.Page.getAttribute(ew_esthwrev).getValue() != null)||

    (Xrm.Page.getAttribute(new_estswrev).getValue() != null))

    (Xrm.Page.getAttribute(new_consultingrev).getValue() != null))

    {

    Xrm.Page.getAttribute(estimatedvalue).setValue(Xrm.Page.getAttribute(new_esthwrev).getValue() + Xrm.Page.getAttribute(new_consultingrev).getValue() + Xrm.Page.getAttribute(new_estswrev).getValue());

    }

    else

    {

    Xrm.Page.getAttribute(estimatedvalue).setValue(null);
    }

    }

    ReplyDelete