Microsoft Dynamics CRM 2011 Scripting Cookbook

5 (1 reviews total)
By Nicolae Tarla
  • Instant online access to over 7,500+ books and videos
  • Constantly updated with 100+ new titles each month
  • Breadth and depth in over 1,000+ technologies
  1. Overview of Dynamics CRM 2011 Customization

About this book

With the increased adoption of Dynamics CRM 2011, more people are faced with various tasks including administering and customizing the environment. Microsoft Dynamics CRM is a Customer Relationship Management software package from Microsoft. It offers solutions to help companies with Sales, Customer Services, and Marketing. Microsoft Dynamics CRM is increasingly being used by businesses of all kinds and all sizes to reach audiences in new ways. Microsoft Dynamics CRM scripting extends system customization through the use of client-side scripting. It builds on the standard customization options offered by Dynamics CRM.

"Microsoft Dynamics CRM 2011 Scripting Cookbook" walks the reader through the process of customizing an environment, from the most basic topics such as working with specific fields and types, working with the forms, and then moves on to the more advanced topics of scripting and debugging your scripts, designing new form and ribbon elements, and using additional well known public scripting libraries, as well as integrating external data sources into your environment.

The first chapters of this book cover the basics of using the wizard-driven customization approach, packaging your customization into solutions, and adding basic scripts to interact with all the form elements. Further down the road we start introducing concepts around debugging your scripts, working with ribbon elements and navigation, taking advantage of other public scripting libraries and integrating them into your solutions, as well as light ways to bring social information in front of your users.

Later chapters will assume knowledge of some of the most basic customizations presented at the beginning of the book. After completing the recipes in "Microsoft Dynamics CRM 2011 Scripting Cookbook", you will have gained a new perspective on how far can you take the customization in Dynamics CRM. The additional details presented around using other public scripting libraries and integrating other data sources into your environment should serve as a start into investigating additional sources.

Publication date:
March 2013
Publisher
Packt
Pages
268
ISBN
9781849688826

 

Chapter 1. Overview of Dynamics CRM 2011 Customization

In this chapter, we will cover:

  • Opening a free 30-day trial of Dynamics CRM 2011 Online

  • Using solutions to package our work

  • Creating and managing entities

  • Creating and managing fields

  • Creating and managing forms

  • Creating and managing scripts

  • Creating and managing other resources

  • Creating and managing workflows

  • Creating and managing dialogs

  • Starting a workflow from a dialog

  • Working with security roles and permissions

 

Introduction


One of the most useful features of Dynamics CRM is the use of scripting. Version 2011 brings a new object model that is much clearer and concise. Using this object model guarantees that future cumulative updates will not break your scripts, thus it is highly recommended that you convert your old scripts that are using Dynamics CRM 4.0 or standard JavaScript to the new object model as much as possible.

The new object model revolves around the Xrm.Page object hierarchy, and brings capabilities to manipulate user interface elements, user forms, and navigational elements. While at first glance the new syntax might seem overwhelming, with some practice it can prove easy to learn.

The object hierarchy of Xrm.Page is described in detail on TechNet, at http://technet.microsoft.com/en-us/library/gg328474.

 

Opening a free 30-day trial of Dynamics CRM 2011 Online


In order to follow along with the recipes described in this book, you should sign up for a 30-day trial of Dynamics CRM 2011 Online rather than using an existing production server. This way, all customizations implemented in this environment will not affect your production environment, but they can all be packaged and moved to any other environment once you feel comfortable to do so.

Getting ready

Dynamics CRM Online has now been packaged with Office 365. In order to sign up for a new online instance, you will need to provide a few details as described in the following section.

How to do it…

