Thursday, September 23, 2010

MS CRM 3.0: Set email default font

 Hello again,

 I've been working recently on a way to set the default font within the CRM 3.0 email message edit field. The default font is Tahoma 10, however, a different font was needed (in this code, Arial). From the 3 specified font families, Internet Explorer will use the first one available on the system. As those 3 fonts are rather mundane, it's unlikely they will not be available. Nonetheless, should you decide to use a special font, make sure it's available on each system the code will run on.

 My method uses the "subject" field in order to determine if the form is used to view an email or to edit a new email / a reply email / a forwarded email. It was enough to achieve my goal on this task. The thing about IFrames is that you're not able to determine exactly when they've finished loading. I have attempted to use the onLoad event of the IFrame, however that lead to nothing viable.

 Thus, it came down to the same old workaround of using a timer to make a check against the readyState of the IFrame object.

 The amount of time set on the timers is really up to you - it was enough for my objectives, you might need a quicker / slower time between checks. If the IFrame is not yet loaded, the method sets another timer to make another call attempt later. That, off course, only if the email is in edit mode (the "subject" field is editable).

 Please note that this is not tested on CRM 4.0. I am not aware if the IFrame structure is different than that used in 3.0, therefore I cannot claim it might work on 4.0 as well. ;-)



 Cheers!



Here is the code (in my case, it was placed within the onLoad code of the Email entity):

---------------------
function forceFontFormat(parentNode, fontFamily, fontSizeInPoints, fontSizeInIndex)
{
    if(parentNode)
    {
        try
        {
         var tagName = '';
         if(parentNode.tagName)
         tagName = parentNode.tagName.toLowerCase();
         switch (tagName)
         {
         case 'div':
         case 'p':
         case 'span':
            parentNode.style.fontFamily = fontFamily;
                 parentNode.style.fontSize = fontSizeInPoints + 'pt';
         break;
         case 'font':
         parentNode.setAttribute('face', fontFamily);
         parentNode.setAttribute('size', fontSizeInIndex);
         break;
         }
        }
        catch (err) { }
        if((parentNode.childNodes) && (parentNode.childNodes.length > 0))
        {
            for(var i = 0; i < parentNode.childNodes.length; i++)
            {
                forceFontFormat(parentNode.childNodes[i], fontFamily, fontSizeInPoints, fontSizeInIndex);
            }
        }
    }
}


function forceDefaultCrm30EmailFontFamily()
{
    try
    {
        var isEditable = false;
        try
        {
            var emailSubject = document.getElementById("subject");
            isEditable = emailSubject.isContentEditable;
        }
        catch (secondErr) {}
        if ( isEditable == true)
        {
            var iframe = document.getElementById("descriptionIFrame");
            var forceFont = false;
            try
            {
                if(iframe.contentWindow.document.readyState == "complete")
                    forceFont = true;
            }
            catch (thirdErr)
            {
                forceFont = false;
            }
            if(forceFont == true)
            {
                forceFontFormat(iframe.contentWindow.document.body, 'Arial', '10', '2');
            }
            else
            {
                setTimeout(forceDefaultCrm30EmailFontFamily, 1000);
            }
        }
    }
    catch (firstErr)
    {
        /*alert("CRM Development\n\nThis warning does not affect your work, please ignore it.\nThank you!\n\n\nError message (see below):\n\n" + err.description);*/
    }
}


// Method call
setTimeout(forceDefaultCrm30EmailFontFamily, 1000);

0 comentarii: