Home > CRM 4.0, JavaScript > Mini Preview Window for Contact Lookup Field

Mini Preview Window for Contact Lookup Field

March 9th, 2010

Preview Field

Looks cool doesn’t it. well here goes nothing..

to achieve the above you will need the following (click read more link..)

// the following code is for the hover over lookup window

function OnCrmPageLoad()
{
 /*
  Reference the to Lookup, Provide the lookup (control) id.
 */
  var PrvToLui = new LookupPreview("new_primarycontactid");
 /*
  Add Account Preview Information ,
  returns an account entity wrapper object
 */
 var accountEntity = PrvToLui.AddEntity("account");

 /*
  Add Account Preview Attributes,
  Provide lable and schema name for each attribute
 */
  accountEntity.AddAttribute("Business Phone","address1_line1");
  accountEntity.AddAttribute("Mobile Phone","address1_city");
  accountEntity.AddAttribute("Fax","address1_stateorprovince");
  accountEntity.AddAttribute("E-mail","address1_postalcode");
 /*
  Add Related Entity (Primary Contact) Information
  AddLinked requires the entity name and
  lookup field schema name on the account.
 */
 var contactEntity = accountEntity.AddLinked("contact","primarycontactid");
  contactEntity.AddAttribute("Primary Contact","fullname");
  contactEntity.AddAttribute("Contact Phone","telephone1");

 /* ------------------------------------------------------------------ */

 //Contact Entity Preview
 var contactEntity = PrvToLui.AddEntity("contact");
  contactEntity.AddAttribute("Business Phone","telephone1");
  contactEntity.AddAttribute("Mobile Phone","mobilephone");
  contactEntity.AddAttribute("Fax","fax");
  contactEntity.AddAttribute("E-mail","emailaddress1");

 var accountEntity = contactEntity.AddLinked("account","parentcustomerid");
   accountEntity.AddAttribute("Parent Company", "name");
   accountEntity.AddAttribute("Parent Street","address1_line1");
   accountEntity.AddAttribute("Parent Suburb","address1_line2");
   accountEntity.AddAttribute("Parent State","address1_stateorprovince");
   accountEntity.AddAttribute("Parent Postcode","address1_postalcode");
   accountEntity.AddAttribute("Parent Website","websiteurl");
 /* ------------------------------------------------------------------ */

}

