• dgrad
  • NEWBIE
  • 0 Points
  • Member since 2008

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Here's my full scontrol code to link a file as an attachment without uploading the file onto Salesforce. It saves SF storage space and upload/download time for large files. Most importantly, it relates documents to the appropriate Salesforce object and viewed instantaneously.
 
To use the code, you need to add your own way of parsing the URL for the parentID (eid) parameter, and base64 encoding (we have these in our common library which we bring in using !INCLUDE). Other than that it's good to go and will attach to any parent having attachments. Create a link that calls the scontrol. Here's a sample link we call Link Attachment:
 
Code:
{!URLFOR( $SControl.MUSW__LinkAttachment  , MUSW__Permit__c.Id, [debug= $User.MUSW__Debug__c, 
stepthrough= $User.MUSW__Step_Through__c ])}
 
The Case Detachifier app on the appExchange gave me the idea for the placeholder attachment. It's seemless to the user, except if the file was moved or removed in the user's network (link is effectively broken). Files added under the Notes & Attachments related list have the word (LINK) after the filename to show it is not stored on Salesforce.
 
We still cannot find a way to customize the Notes and Attachment section to add or overwrite their "Attach File" buttons so it remains as a link on the detail page instead of in the related list. Next step for us is having a tighter integration with a file repository/management with choices of folders, etc.
 
James Wikkerink
Municipal Software
 
Code:
<html>
<head>
 <title>Link Attachment</title>
 <link href="/dCSS/Theme2/default/common.css" type="text/css" media="handheld,print,projection,screen,tty,tv" rel="stylesheet">
    <script type="text/javascript" src="/js/functions.js"></script>
    <script src="/soap/ajax/8.0/connection.js"></script>
    {!INCLUDE($SControl.MUSW__lgmCommon)}
    <script id="clientEventHandlersJS" language="javascript">
    <!--  
var b64 = new lgmBase64();

function setup() {

 lgm.href.getParameters();
 
}
function submit2(fullFileName) {
var fileName = "";
var placeholder = "";
var nameSuffix = " (LINK)";
 
 //get just file name
 if (fullFileName.indexOf('/') > -1)
        fileName = fullFileName.substring(fullFileName.lastIndexOf('/')+1,fullFileName.length);
    else
        fileName = fullFileName.substring(fullFileName.lastIndexOf('\\')+1,fullFileName.length);
 
 //make filename URL friendly
 URLfullFileName = "file:\/\/" + fullFileName.replace(/\\/g,"/");
 
 //confirm if want to link to a local file
 if ((fullFileName.indexOf("C:\\") > -1)  || (fullFileName.indexOf("D:\\") > -1)) {
 
  var x = window.confirm("Are you sure you want to link to a local file that no other users can access—"); 
  if (!x)
   return true;
  else
   nameSuffix = " (LOCAL LINK)";
 }
 
 if (URLfullFileName.indexOf("'") > -1 ) {
  alert("File names or paths containing single quotes (') are not allowed. Please rename file name or path.");
 } else {
 
  placeholder = "<html><head><meta http-equiv='refresh' content='1;url=" + URLfullFileName + "'></head>"+
     "<body><br><center>Opening file <a href='" + URLfullFileName + "'>" + URLfullFileName + "</a> <br>" +
     "Click link if file does not automatically open.</center></body></html>";
  
  var attachmentRecord = new sforce.SObject("Attachment"); 
  attachmentRecord["ParentId"] = lgm.href.param["eid"];
  attachmentRecord["Name"] = fileName + nameSuffix;
  attachmentRecord["ContentType"] = "text/html";
  attachmentRecord["IsPrivate"] = false;
  attachmentRecord["Body"] =b64.encode(placeholder);// b64.encode(documentContent)
  
  
  try {
   var error = sforce.connection.create([attachmentRecord]);
   if (error.toString().indexOf("errors") != -1 ) {
    throw ("Document Save Error: " + error.toString());
   }
   
   opener.location.reload();
   if (lgm.debug.flag != true ) { //see debug statements remaining
    window.close();
   }
   
  }
  catch (err) {
   addMessage(err.toString());
   retval = false;
  }
 }
}
function isUrl(s) {
 var regexp = /(ftp|http|https|file):\/\/(\w+:{0,1}\w*@)–(\S+)(:[0-9]+)˜(\/|\/([\w#!:.™+=&%@!\-\/]))?/
 return regexp.test(s);
}

//-->
</script>
</head>

<body onload="setup()">
  <form id = "myform" name = "myform">
   <div class="lookup">
    <div class="bPageTitle">
     <div class="ptBody primaryPalette">
      <div class="content">
       <img src="/s.gif" alt="Lookup"  class="pageTitleIcon">
       <h1>Link Attachment</h1>
      </div>
     </div>
    </div>
    <table cellspacing = 0 cellpadding = 0 border = 0>
     <tr>
      <td width = 5>&nbsp;</td>
      <td width = 100% style = "padding:2px 10px 2px 2px; font-size: 9pt; font-weight: bold; color:#333;">Select the File</td>
      
      <td width = 5>&nbsp;</td>
     </tr>
     <tr>
      <td width = 5>&nbsp;</td>
      <td width = 100% style = "padding:2px 10px 2px 2px; font-size: 9pt; color:#333;">Ensure file path is accessible to entire organization.
      <br>Note: Do not move or rename file after linking
      <td width = 5>&nbsp;</td>
     </tr>
     <tr>
      <td width = 5>&nbsp;</td>
      <td width = 100%>&nbsp;</td>
      <td width = 5>&nbsp;</td>
     </tr>
     <tr>
      <td>&nbsp;</td>
      <td align = left><input  id="file" name="file" size="35" title="Type the path of the file or click the Browse button to find the file." type="file" /></td>
      <td>&nbsp;</td>
     </tr> 
     <tr>
      <td>&nbsp;</td>
      <td align = center>&nbsp;</td>
      <td>&nbsp;</td>
     </tr>
    
     <br>
     <tr>
      <td>&nbsp;</td>
      <td align = center>
       <div class="pBody" align = center>
        <input id = "submit" Name = "submit" class="btn" type=button value="Submit" onClick="submit2(myform.file.value);" disable = "true">
        <input id = "cancel" Name = "Cancel" class="btn" type=button value="Cancel" onClick="window.close();" disable = "true">     
       &nbsp;&nbsp;&nbsp;&nbsp;
       </div> 
       
      </td>
      <td>&nbsp;</td>
     </tr>
    </table>       
   </div>
   <DIV id="divDebug"></DIV> 
  </form> 
 </body>
</html>