Donnerstag, 16. Juni 2011

Useful Form Scripts, CRM 2011 Style

Useful Form Scripts, CRM 2011 Style

The style of JScript we need to write for Dynamics CRM 2011 has changed significantly in the upgrade from CRM 4.0. If you learn best from examples, here are a few you may find helpful. If you’re just starting out and need to figure out some of the basic stuff like how you add functions to function libraries, and how you add a function library to form properties, here’s an intro article I wrote on the topic.
This article is in more of a summary style — let me know what you think of the format.
Function ShowAccountTabs()
Illustrates hiding and showing tabs , based on selected value of option set
- Contained in function library added to Account form
- Requires two custom tabs, named “customer” and “competitor”
- Tab labels can be anything you want
- function added as event handler for the system option set, customertypecode, (default label Relationship Type)
Function Listing:
function ShowAccountTabs() {
   if (Xrm.Page.getAttribute("customertypecode").getValue() != null ) {
var RelationshipType = Xrm.Page.getAttribute("customertypecode").getSelectedOption().text;
if (RelationshipType == "Customer") {
         Xrm.Page.ui.tabs.get("customer").setVisible(true);
         Xrm.Page.ui.tabs.get("competitor").setVisible(false);
      }     
      else if (RelationshipType == "Competitor") {
         Xrm.Page.ui.tabs.get("customer").setVisible(false);
         Xrm.Page.ui.tabs.get("competitor").setVisible(true);
      }
      else {
         Xrm.Page.ui.tabs.get("customer").setVisible(false);
         Xrm.Page.ui.tabs.get("competitor").setVisible(false);
      }
   }
}
Function CreateOpportunityURL()
Creates a clickable link (URL) for an opportunity record. Useful to include in a workflow-generated email. (Remember: if they aren’t using the Outlook client, the “Regarding” field won’t be clickable.
- Contained in function library added as handler to Opportunity form
- Added as event handler to OnSave event
- Requires custom field for URL, prefix_url
- Current code requires record to be saved once so that GUID exists
Function listing:
function CreateOpportunityURL()
{
   var opportunityid = Xrm.Page.data.entity.getId();
if (opportunityid!= null) {
      if (Xrm.Page.getAttribute("rgk_url").getValue() == null)
      {
         var serverurl = Xrm.Page.context.getServerUrl();
         var recordurl = serverurl+"main.aspx?etn=opportunity&id=";
         var strtemp = opportunityid.replace("{", "%7b");
         var strtemp = strtemp.replace("}", "%7d");
         recordurl += strtemp;
         recordurl += "&pagetype=entityrecord";
         Xrm.Page.getAttribute("rgk_url").setValue(recordurl) ;
      }
   }
}
Function DisplayWebSiteinIFrame()
- Contained in function library added to Account form
- Event handler for custom option set, <prefix>_website, with two values, “SharePoint” and “Bing Maps”
- Depending on which is selected, updates IFRAME_selectedwebsite with dynamically created URL
- For SharePoint search URL, replace “https://sp.magenium.com” with your organization’s SharePoint site
Function listing:
function DisplayWebSiteInIFrame()
{
 if (Xrm.Page.getAttribute("rgk_website").getSelectedOption().text != null) {
var selectedwebsite = Xrm.Page.getAttribute("rgk_website").getSelectedOption().text;
var IFrame = Xrm.Page.ui.controls.get("IFRAME_selectedwebsite");
var Address = Xrm.Page.getAttribute("address1_line1").getValue();
   Address = Address + " " + Xrm.Page.getAttribute("address1_city").getValue();
   Address = Address + " " + Xrm.Page.getAttribute("address1_stateorprovince").getValue();    
switch(selectedwebsite)
   {
      case "SharePoint" :
         IFrame.setSrc("https://sp.magenium.com/search/Pages/Results.aspx?k=" + Xrm.Page.getAttribute("name").getValue() );
         break;
case "Bing Maps" :
         IFrame.setSrc("http://www.bing.com/maps/?q=" + Address);
         break;
   }
 }
}

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Free Samples By Mail