Saturday, January 16, 2010

Setting attribute description as tooltip for fields

If you want to give the end user some information about the meaning of a field, one way to do so is using tooltips. a33ik wrote a javascript that retrieves the field descriptions from the entity definition and attaches them as a tooltip.

Now it finally makes sense to fill out the description field:
DescriptionField   

Here’s the script to be placed OnLoad of the 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 here is the result:ToolTips

Enlarge Attachment Size

Here are some simple steps to enlarge the max attachment size in Microsoft Dynamics  CRM.

1. Open the web.config of MS CRM
2. Find the line  <httpRuntime executionTimeout="300" maxRequestLength="8192" />
3. Change the value of “8192” to the desired max size in kilobytes. For this example it is set to “20480”.
4. Open CRM, go to System settings. You can now change the max size of attachments in the E-mail tab.

AttachmentSize

User friendly information messages

In some situations you might want to provide the end user with some information. A way to do that is using alerts. But that is not always as user friendly as you might want.

Tanguy wrote a javascript for more user friendly messages that was improved by Si. I thought I could improve it a little further by making it more flexible and share the code with you.

Place the next function on the OnLoad event of the CRM form.

Notification = function(sNotification) {
    var elementId = 'Notifications';
    var id = 'divMessage';
    var src = document.getElementById(elementId);

    if ((sNotification == null) || (sNotification == "")) {
        src.style.display = 'none';
    }
    else {
        var newcontent = document.createElement("span");
        newcontent.id = id;

        newcontent.innerHTML = "<table><tr><td><img src='/_imgs/ico/16_info.gif' /></td><td valign='top'>" + sNotification + "</td></tr></table>";
        src.style.display = "";

        var previous = src.firstChild;
        if (previous == null || previous.attributes['id'].nodeValue != id) {
            if (src.childNodes.length == 0)
                src.appendChild(newcontent);
            else
                src.insertBefore(newcontent, src.firstChild);
        }
        else
            src.replaceChild(newcontent, previous);
    }
}

Now you can call this function from anywhere on the form. For example on the exchange of accountnumber:

if(crmForm.all.accountnumber.DataValue != null)
{
    Notification("Accountnumber = "  + crmForm.all.accountnumber.DataValue);
}
else
{
    Notification();
}

Which results in the next information message:

InformationMessage