function LookupPreview( lookupId )
{
   var Instance = this;
  Instance.Lookup = document.getElementById( lookupId );

   if(  isNullOrEmpty(Instance.Lookup) )
  return;

   //Public
   Instance.Entities = [];

   Instance.AddEntity = function(entityName){
  var Entity = new Object();
   Entity.Name = entityName;
   Entity.AddAttribute = function( labelName , attrName ){
    var Attributes = Instance.Entities[entityName].Attributes;
    var attribute  = new Attribute(entityName , attrName , labelName , attrName);
     Attributes[Attributes.length] = attribute;
     DisplayAttributes[DisplayAttributes.length] = attribute;
      }
   Entity.Attributes  = [];
   Entity.LinkedByName  = [];
   Entity.LinkedByIndex = [];
   Entity.AddLinked = function( lnkEntityName , referencingAttribute )
   {
    var LinkEntity = new Object();
     LinkEntity.Name = lnkEntityName;
     LinkEntity.Attributes = [];
     LinkEntity.RefAttribute = referencingAttribute;
     LinkEntity.AddAttribute = function( labelName , attrName )
     {
      var attribute = new Attribute(entityName , attrName , labelName , LinkEntity.RefAttribute + "." + attrName);
       LinkEntity.Attributes[LinkEntity.Attributes.length] = attribute;
       DisplayAttributes[DisplayAttributes.length] = attribute;
     }
     Entity.LinkedByIndex[Entity.LinkedByIndex.length] = Entity.LinkedByName[name] = LinkEntity;
    return LinkEntity;
   }
   Instance.Entities[entityName] = Entity;
  return Entity;
   }

   Instance.Show = function(dataElement)
   {
  var control   = Instance.Lookup;
  var DataValue = control.DataValue;

  if( isNullOrEmpty(DataValue) )
   return;
  if( isNullOrEmpty(Instance.Entities[DataValue[dataElement.Index].typename]) )
   return;

  TooltipPopup = window.createPopup();            

  if( !dataElement.PreviewHTML )
  {
   var ToolTipHTML = "<fieldset style='width:100%;height:100%;border:1px solid gray;background-color: #d8e8ff;filter: progid:DXImageTransform.Microsoft.Gradient(GradientType=0,StartColorStr=#eff3ff,EndColorStr=#c6dfff);padding-left:2px;'><legend style='background-color:#eff3ff;width:100;padding-left:13px;border:1px solid gray;font-size:12px;font-family:tahoma'>Preview</legend>";
   var xmlHttp = CreateXmlHttp();
   var xml = "<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"";
    xml += " xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"";
    xml += " xmlns:xsd="http://www.w3.org/2001/XMLSchema">";
    xml += GenerateAuthenticationHeader();
    xml += "<soap:Body>";
    xml += "<Fetch xmlns="http://schemas.microsoft.com/crm/2007/WebServices">";
    xml += "<fetchXml>";

   var fetchxml = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>";
   var DataVal  = control.DataValue[dataElement.Index];
   var entity   = DataVal.typename;
    fetchxml += "<entity name='" + entity + "'>";
   var attributes = Instance.Entities[entity].Attributes;
    for( var i = 0 ; i < attributes.length ; i++ )
      fetchxml += "<attribute name='" + attributes[i].Name + "'/>";
    fetchxml += "<filter type='and'>";
    fetchxml += "<condition attribute='" + entity + "id' operator='eq' value='" + DataVal.id + "'/>";
    fetchxml += "</filter>";

    for( var i = 0 ; i < Instance.Entities[entity].LinkedByIndex.length ; i++ )
    {
     var linked = Instance.Entities[entity].LinkedByIndex[i];
     fetchxml += "<link-entity name='" + linked.Name + "' from='" + linked.Name + "id' to='" + linked.RefAttribute+ "' visible='false' link-type='outer'>";
     for( var j = 0 ; j < linked.Attributes.length ; j++ )
      fetchxml += "<attribute name='" + linked.Attributes[j].Name + "'/>";
     fetchxml += "</link-entity>";
    }
    fetchxml += "</entity>";
    fetchxml += "</fetch>";

    xml += _HtmlEncode(fetchxml);
    xml += "</fetchXml>";
    xml += "</Fetch>";
    xml += "</soap:Body>";
    xml += "</soap:Envelope>";

    xmlHttp.open("POST", "/mscrmservices/2007/crmservice.asmx", false );
    xmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xmlHttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Fetch");
    xmlHttp.send(xml);

   var resultDoc   = loadXmlDocument(xmlHttp.responseXML.text);
   var previewHtml = "<br style='line-height:2px'/><table width='100%' style='font:12 px tahoma'>";
    dataElement.Width = 0;
    //debugger
    for( var i = 0 , j = 1; i < DisplayAttributes.length ; i++ )
    {
     var attribute = DisplayAttributes[i];
     if( attribute.Entity != entity ) continue;

     var attrNode  = resultDoc.selectSingleNode("//" + attribute.XPathName );
     var attrValue = (attrNode)? attrNode.text:"";

      dataElement.Height = (j++) * 22;
      var maxLength  = ( attrValue.length + attribute.Label.length ) * 9;
      if( dataElement.Width < maxLength )
       dataElement.Width = maxLength;
      previewHtml += "<tr><td style='padding:3px 0px 0px 0px;color:brown'><nobr>" + attribute.Label + "</nobr></td><td><nobr>" + attrValue  + "</nobr></td></tr>";

    }

    dataElement.Height += 20;

    ToolTipHTML += previewHtml;
    ToolTipHTML += "</table></fieldset>";
    dataElement.PreviewHTML = ToolTipHTML;
   }

   TooltipPopup.document.body.innerHTML = dataElement.PreviewHTML;      

  var Position = getControlPostion();
  var Left   = Position.X + 1;
  var Top    = Position.Y + 5;

   TooltipPopup.show( Left , Top - dataElement.parentElement.scrollTop , dataElement.Width, dataElement.Height , null );
   }

   Instance.Hide = function()
   {
  if( TooltipPopup )
   TooltipPopup.hide();
   }

   Instance.OnLookupChange = function()
   {
  var jump = 0;
  if( Instance.Lookup.DataValue != null )
     {
   var DataElementsLen = Instance.Lookup.parentElement.previousSibling.childNodes[0].childNodes.length;
   var DataValuesLen   = Instance.Lookup.DataValue.length;
    jump = (DataElementsLen == DataValuesLen)? 1:2;

     for( var i = 0 ; i < DataValuesLen*jump ; i+=jump )
     {
    var DataValueElemet = Instance.Lookup.parentElement.previousSibling.childNodes[0].childNodes[i];
    if( !isNullOrEmpty(DataValueElemet.onmouseover) ) continue;

    DataValueElemet.Preview = Instance;
    DataValueElemet.Index = i/jump;
    DataValueElemet.onmouseover = function(){
     this.Preview.Show(this);
    }
    DataValueElemet.onmouseout = function(){
     this.Preview.Hide();
    }
     }
     }
   }

   //Private
   var TooltipPopup;
   var DisplayAttributes;

   function Init()
   {
  DisplayAttributes = [];
  Instance.Lookup.attachEvent( "onchange" , Instance.OnLookupChange );
  Instance.OnLookupChange();  //First Time
   }

   function Attribute(entityName,attrName,attrLabel,attrXpathName)
   {
  this.Name = attrName;
  this.Entity = entityName;
  this.Label = attrLabel;
  this.XPathName = attrXpathName;
   }

   function getControlPostion()
   {
  control = event.srcElement;
  var Position = new Object();
  var controlHeight = control.offsetHeight;
  var iY = 0, iX = 0;
  while( control != null )
  {
   iY += control.offsetTop;
   iX += control.offsetLeft;
   control = control.offsetParent;
  }
  Position.X = iX + screenLeft;
  Position.Y = iY + screenTop + controlHeight;
  return Position;
    }

 function isNullOrEmpty( obj ){
  return obj == null || typeof(obj) == "undefined" || obj == "";
 }

   Init();
}

