Search code examples
classattachmentsugarcrm

SugarCRM: Create NOTE with Attachment without SOAP?


I've got this custom button on Lead Editview that when clicked on generates (via AJAX) an invoice number and a PDF bearing the same number.

In the next step, the routine uses SOAP to loopback to Sugar and creates a Note (along with the PDF as attachment).

My question is can I avoid this SOAP call and use some other internal mechanism / classes to do the same? Something along the lines of

$invoice = new Note();
$invoice->create(....);
...

Is this possible? I couldn't find any documentation anywhere... all roads seem to point to SOAP.


Solution

  • If your Ajax call is performing a db update/save operation, then you could look into using a after_save logic hook.

    EDIT: for eg: you could try out this code, have a look at the code in <sugar_root>/modules/Notes/Note.php

    $note = new Note();
    $note->modified_user_id = $current_user->id;
    $note->created_by = $current_user->id;
    $note->name = 'New';
    $note->parent_type = "Accounts";
    $note->parent_id = $bean->parent_id;
    $note->description = $bean->description;
    $note->save();
    

    As far as attachment goes, it's a bit tricky. Sugar expects the attachment to be a upload_file object. Have a look at the code in <sugar_root>/modules/Notes/controller.php the function action_save() and <sugar_root>/include/upload_file.php

    HACK: this is not the correct way but it works. With a slight modification to the code above and cunning use of the move function , you could make the attachment work. Sugar stores the attachments in cache/upload folder with the ID of the note created.

    $note->filename = "Yourfilename.txt" //your file name goes here
    $note->file_mime_type = "text/plain"  // your file's mime type goes here
    $new_note_id = $note->save();
    
    move(your_file_location, cache/upload/$new_note_id)
    //don't add a extension to cache/upload/$new_note_id
    

    HTH

    P.S: untested code