Additional Script Samples

Chapter 7, described how you can use scripting techniques to further extend Microsoft Dynamics CRM. The next two examples demonstrate how to perform the following common application requests using scripts:

  • Conditionally enabling attributes

  • Hiding navigation links on a form

Conditionally Enabling Attributes

When you configure attributes in Microsoft Dynamics CRM, you can specify a requirement level for each attribute: no constraint, business recommended, or business required. However, you will probably encounter scenarios where you want to dynamically change the requirement level of an attribute based on the value of a different attribute. For example, you might require an account number for your customers (account entity), but you may not require an account number for your prospects, vendors, suppliers, etc. Since all of these types of records use the account entity, you can’t use the entity editor to set the requirement level of the account number attribute without impacting all types of accounts. Fortunately, you can take advantage of form scripting techniques and the SetRequiredLevel() method to implement this type of customization in Microsoft Dynamics CRM. Example 15-5 shows a simple example of enabling the Account’s account number attribute when the Account’s relationship type attribute is set to the value of Customer.

Example 15-5. Conditionally enabling the Account’s account number attribute

if (crmForm.all.customertypecode.DataValue == 3) {
  crmForm.SetFieldReqLevel("accountnumber", 1);
}
else {
  crmForm.SetFieldReqLevel("accountnumber", 0);
}

You will need to add the script in Example 15-5 to both the Account form’s onLoad event and the Relationship Type attribute’s onChange event. Figure 15-1 shows that when a user selects Customer for the Relationship Type, the account number field becomes required.

Conditionally requiring the account number attribute

Figure 15-1. Conditionally requiring the account number attribute

Warning

Warning

This type of form manipulation might not upgrade to future versions of Microsoft Dynamics CRM without additional work.

Hiding Navigation Links on a Form

Each entity’s form contains one or more related links located on the form’s left navigation links. Figure 15-2 shows the default left navigation links of the Account form.

The Account form’s left navigation links

Figure 15-2. The Account form’s left navigation links

When you create a new custom relationship between entities, Microsoft Dynamics CRM allows you to configure whether you want to display a link on the referenced entity to the referencing entity or a link on each entity in a many-to-many relationship, as shown in Figure 15-3.

Configuring the left navigation links of a custom relationship

Figure 15-3. Configuring the left navigation links of a custom relationship

By default, the native Microsoft Dynamics CRM security settings determine most of the related links that a user will see when they open a record. If the user viewing the record does not have minimum Read access to a related entity, Microsoft Dynamics CRM automatically removes the link from the record. However, some of the links, such as an Account’s Relationships, More Addresses, or Sub-Accounts, always display on the form’s left navigation. In the following situations, the ability to hide or disable the form’s system links may be useful:

  • You may wish to hide the system links that are not used in your implementation.

  • You want to replace the purpose of those links with your own implementation and don’t need two links displayed.

  • You want to enable the links for only certain users or roles.

  • You need to enable the links only when certain business criteria are met.

Unfortunately, toggling the security roles will not hide some of these links from the navigation pane. Fortunately, you can use form scripting to programmatically hide or disable the related links. If you want to, you could further extend this script example to dynamically modify the navigation pane links based on the security role of the user viewing the record. Example 15-6 shows a series of script functions you can use to hide/show and enable/disable a form’s system left navigation links. You can easily reuse these functions with any entity.

Warning

Warning

This type of customization might not upgrade to future versions of Microsoft Dynamics CRM without additional work.

Example 15-6. Left navigation link display functions

// Removes a specified left navigation link on a form
function hideLeftNavItem(leftNavItemText)
{
  displayLeftNavItem(leftNavItemText, false)
}

// Displays a hidden left navigation link on a form
function showLeftNavItem(leftNavItemText)
{
  displayLeftNavItem(leftNavItemText, true)
}

// Show/hide helper function
function displayLeftNavItem(leftNavItemText, showItem)
{
  // left nav items have two extra trailing spaces
  leftNavItemText += "  ";

  var leftNav = document.getElementById("crmNavBar");
  if (leftNav)
  {
    var items = leftNav.all;
    var itemsLength = items.length;

    for (var i = 0; i < itemsLength; i++)
    {
      var leftNavItem = items[i];

      if (leftNavItem.tagName == "LI" && leftNavItem.innerText == leftNavItemText)
      {
        // set display value based on the value of showItem
        leftNavItem.style.display = (showItem) ? "" : "none";
        return;
      }
    }
  }
}

// Disables a left navigation link
//  (will gray out the text of the left nav item as well as the image)
function disableLeftNavItem(leftNavItemText)
{
  setLeftNavItemState(leftNavItemText, true);
}

// Enables left navigation link
function enableLeftNavItem(leftNavItemText)
{
  setLeftNavItemState(leftNavItemText, false);
}

// Enable/disable helper function
function setLeftNavItemState(leftNavItemText, disableLink)
{
  // left nav items have two extra trailing spaces
  leftNavItemText += "  ";

  var leftNav = document.getElementById("crmNavBar");
  if (leftNav)
  {
    var items = leftNav.all;
    var itemsLength = items.length;

    for (var i = 0; i < itemsLength; i++)
    {
      var leftNavItem = items[i];

      if (leftNavItem.tagName == "LI" && leftNavItem.innerText == leftNavItemText &&
        leftNavItem.firstChild != null)
      {
        // Disable the item based on the value of disableLink
        leftNavItem.firstChild.disabled = disableLink;

        var leftNavItemChildren = leftNavItem.all;
        var leftNavItemChildrenLength = leftNavItemChildren.length;

        for (var j = 0; j < leftNavItemChildrenLength; j++)
        {
          var leftNavItemChild = leftNavItemChildren[j];
          if (leftNavItemChild.tagName == "IMG")
          {
            // Set filter style based on value of disableLink
            leftNavItemChild.style.filter = (disableLink) ? "gray" : "";
            break;
          }
        }
        return;
      }
    }
  }
}

To use the display-related links logic, you need to include the appropriate script functions to the onLoad event of the form. You also need to pass the display name of the link into the function.

The following sample demonstrates how to use the script in Example 15-6 to disable the Relationships link from the Account form’s left navigation links. Insert the following line of code preceding the script shown in Example 15-6 into the onLoad event of the Account form:

disableLeftNavItem("Relationships");

Publish your changes and then open an account record. Figure 15-4 shows the Relationships link disabled after you apply the script.

Account with the Relationships link disabled

Figure 15-4. Account with the Relationships link disabled

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset