Thursday, January 07, 2010

Setting attribute description as a tooltip for a field in entities forms in Microsoft Dynamics CRM 4.0

There is simple way to set tooltip text for field on CRM form. You can do it with following script placed on OnLoad event handler of some CRM form:

crmForm.all.<field name>_c.title = 'Tooltip text';
crmForm.all.<field name>_d.title = 'Tooltip text';


It will be hard work to create detailed tooltips for all fields placed on form
using this script. I've decided to develop script which will set text of field's tooltip based on description of field in attribute customization form.



To make this possible only add following script to OnLoad event handler of entity form:

SetTooltips = function()
{
var request = "" +
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
GenerateAuthenticationHeader() +
" <soap:Body>" +
" <Execute xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +
" <Request xsi:type=\"RetrieveEntityRequest\">" +
" <RetrieveAsIfPublished>true</RetrieveAsIfPublished>" +
" <EntityItems>IncludeAttributes</EntityItems>" +
" <LogicalName>" + crmForm.ObjectTypeName + "</LogicalName>" +
" </Request>" +
" </Execute>" +
" </soap:Body>" +
"</soap:Envelope>";

var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

xmlHttpRequest.Open("POST", "/mscrmservices/2007/MetadataService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/Execute");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", request.length);
xmlHttpRequest.send(request);

var result = xmlHttpRequest.responseXML;


for(var i = 0; i < crmForm.all.length; i++)
if(crmForm.all[i].title != null && crmForm.all[i].title != 'undefined')
{
var fieldName = crmForm.all[i].id;

var desc = result.selectSingleNode("//EntityMetadata/Attributes/Attribute[LogicalName='" + fieldName + "']/Description/UserLocLabel/Label");
try
{
if(desc != null)
{
crmForm.all[fieldName + '_c'].title = desc.nodeTypedValue;
crmForm.all[fieldName + '_d'].title = desc.nodeTypedValue;
}
}
catch(e) {}
}
}

SetTooltips();


And the result you can see here:

10 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Somehow it didn't worked for me. I pasted the code in the Contact entity's Form onLoad event, enabled it and published the changes....

    ReplyDelete
  3. I could not get this to work either, perhaps we need a txt file with the code in it? When I used it it actually caused a embeded IFRAME to fail as well.

    ReplyDelete
  4. I haven't tried it to use with Iframes but I think it must work. Actually no text files is needed.

    ReplyDelete
  5. Hello Andriy,

    Recently I posted an alternative way of displaying tooltips. It requires a custom entity in which you can store the tooltips per entity/attribute. See this post: http://crmxpg.nl/wp/2010/10/22/display-tooltips-for-fields-in-ms-crm-part-2/

    I'm planning to expand this function to support multiple languages.

    Cheers.

    ReplyDelete
  6. Hello.

    Awesome! To make easier developing the solution which will support multiple languages you can check stunnware blog for a sample. Michael has solution which can be used.

    ReplyDelete
  7. This works great for me if I'm System Administrator og System Customizer. Not if I'm just Sales Person. Any idea on what to set in User Rights?

    ReplyDelete
  8. Hello. Try to give to Sales Person Role Permission to read Attributes at Customization tab.

    ReplyDelete
  9. Hi Andriy,
    Your script is exactly what I need for CRM 2011.
    Do you have a version of this that works for CRM 2011?

    ReplyDelete