CRM Blog

Converting A CRM Field To A Browse-able File Path Field

by Danny Varghese 03.24.08

One of the requirements for a client was the ability to “attach” a document to a record in CRM without using CRM built in attachments/notes nor actually attaching the document but rather creating a link to the document.  This is because, when attaching any files using attachments/notes, those attachments are stored on the server and the client already had these files stored in a shared directory on a server.  Recreating these documents within the CRM db would have just been overhead (the data would not be needed when disconnected) and would not have allowed easy updating of the shared document. 

To curb their apprehensions, a senior manager of ours, Bryan Van Antwerp created a customization that allows a field to be converted to a browse-able file path field.  This allowed users to browse to a certain file on the shared network, and “attach” it to a custom entity.  Then whenever the user wants access to that document, he/she would simply open the record up and double click on the path and the document will use Window’s functionality to open/save the document.

The two functions below, written by Bryan Van Antwerp, illustrates this example:

function convertFilePathToFileInputType(fieldName)

{          //variable initialization         var field = null;                                                      var fieldValue = null;                                                        var fieldTabIndex = "";         var fieldMaxLength = "";         var fieldReq = "";         var fieldColSpan = "";                field = document.getElementById(fieldName);         fieldValue = field.value;         fieldTabIndex = field.tabIndex;         fieldMaxLength = field.maxLength;         fieldReq = field.req;         fieldColSpan = document.getElementById(fieldName + '_d').colSpan;       

  if(field.value.length > 0)

  {                   //have value so show text and remove button                                 field.insertAdjacentHTML('afterEnd','<span id="' + fieldName + '_rBSpace">&nbsp;</span><input width=0 height=0 type="hidden" value="Remove" id="' + fieldName + '_removeButton" onClick="removeFilePath(\''+fieldName+'\');"></td>');                   document.getElementById(fieldName).style.width = "100%";             

     document.getElementById(fieldName+'_removeButton').style.width = "0%";

     document.getElementById(fieldName+'_rBSpace').style.width = '0%';                   field.readOnly = true;                   field.className = 'txt ro';                   field.attachEvent('onclick', function(){try{var win = window.open(document.getElementById(fieldName).value);}catch(e){alert("Unable to open file.");}});                     }         else         {                   //do not have value so show file input type                   field.insertAdjacentHTML('afterEnd','<td colspan=' + fieldColSpan + ' id="' + fieldName + '_d"><input type="file" id="' + fieldName + '" tabindex="' + fieldTabIndex + '" maxlength="' + fieldMaxLength + '" class="txt" value="' + fieldValue + '" req="' + fieldReq + '"></td>');             

     document.getElementById(fieldName).style.width = '100%';

     field.parentNode.removeChild(field);         } } function removeFilePath(fieldName){         document.getElementById(fieldName).value="";         var field = document.getElementById(fieldName+"_removeButton");         field.parentNode.removeChild(field);          field = document.getElementById(fieldName+"_rBSpace");         field.parentNode.removeChild(field);      

  convertFilePathToFileInputType(fieldName);     

}

The first function converts the actual field to a browse-able field if there’s no data that exists.  If there is data, the “Remove” button is shown (per requirement of the business), and the field is disabled - also note that there is an event that is attached that when the field is clicked it will open the link.  The second function is called when the “Remove” button is clicked and it removes any values in that field.

Filed under:

Comments

# Kostya Afendikov said on February 24, 2009 03:17 AM:

I tried this example, and I get field with "Browse" button. But how can I attach some file? Thanks

# Danny Varghese said on February 24, 2009 10:00 AM:

Hi Kostya,

This example was for CRM 3.0 so I'm not sure if it works for CRM 4.0, but with 3.0 you should just be able to select the file and it will automatically attach the file.

Then to access the file, all you'd have to do is double click on the file and it will open with the appropriate editor.

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Security Check
Please answer the simple math problem below.

(required)