Creating a new instance of Microsoft Dynamics 2011 Online is a quick and painless task. All you need to figure out is what URL you want to use, and fill in the wizard-driven configuration.

  1. Navigate to http://crm.dynamics.com.

  2. On the GET STARTED tile, click on the Free trial icon.

  3. On the following page click on the Start your CRM trial button.

  4. The next screen presents you with a form where you provide your personal information as well as the new domain you want to use. This sign-up creates your Office 365 trial account of which Dynamics CRM 2011 is a part.

  5. When choosing your domain name, check its availability by clicking on the Check availability button. If the selected name is already taken, you will be prompted to select a different one.

  6. Pay close attention to the Country or region field. This selection cannot be modified once the instance is created, as it sets global parameters for your environment. Note that this will not stop you from configuring additional currencies and territories.

  7. Once you confirm the domain name availability, you are prompted to create a user ID. As part of the validation process, your mobile number is required and a text message with a confirmation code is sent.

  8. Once all the fields are completed, click on the create your account button. You are being directed to the Administrative page for your organization. You will observe here the message about the remaining trial period, as well as the navigation to set up and manage your subscription services.

  9. The Microsoft Dynamics configuration usually takes a little while longer, and you will see a status of "complete" when done.

  10. From here on you are ready to add new users and to navigate to your newly created Dynamics CRM 2011 Online instance by clicking on the CRM link.

How it works...

Dynamics CRM 2011 Online is a cloud-based solution offered on the SaaS model. A new instance is provisioned every time a new user goes through this process. With the new model, CRM Online is now part of the Office 365 offering, thus the common familiar account creation process.

There's more...

As mentioned before, certain instance information cannot be modified after they are provisioned during the wizard. These include the organization's name, country, currency, and language. While the default values cannot be modified, additional languages and currencies can be customized, and additional territories can be configured.

 

Using solutions to package our work


A new concept introduced with Dynamics CRM 2011 is that of solution packages. This allows for code separation, ease of deployment, and cleans up the mess sometimes created in previous version by the large number of possibly unorganized customizations.

Getting ready

In order to follow through with this recipe, log in to your instance of Dynamics CRM 2011 with an account that has either an administrator or system customizer role. These roles have the necessary out-of-the-box permission to allow you to work with solutions.

How to do it...

A solution package is a collection of customizations and configurations that can be generated in an environment and relocated to an additional environment. Creating a new solution is a process that a system administrator or a system customizer can achieve with the following steps:

  1. Navigate to Settings.

  2. Under Customization, click on Solutions.

  3. In the All Solutions view, select New to create a new solution package.

  4. Fill in the mandatory fields, create a new Publisher or use the default value, and click on the Save icon.

Note

When developing a solution for the Marketplace (as an ISV) you will need to fill in most of the provider information.

Removing a solution is also a very simple process. Select the solution to be removed, and click on the Delete icon.

Tip

If this is a managed solution, all solution configurations are cleanly removed from the environment. For an unmanaged solution, the process is not as straight-forward, as the removal of an unmanaged solution will leave behind customizations and trailing components. For additional details on solution see the Solution model section of this recipe.

How it works...

Creating a new solution requires you to assign a publisher. By default, each instance of Dynamics CRM 2011 includes a default publisher for the instance. For environments where a solution is to be published to production, create a publisher that includes the organization details of the customizer.

There's more...

Solutions offer additional features such as versioning, which can be essential in tracking progress and deploying to the production environment.

Solution model

Also, with solutions, we have two very important models, a managed and unmanaged solution model. A managed solution is a restrictive package, which can only be edited by specific users, and can be cleanly rolled back. An unmanaged solution on the other hand is a type of package used mostly for development environments, and it allows various developers to move customizations from one environment to another, while keeping all customizations editable by all users with the proper permissions. Unmanaged solutions, when removed, will not be removed cleanly, and will leave behind traces of customizations. For this reason, they are not the recommended way of deploying to the production environment.

See also

 

Creating and managing entities


In this recipe, we'll go through the process of creating a new entity, look at how to configure such an entity, and in the end, how to clean up an entity that's not required in our solution package.

All customizations in this book are created as part of a solution. As such, we will be reusing the previously created solution.

Getting ready

In order to proceed, log in to your existing Dynamics CRM 2011 instance and navigate to the solution we created previously.

How to do it...

In the created solution package, we will be adding a new entity to store a list of all countries. We want this listing to be manageable by a user with proper permissions, so that it can be updated as they do business with new countries.

  1. Open the solution, and navigate to the Entities tab.

  2. Click on New to add a new entity to the solution.

  3. Fill in the mandatory fields, and define where this entity will be visible. In our case, we will make this entity visible only in the Settings area. We are setting the Ownership of this entity to Organization, thus making it available across the environment.

  4. Additionally, the Options for Entity section allows you to define some of the standard elements and behaviors that can be included with the entity. These are comprised of behavioral settings, data settings, and configurations for mobile and Outlook. These settings give you a granular access to configure how an entity can interact with the system and other entities, what processes can be run against this entity, and storage options for related files.

  5. Before clicking on the Save icon, direct your attention to the second tab named Primary Field. All entities will need to have a primary field, and we can define the properties of such fields on this tab. We will define the display name as Country, leave the Requirement Level with the default value Business Required, and the Type as Single Line of Text.

  6. Now we click on Save.

  7. Once the entity is created, additional options to edit Forms, Views, Fields, and Relationships become available.

  8. Great job! Now we have the entity created and we can start working with it.

Removing an entity that is not being used by our customization is again a relatively simple task.

  1. In our solution package, select the entity.

  2. Click on the Delete button.

Tip

If the entity does not have any remaining associations with other entities in the system, it will be removed. Otherwise, a message will prompt you, and additional information is provided to help in identifying what relationships are preventing you from deleting this entity.

How it works...

Each entity is stored in the database as a set of two tables. The first one stores the base entity, while the second one stores the customizations to the entity. Creating a new entity in fact creates this set of tables, one that stores the entity generic properties, and another that stores each data fields defined. So, in our case, we will have a table called new_countryBase, and another called new_countryExtensionBase, as seen in the following two images:

The extended table has the following definition:

This information is only available while working On-Premise. With a Dynamics CRM 2011 Online instance, there is no direct access to the database.

Making modifications directly to the database in Dynamics CRM 2011 is not supported. This can cause various issues. All data access should go through web services. When creating custom reports, use the views. They also present the trimmed data security.

See also

 

Creating and managing fields


Once we have an entity ready to work with, whether it's a new custom entity or an existing out-of-the-box entity, we can start managing the information to be captured.

Getting ready

In this recipe, we will be building on the custom Country entity we created in the previous recipe. Open the created solution and navigate to the created Country entity. We will be adding two more fields to capture the country code and display sequence.

How to do it...

In order to add the additional fields to capture information, we will follow these steps:

  1. Expand the Entities section in the solution, and expand the Country custom entity that we created:

  2. Select the Forms option, and look at the view of forms associated with this entity.

  3. By default, we will be presented with two views, one that we will customize, and another mobile form. We will open the first form, of type Main.

  4. From the bottom-right of the screen, we will click on the New Field button.

  5. We will create a field with the display name of Country Code, no requirements constraint, and of type text, single line, with a maximum length of 3. We will be using this field to capture country codes such as CA, USA, and UK.

  6. Once complete, we'll click on Save and Close. The new field will show in the listing of All Fields in Field Explorer.

  7. Now we can simply drag this field on the form, under our Country field.

  8. With this field already added to the form, we can add a new field called Display Sequence, of type Whole Number, as described by the following screenshot:

  9. We will be adding this field to the same form. This entity will be used in the next recipe. The end result should look like the following image:

  10. Now we can click Save on the form screen, and then Publish. All customizations must be published before they are visible to all users.

  11. Once all customizations are published, we can verify the form by navigating to the Settings area, and looking under Extensions. We will find our custom Countries entity there.

  12. Clicking on Countries will show us a view of all Active Countries (default view). We can click on New to see how the customized form will look.

  13. If we are not satisfied with the way the form looks, we can go back to the solution and tweak the look of it again, or else we can proceed further.

