General Technical Discussions for Service

Get Involved. Join the Conversation.

Topic

    Ian Oliver
    Issue with imported attachments over the API
    Topic posted April 18, 2018 by Ian OliverRed Ribbon: 250+ Points 
    57 Views, 1 Comment
    Title:
    Issue with imported attachments over the API
    Content:

    Hi All

    Ok so we have an integration set up with an external REST API where we look for attachments in the other system and pull them into Service Cloud.

    This is working great but we are having some slight issues and not sure if its an encoding/converting issue ?  The attachments get created perfectly but some will not open, for example:

    Text files .txt - No problems

    Word Docs .docx - When you click on 'Open' from the incident workspace it tries to open the document but word reports there is an issue with the content.  When you 'Download' it to your desktop it opens fine. 

    PDF files .pdf - wont open either way Adobe just reports an issue with the file content.

    This is what we are using to create them:

                $fattach = new RNCPHP\FileAttachmentIncident();
                $fattach->ContentType = $contentType;
                $fp = $fattach->makeFile();
                fwrite( $fp, $data);
                fclose( $fp );
                $fattach->FileName = $fileName;
                $fattach->Name = substr($fileName, 0, 40);
                $this->incident->FileAttachments[] = $fattach;
                $ret = $this->incident->save(RNCPHP\RNObject::SuppressAll);
                RNCPHP\ConnectAPI::commit();
     
    We are pulling the right content type and I believe the source data is base64.
     
    Not sure what the problem is here, does anyone have any ideas ?
     
    Thanks!
     
    Ian
    Version:
    17d
    Code Snippet:

    Comment

     

    • Rajan Davis

      Hi Ian,



      I have ran into similar issues with .zip files, Ruby, and the REST API.



      My hypothesis is that certain file types do not tend to work "as expected" with base64 encoding.



       



      At least for Ruby, when a file is being 'read', it will be converted as a string, so when the base64 encoding happens, it encodes a string.



      This works okay for text documents, but not for zip files as there are multiple levels of files that may need to be read.





       



      So a way to get around this that is not a very standard way but will use standard functionality is to POST your files to the 'ci/fattach/upload' portal endpoint. This will return the following JSON if successful:




      {
      error:0,
      name:"filename.PNG",
      size:32859,
      tmp_name:"NYfmoqyour-interface.custhelp.com",
      type:"image/png"
      }


      Then, with the above name and tmp_name values above, you can save your file attachment to an incident with the following modifications to your code block above:




      $fattach = new RNCPHP\FileAttachmentIncident();

      // this method will set the file to be what was saved in the
      // temporary file system
      $fattach->setFile('/tmp/'.$data['file_upload_tmp_name']);

      // Sets the file name from what was called back from the file upload
      $fattach->FileName = $data['file_upload_name'];
      $this->incident->FileAttachments[] = $fattach;
      $ret = $this->incident->save(RNCPHP\RNObject::SuppressAll);
      RNCPHP\ConnectAPI::commit();


       



      I haven't tested the above code, but it should work; I have a more detail documented here. This should get around the base64 encoding issues... hope this helps!



      Kind Regards,



      Rajan