OnCrmPageLoad();

OK, this is what needs to be edited if you need custom attributes listed in the preview window.

you may edit the following code block:

  contactEntity.AddAttribute("Business Phone","telephone1");
  contactEntity.AddAttribute("Mobile Phone","mobilephone");
  contactEntity.AddAttribute("Fax","fax");
  contactEntity.AddAttribute("E-mail","emailaddress1");

“Business Phone” represents the label on the preview form
“telephone1″ is the attribute on the contact entity

I have uploaded a text file with all the code at: http://mscrmblog.net/downloads/previewWindow.txt

This code is pretty self explanatory.. however if you need some help, email me or post a comment :)

MSCRM-Admin CRM 4.0, JavaScript , , , , ,

  1. Paul Robert
    March 17th, 2010 at 18:39 | #1

    Internet Marketing

    We would like to get your website on first page of Google.

    All of our processes use the most ethical “white hat” Search Engine Optimization techniques that will not get your website banned or penalized.
    Please reply and I would be happy to send you a proposal.

  1. March 10th, 2010 at 10:04 | #1
  2. March 10th, 2010 at 11:26 | #2
  3. March 10th, 2010 at 15:15 | #3
  4. March 24th, 2010 at 11:23 | #4
  5. May 13th, 2010 at 01:07 | #5
  6. May 18th, 2010 at 16:03 | #6
  7. May 28th, 2010 at 11:07 | #7
  8. June 4th, 2010 at 05:29 | #8
  9. June 7th, 2010 at 16:09 | #9
  10. June 12th, 2010 at 10:12 | #10
  11. June 13th, 2010 at 13:48 | #11
  12. June 13th, 2010 at 22:50 | #12
  13. June 15th, 2010 at 13:19 | #13
  14. June 18th, 2010 at 10:17 | #14
  15. June 18th, 2010 at 19:10 | #15
  16. June 19th, 2010 at 04:38 | #16
  17. June 20th, 2010 at 18:28 | #17
  18. June 21st, 2010 at 15:49 | #18
  19. June 23rd, 2010 at 07:26 | #19
  20. June 27th, 2010 at 04:41 | #20
  21. July 15th, 2010 at 22:38 | #21
  22. July 18th, 2010 at 07:12 | #22
  23. July 18th, 2010 at 07:13 | #23
  24. July 18th, 2010 at 07:13 | #24
  25. July 18th, 2010 at 07:13 | #25
  26. July 18th, 2010 at 07:13 | #26
  27. July 18th, 2010 at 08:56 | #27
  28. July 18th, 2010 at 08:56 | #28
  29. July 18th, 2010 at 08:57 | #29
  30. July 18th, 2010 at 08:57 | #30
  31. July 18th, 2010 at 08:57 | #31
  32. July 18th, 2010 at 08:58 | #32
  33. July 18th, 2010 at 08:58 | #33
  34. July 18th, 2010 at 15:20 | #34
  35. July 18th, 2010 at 22:02 | #35
  36. July 18th, 2010 at 23:39 | #36
  37. July 19th, 2010 at 02:17 | #37
  38. July 19th, 2010 at 03:37 | #38
  39. July 19th, 2010 at 03:38 | #39
  40. July 19th, 2010 at 03:38 | #40
  41. July 19th, 2010 at 03:38 | #41
  42. July 19th, 2010 at 03:39 | #42
  43. July 19th, 2010 at 03:39 | #43
  44. July 19th, 2010 at 09:59 | #44
  45. July 19th, 2010 at 10:00 | #45
  46. July 19th, 2010 at 18:28 | #46
  47. July 19th, 2010 at 20:03 | #47
  48. July 19th, 2010 at 21:22 | #48
  49. July 20th, 2010 at 03:35 | #49
  50. July 20th, 2010 at 09:50 | #50
  51. July 20th, 2010 at 09:50 | #51
  52. July 20th, 2010 at 09:50 | #52
  53. July 20th, 2010 at 09:51 | #53
  54. July 20th, 2010 at 09:51 | #54
  55. July 20th, 2010 at 09:51 | #55
  56. July 20th, 2010 at 09:52 | #56
  57. July 20th, 2010 at 09:52 | #57
  58. July 20th, 2010 at 16:24 | #58
  59. July 20th, 2010 at 16:25 | #59
  60. July 20th, 2010 at 16:25 | #60
  61. July 20th, 2010 at 16:25 | #61
  62. July 20th, 2010 at 16:25 | #62
  63. July 20th, 2010 at 16:26 | #63
  64. July 20th, 2010 at 16:26 | #64
  65. July 20th, 2010 at 22:01 | #65
  66. July 21st, 2010 at 01:38 | #66
  67. July 21st, 2010 at 01:38 | #67
  68. July 21st, 2010 at 02:59 | #68
  69. July 21st, 2010 at 02:59 | #69
  70. July 21st, 2010 at 02:59 | #70
  71. July 21st, 2010 at 09:15 | #71
  72. July 21st, 2010 at 10:56 | #72
  73. July 21st, 2010 at 10:57 | #73
  74. July 21st, 2010 at 17:09 | #74
  75. July 21st, 2010 at 23:29 | #75
  76. July 22nd, 2010 at 01:31 | #76
  77. July 22nd, 2010 at 01:32 | #77
  78. July 22nd, 2010 at 01:32 | #78
  79. July 22nd, 2010 at 01:32 | #79
  80. July 22nd, 2010 at 01:33 | #80
  81. July 22nd, 2010 at 07:56 | #81
  82. July 22nd, 2010 at 13:35 | #82
  83. July 22nd, 2010 at 13:35 | #83
  84. July 22nd, 2010 at 13:35 | #84
  85. July 22nd, 2010 at 13:36 | #85
  86. July 22nd, 2010 at 13:36 | #86
  87. July 22nd, 2010 at 13:36 | #87
  88. July 22nd, 2010 at 20:12 | #88
  89. July 23rd, 2010 at 14:39 | #89
  90. July 23rd, 2010 at 16:04 | #90
  91. July 23rd, 2010 at 18:36 | #91
  92. July 23rd, 2010 at 19:52 | #92
  93. July 24th, 2010 at 00:32 | #93
  94. July 24th, 2010 at 02:12 | #94
  95. July 24th, 2010 at 03:34 | #95
  96. July 24th, 2010 at 03:34 | #96
  97. July 24th, 2010 at 10:04 | #97
  98. July 24th, 2010 at 10:04 | #98
  99. July 24th, 2010 at 10:04 | #99
  100. July 24th, 2010 at 10:05 | #100
  101. July 24th, 2010 at 15:59 | #101
  102. July 24th, 2010 at 15:59 | #102
  103. July 24th, 2010 at 16:00 | #103
  104. July 24th, 2010 at 16:00 | #104
  105. July 24th, 2010 at 17:37 | #105
  106. July 24th, 2010 at 19:20 | #106
  107. July 24th, 2010 at 19:21 | #107
  108. July 24th, 2010 at 21:00 | #108
  109. July 24th, 2010 at 22:31 | #109
  110. July 24th, 2010 at 22:32 | #110
  111. July 25th, 2010 at 00:24 | #111
  112. July 25th, 2010 at 00:24 | #112
  113. July 25th, 2010 at 00:24 | #113
  114. July 25th, 2010 at 00:24 | #114
  115. July 25th, 2010 at 00:25 | #115
  116. July 25th, 2010 at 00:25 | #116
  117. July 25th, 2010 at 01:27 | #117
  118. July 25th, 2010 at 02:34 | #118
  119. July 25th, 2010 at 02:34 | #119
  120. July 25th, 2010 at 02:35 | #120
  121. July 25th, 2010 at 04:15 | #121
  122. July 25th, 2010 at 04:15 | #122
  123. July 25th, 2010 at 04:16 | #123
  124. July 25th, 2010 at 04:16 | #124
  125. July 25th, 2010 at 04:16 | #125
  126. July 25th, 2010 at 10:42 | #126
  127. July 25th, 2010 at 20:32 | #127
  128. July 26th, 2010 at 03:00 | #128
  129. July 26th, 2010 at 20:54 | #129
  130. July 26th, 2010 at 20:55 | #130
  131. July 26th, 2010 at 21:01 | #131
  132. July 27th, 2010 at 03:24 | #132
  133. July 27th, 2010 at 03:24 | #133
  134. July 27th, 2010 at 03:25 | #134
  135. July 27th, 2010 at 08:35 | #135
  136. July 27th, 2010 at 08:36 | #136
  137. July 27th, 2010 at 08:36 | #137
  138. July 27th, 2010 at 14:58 | #138
  139. July 27th, 2010 at 14:58 | #139
  140. July 27th, 2010 at 18:27 | #140
  141. July 29th, 2010 at 04:14 | #141