How it works...

Because we have configured the new entity to be visible only in the Settings section, we will have to navigate to Settings to get access to the listing of countries we will be adding to the system. We have customized it as such so that when we build a more complex solution later in the book, we can make sure that only authorized users will be able to see and modify the listing of all countries in such a way.

There's more...

Dynamics CRM 2011 allows us to define a large number of field types. We have only covered two simple examples here, one of text and one of whole numbers. Additional field types will be described in the following recipes, when we start looking at how to script these fields.

See also

 

Creating and managing forms


In certain instances, we will require additional forms for the same entity. One obvious example is when we need to implement role-based forms. In such instances, a specific role could have access to only a subset of fields, while a different role could have access to other fields. We could have an overlap of fields available to all roles too.

Getting ready

Open the previously created solution, and navigate to the Countries entity we created. In the view of All Forms, by default, we only have the two forms, one for the normal use and one for mobile.

How to do it...

Adding a new form can be achieved as follows:

  1. From the All Forms view, select New | Main Form, or simply click on New.

  2. On this new form, we can remove the Country Code and Display Sequence fields, by selecting each field and clicking on Remove on the ribbon.

  3. Click on the Form Properties button on the ribbon, and on the Display tab (second from the left) to define a new name for our form.

  4. Once the fields are removed and the form renamed, we can save and publish the new form.

  5. With the form published, we can verify the listing of All Forms to make sure the form is saved.

  6. To verify how this form is presented to users, we can navigate to Country created in the system, and select from the available forms.

  7. Now, both forms are available to be selected for users that have permissions to view both. We will be covering role-based forms later in a different recipe.

Removing a form is also a relatively simple process.

  1. Navigate in the solution package to the All Forms view and select the form to be removed.

  2. Click on the Delete button.

  3. Publish all customizations.

How it works...

Creating various forms for entities can help reduce the amount of scripting required to hide/show fields. We can assign specific forms to specific user roles, and thus reduce the amount of client-side processing on the form.

 

Creating and managing scripts


JavaScript scripts are added to a solution package just like any other resource. They are becoming part of the package, and thus are easily portable to other environments, along with the solution.

Getting ready

Using the existing solution package we have created, we will be adding a new JavaScript resource.

How to do it...

To add a new JavaScript resource, navigate to Settings, and open your existing solution package. Follow these steps to add a script resource:

  1. In the package, navigate to Web Resources.

  2. In the Web Resources view, select New to add a new script resource.

  3. Give the resource a name, and select the Script (Jscript) option from the Type drop-down list. The Display Name should be in a readable format. It is good practice to always add a Description for other developers that will follow in your foot steps to easily identify the content of the library without having to open the file.

  4. Click on Save, and then on Publish. Now you have a JavaScript resource added to your solution package. There is no actual script added yet, so let's do that.

  5. Click on the Text Editor button. A new form will open allowing you to type in your script.

  6. Click OK, then Save and Publish your resource again.

  7. Now we have a script added as part of the solution package. It is not related to any action, but we will cover that in one of the later recipes.

Sometimes a script resource can become obsolete, and we would want to remove it. In order to remove a script resource that is not associated with any other system entities, perform the following steps:

  1. From the Web Resources view, select the script.

  2. Click on Delete.

  3. Make sure to click on Publish all Customizations for the changes to take effect. Now your script resource has been removed.

 

Creating and managing other resources


JavaScript resources are not the only available resource that can be added to your solution package. Other file types can be also associated in a similar manner, including HTML pages, stylesheets, image files, as well as Silverlight controls.

Getting ready

Navigate to your solution package, and expand the Web Resources section.

How to do it...

