You Want That Form To Do What?

What is a quick and ‘easy’ way to make a form come alive?  You guessed it; JavaScript, and if you’ve been working with Dynamics CRM for more than a few days, you’ve probably had many ‘scripting’ type requests. 
 

Although writing any type of code is not one of my strengths, I have managed to create some JavaScript for CRM forms through the help of the community.   I love the Microsoft Dynamics Community, there are so many terrific people willing to jump in and help out, post solutions, answer questions, etc., and the resources seem almost endless:

…… just to mention a few.

Ok, so back to me and JavaScript.  I like to think of JavaScript as my friend, however, we are probably more like distant acquaintances who I visit infrequently.  So, if you are like me and enjoy the ability to occasionally spice up CRM forms with a little scripting, I’ll share a tip that I use.

First and foremost, the Dynamics CRM developer community is very active and generally more than happy to share code so I leverage it with great appreciation.  A couple of my frequently visited web sites include:

I can generally find just about anything JavaScript related that I need from these two sites.  Here is an example of how I approached a recent “script” request.  

I received a request to update the Title field of the Contract Line with the linked Product Name so the first thing I did was visit the “Working With All MS CRM DataTypes In JavaScript” web site and found a code snippet that fit what I needed.

Code Snippet

The following code example shows how to use a Customer field type.

var customer = new Array();
customer = null;

// This gets the lookup for the attribute customerid on the Opportunity form.
customer = crmForm.all.customerid.DataValue;

// If there is data in the field, show it in a series of alerts.
if (customer[0] != null)
{
   // The name of the customer.
   alert(customer[0].name);

   // The GUID of the customer.
   alert(customer [0].id);

   // The entity type name of the customer.
   alert(customer[0].typename);
}

Since the Customer is a linked entity to the Opportunity and the code snippet shows how to obtain the “Name” value of the linked Customer, I knew with a few tweaks, I could make this code work to obtain the Product Name & then update the Title field of the Contract Line with that value.

Following is the modified script I used to update the Contract Line field

// set the value of the Contract Line title field = linked product name

product = crmForm.all.productid.DataValue;

if (product != null)

{

crmForm.all.title.DataValue = product[0].name;

}

I placed the code above in the OnChange event of the Title field and placed the following in the OnChange event of the Product lookup field.

  • crmForm.all.title.FireOnChange();

I also changed the Product field to required and updated the Dependency fields where I applied script.  Another recommendation is to change the order of the fields so that the Product field is one of the first fields that the user encounters when opening or creating a record.  This minimizes that chance that a user will unnecessarily enter a value in the field just to have it overwritten by the script. 

You can use the above example for auto-updating almost any entity “Title” type field in CRM like Opportunities, Cases, etc.  This is one option for gaining consistency for these field type values.  Since the Title / Topic type fields are required fields for many entities, and are often the primary field displayed in lookups, it can add value to have specified naming conventions that are automatically updated for the end user.  You can easily string values together to create a consistent & useful field value.     

For all you JavaScript & code experts, I know this is the long way around and I should really use JavaScript Libraries (maybe one day in the near future), but for those of you reading this post who infrequently write JavaScript or are just getting started and want to make a form come alive, I hope you find this helpful.

Technorati Tags: , , , , ,   , , , , , , ,, ,  , ,

Windows Live Tags: , , , , ,   , , , , , , ,, ,  , ,

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s