You're almost there! Please answer a few more questions for access to the Applications content. Complete registration
Interested in joining? Complete your registration by providing Areas of Interest here. Register

Fileattachment to custom object

Accepted answer
6
Views
6
Comments
JoshL
JoshL Green Ribbon
edited Sep 20, 2016 10:49PM in General Functional Discussions 6 comments

Best Answer

  • Bastiaan van der Kooij
    edited Oct 2, 2015 7:52AM Accepted Answer

    yeah, you're mixing up a couple of thing. Below some code I wrote for an Incident upload, there you can see the proper setup;

    // Controller
        if ($_FILES["file"] AND $_FILES["file"]["error"] == 0) {
              $FileName = $_FILES["file"]["name"];
              $FileLocation = $_FILES["file"]["tmp_name"];
              $FileType = $_FILES["file"]["type"];
              $FileResult = $this->CI->model('custom/IncidentModel')->store_attachment($this->IncidentID, $FileName, $FileLocation, $FileType);
        }

    // Model
        function store_attachment($IncidentID, $FileName, $FileLocation, $FileType)
        {
            $incident = RNCPHP\Incident::fetch($IncidentID);

            $incident->FileAttachments = new RNCPHP\FileAttachmentIncidentArray();
            $fattach = new RNCPHP\FileAttachmentIncident();
            $fattach->ContentType = $FileType;
            $file = $FileLocation;
            $fattach->setFile($file);
            $fattach->FileName = $FileName;
            $incident->FileAttachments[] = $fattach;

            \RightNow\Libraries\AbuseDetection::check();
            $incident->save();
        }

Answers

  • Bastiaan van der Kooij
    edited Sep 30, 2015 1:57PM

    hey John,

    that is a tough challenge for your first day at work! There is nothing OOTB to support custom objects, let alone file attachements. All there is, is dealing with incidents.

    So to give you a bit of an overview of the steps you have to take;

    1. Create a custom model for your CO to support CRUD operations and File uploading (using ConnectAPI)
    2. Create a view that uses your model to display CO items
    3. Create a new view with a form with the fields you like to be populated and drop in a file input for your attachement, the view should submit to the custom controller in the next step using Ajax
    4. Create a custom controller that is the receiver of your view and process the data by calling the functions in your model
    5. Say something nice to the user like "thank you"

    If you don't want to use ajax you can submit into a thank you page and have that page pick up the data by a custom widget or something like that.

    probably quite some looking up to do in the documentation / forum, but at least this should give you some general guidelines.

    Bastiaan

  • JoshL
    JoshL Green Ribbon
    edited Oct 1, 2015 5:39PM
    Bastiaan van der Kooij said:

    hey John,

    that is a tough challenge for your first day at work! There is nothing OOTB to support custom objects, let alone file attachements. All there is, is dealing with incidents.

    So to give you a bit of an overview of the steps you have to take;

    1. Create a custom model for your CO to support CRUD operations and File uploading (using ConnectAPI)
    2. Create a view that uses your model to display CO items
    3. Create a new view with a form with the fields you like to be populated and drop in a file input for your attachement, the view should submit to the custom controller in the next step using Ajax
    4. Create a custom controller that is the receiver of your view and process the data by calling the functions in your model
    5. Say something nice to the user like "thank you"

    If you don't want to use ajax you can submit into a thank you page and have that page pick up the data by a custom widget or something like that.

    probably quite some looking up to do in the documentation / forum, but at least this should give you some general guidelines.

    Bastiaan

    View original

    Thanks for your reply Bastiaan,

    I took most of what you said and was able to create a form which submits to a PHP. So far, I've been successful in connecting to my object and inserting a value to field. Now I'm stuck with getting a file attachment uploaded. I'm obviously doing something very wrong. Below is my code thus far..

       require_once(get_cfg_var('doc_root')."/ConnectPHP/Connect_init.php");

        initConnectAPI();


    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $file = new RightNow\Connect\v1_1\UPLOAD\Documents();<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// $file->CreatedTime = 12345;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fattach = new RNCPHP\FileAttachmentArray();<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fattach->ContentType = "application/pdf";<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $uploaddir = get_cfg_var(&#39;upload_tmp_dir&#39;) . &#39;/&#39; ;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $uploadfile = $uploaddir . basename($_FILES[&#39;userfile&#39;][&#39;name&#39;]);<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fattach->$uploadfile;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fattach->FileName= $_FILES[&#39;userfile&#39;][&#39;name&#39;];<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $fattach->Name= $_FILES[&#39;userfile&#39;][&#39;name&#39;];<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $file->FileAttachments[] = $fattach;&nbsp;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $file->save();

  • Bastiaan van der Kooij
    edited Oct 2, 2015 3:04PM

    Incident is a standard object. So in your scenario you're using a custom object, the Fileattachements array is a subobject of your document CO, just like any other fields you would like to populate. So you would end up with something like;

    $MyDocument = new RightNow\Connect\v1_1\UPLOAD\Documents();
    $MyDocument->Name = "Whatever Name";
    $MyDocument->FileAttachments = new RNCPHP\FileAttachmentArray();

    // rest of the file attachment stuff & Abusedetection

    $MyDocuments->save();

     

    Bastiaan

     

  • Bastiaan van der Kooij
    edited Oct 5, 2015 7:44AM

    please take a better look at the code I provided, as all the answers are there.

  • Rodrigo Utrera
    edited Aug 10, 2016 3:27PM
    Josh L said:

    Hi again Bastiaan,

    We are attempting to view the uploaded files. Reading through the documentation, I see that "No file attachment data is returned within any ROQL method or fetch()". What are our options than for finding and displaying uploaded files? I'm looking for functionality similar to the FileListDisplay widget where given an ID, displays a list of clickable files which open to display the uploaded file's contents.

    View original

    Hi Josh, I'm trying get and display uploaded files on the portal. were you able to do it?

    Thanks!