On the Web Resources view, follow these steps:

  1. Click on New to add a new resource.

  2. In the new window, give the resource a name, a display name, and add a description if necessary.

  3. Select JPG for Type as we will be adding an image resource.

  4. Click on Browse to retrieve a locally-stored image.

  5. Once the picture is selected, click on Save and then on Publish. Now your resource is added to the solution package.

    Tip

    The URL field shows a reference to how you access the resource image directly. We will be using part of that URL in scripts where we work with image resources.

 

Creating and managing workflows


Workflows, along with the newly introduced concept of dialogs have all been grouped as Process in Dynamics CRM 2011. The workflow concept remains similar to that of previous versions, with some additional functionality.

Getting ready

We will be adding a very basic workflow to our existing solution package. Open the already created solution package to work with.

How to do it...

In order to add a new workflow using the wizard, perform the following steps:

  1. In the solution package, navigate to Processes and open the Processes view.

  2. Click on New to create a new workflow.

  3. On the Process: New page, give the new workflow a name, select an entity it will work against, and from the Category drop-down, select Workflow.

  4. Leave default the selection for New blank process, and click on the OK button.

  5. The process wizard starts, and it allows us to configure the workflow properties and parameters.

  6. In the Options for Automatic Processes section, I will select the scope to be Organization, and the start to be generated by the Record is created event.

  7. Next, we will add a simple e-mail notification step to be executed. Click on Add Step, and select Send E-mail. This will add the Send e-mail step, and allows us to configure the properties.

  8. Click on Set Properties, and start customizing the e-mail properties. We can add the From to be the current user that creates the record, and the To field to be the owner of the record. Also, we can create a subject and body.

  9. Click on Save and Close to complete the message configuration.

  10. Additionally, you can define a step description in the workflow step wizard window.

  11. With our step created and configured properly, now we can save the workflow. Click on Save. In order to enable this workflow, we have to activate it. Click next on Activate.

  12. Click OK on the Process Activation Confirmation window. If there is no error in the workflow, it will become active.

  13. Once the workflow is activated, we can create a new record and verify that it performs as expected.

How it works...

This sample workflow we created sends an e-mail confirmation once a new Contact record is created. As long as the workflow stays active in the system, it will execute on each record creation, as configured. Other configuration options include the capture of field value changes, new assignment of record to another user, or on record deletion. The record deletion event captured can also be used to validate record deletion, and stop the process if certain conditions are not met.

See also

 

Creating and managing dialogs


A dialog differs from a workflow through the fact that it is an interactive process, where user input is required. The addition of dialogs offers new possibilities with Dynamics CRM 2011.

Getting ready

We will be using the same solution package we created earlier.

How to do it...

Adding a dialog is done by performing the following steps:

  1. Open the existing solution package, and navigate to Processes.

  2. In the Processes view, click on New to add a new process.

  3. On the Process: New window, select Dialog this time.

  4. Keep New blank process selected and click on OK.

  5. The dialog configuration window allows us configure the dialog details. A dialog can be started as either an on-demand process, or as a child of another process. This allows us to create smaller dialogs, and chain them together as required to achieve our final result. We will mark this dialog as an on-demand process, so we can see the very basic functionality available.

  6. To capture user interaction, we will first add a page to the dialog, and then a Prompt and Response on that page.

  7. Click on Set Properties to define the prompt and available answers.

  8. Click on Save and Close to finish defining this step.

  9. In the process builder, add a new Update step that will fill in the captured details within the form field.

  10. Click on Set Properties to define which value gets assigned to what field. We are being presented with the standard Contact form. Click into the First Name field, and from the Form Assistant, select New contact first name under Local Values in the Look for drop-down list, and Response Text. Click on Add and then OK.

  11. Save and Close this form. Save the dialog and activate it.

  12. To test this newly created dialog, create a new Contact record, and from the ribbon, click on the Start Dialog button.

  13. From the selection window that opens up, select the dialog we created earlier.

  14. Click on OK and answer the prompt question.

  15. Click on Next. When the dialog finishes, an end of dialog window is displayed. Click on Finish.

  16. Once the dialog completes execution, you will see the value captured during the dialog populated into the contact's first name field.

