phprightnow-crm

Oracle Service Cloud File Attachment upload/view


Im new to service cloud and have been tasked with figuring out a way to save file attachments to custom objects. I believe what I have thus far accomplishes just that as I can see the new records being created in my object. My next step is to figure out a way to find uploaded files given an ID and display a clickable link that will open in a new tab to display the uploaded file's contents. (similar to the stock FileListDisplay widget). According to the Connect PHP API documentation, no ROQL query or fetch() returns any file attachment data. How would I go about displaying uploaded files?

Code so far (shortened for brevity)

View:

   <form action="https://test.rightnowdemo.com/app/Upload2/uploadcontroller2" method="post" enctype="multipart/form-data" />
      <div>
     <h3>Select a .txt file only:</h3><br />
       <input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
       <label for="userfile">Select a file:</label>
       <input type="file" class="form-control" name="userfile" /><br>
       <input type="submit" class="nybeButton" value="Send" />
     </div>
  </form> 

controller:

function store_attachment($FileName, $FileLocation, $FileType)
{


    $MyDocument = new RightNow\Connect\v1_1\UPLOAD\Documents();
    $MyDocument->FileAttachments = new RightNow\Connect\v1_1\FileAttachmentArray();
    $MyDocument->UserName = "Test user";
    $fattach = new RightNow\Connect\v1_1\FileAttachment();

    $fattach->ContentType = $FileType;

    $file = $FileLocation;
    $fattach->setFile($file);
    $fattach->FileName = $FileName;
    $MyDocument->FileAttachments[] = $fattach;
    //\RightNow\Libraries\AbuseDetection::check();
    $MyDocument->save();
}
$FileName = $_FILES["userfile"]["name"];
$FileLocation = get_cfg_var('upload_tmp_dir') . "/" . $_FILES["userfile"]["name"];
$FileType = $_FILES["userfile"]["type"];
store_attachment($FileName, $FileLocation, $FileType);

Solution

  • A similar question was asked on the OSvC communities lately. Generally, there's no way to get the file data through Connect for PHP; you can only set through Connect (we used to have some scripts to get around this with PHP, but I believe that Prod Dev has closed all PHP methods to access the file servers). You must use the SOAP API to retrieve the files through your code.

    However, if you just want to display a link on Customer Portal to the file, then you can follow the following format, as per Arun's answer in the communities:

    <a href="/ci/fattach/get/<?php echo $file->ID; ?>/<?php echo $file->CreatedTime; ?>/filename/<?php echo urlencode($file->FileName); ?>">Download File</a>