How it works...

For demonstration purposes we have only captured the contact's first name and populated the first name field. More steps can be added to a dialog to capture additional information and guide a system user in capturing all required details through a dialog rather than by using a free form. This way a "script" can be created for the user to capture information in a specific order, thus enforcing a clean, repeatable process in dealing with customers in a Call Centre scenario.

 

Starting a workflow from a dialog


We have looked at workflows and dialogs. But they can function together to achieve a result. For example, we can start from a dialog, capture specific information, and then kick off a workflow as part of the same process to process the information and generate a result.

Getting ready

Using the same solution package, we will be creating two new processes. One is a workflow that sends an e-mail when a field value changes, which we will mark as a child process. The other is a dialog that will call this workflow.

How to do it...

Create the child workflow by performing the following steps:

  1. Create a new workflow, on the Account entity this time. Mark it to run as a child process.

  2. Add a send e-mail step to the workflow, and configure the email properties as described in a previous recipe.

  3. Activate the workflow.

Create the parent dialog that will kick-of this workflow by performing the following steps:

  1. Create the parent dialog on the same entity, as a dialog.

  2. Create a new page, then a question with a Yes/No option set. Create a new check condition step, in which if the value selected in the dialog is Yes, then create a Start Child Workflow step. Look up the child workflow we created earlier and select it.

  3. Save and activate this dialog.

How it works...

The dialog we have created, as simple as it is, prompts the user to decide whether they want to run a workflow or not. When the user selects Yes, the workflow is started.

We can easily test this functionality by going to a new account, and selecting from the ribbon the Start Dialog button. A window prompts us to select the dialog we will run.

We can easily check the execution status of both the dialog and the workflow by navigating on the account to the Processes section.

These two views will give us details about the processes that run against the current account, and the status of each.

 

Working with security roles and permissions


Security roles and permission can be configured as part of the same solution package. This allows us to port these configurations from one environment to another. Be aware though that there are some limitations in the use of security roles as part of a solution. One of the most important is a limitation where roles can only be configured at a top business unit when they are saved as part of a solution package.

Getting ready

We will be working within the same solution package we created earlier.

How to do it...

In order to add a new security role we must perform the following steps:

  1. Open the existing solution package.

  2. Navigate to the Security Roles view.

  3. To create a completely new role, select New. A better approach is to modify one of the existing roles by selecting Add Existing, but for the purpose of this recipe we will create a new role. Click on New.

  4. Give the role a name and make the necessary modifications. Click Save and Close. The new role is added as part of the solution.

  5. With this new role created, we can go back to the custom Country form we previously created. Open the form and click on Assign Security Roles.

  6. Deselect the selected security roles, and select the newly created security role.

  7. Click on OK, save changes, and publish all customizations.

How it works...

At this point we have a new security role added to the solution package. Additionally, we have configured the custom Country form to be visible only by users that are assigned the newly created security role.

See also

About the Author

  • Nicolae Tarla

    Nicolae Tarla is an independent senior consultant specializing in business solutions architecture and implementations. Starting with 2014, he was recognized for four consecutive years as a Microsoft Business Applications MVP for his contributions to the community. For more than 10 years he's been designing and implementing multiple enterprise and mid-size solutions across the entire Microsoft stack for both the private and public sectors. He has been delivering Microsoft Dynamics 365 solutions since version 3.0 of the platform. He continues to be actively involved with various community events, and continues to share his knowledge through his personal blog and other networks. He can be reached on Twitter where his handle is @niktuk.

    Browse publications by this author

Latest Reviews

(1 reviews total)
great book, lots of info.