Javascript 7.11 getting a bean

I am facing issue to instantiate a bean in the version 7.11 order to get data from the Documents module in order to attach a PDF an existing document to an email, via opening a drawer.

I might be missing something here. I am putting this issue here once I posted a similar one in the community forum and no one answered so far.

The function is triggered after a pressed button in the edit view of a custom module.

I have tried some different approachs as

Approach 1:
var document1;
//document1 = app.data.createBean("Documents", {id:document_id}); // same effect
document1 = SUGAR.App.data.createBean('Documents', {id:document_id});
var loaded_document;
document1.fetch();
console.log(document1.toJSON()); // got just the ID

APPROACH 2:

var document1 = app.data.createBean("Documents");
document1.fetch({
     filter:[{id:document_id}],
});
console.log(document1.toJSON()); // get none

APPROACH 3:

var loaded_document;
var document1 = SUGAR.App.data.createBean('Documents', {id:document_id});

var request = document1.fetch();

request.xhr.done(function () {
    console.log( "entered" );
    console.log( document1.get('name') ); //undefined
    loaded_document = document1; // loaded_document remains undefined
});

APPROACH 4:

document2 = SUGAR.App.data.createBean('Accounts', {'id': '9f77f918-22e0-70b7-a099-55d38d970d20'});
requestA = document2.fetch();
var datatest;
requestA.xhr.done({
        success: function(data){
        console.log("document2 in success --- ");
        console.log(data);
       loaded2 = document2;
       datatest = data;
    }
});

console.log(document2.toJSON());// just the ID

APPROACH 5:

With this approach, the result comes is called afterwards. How could I assure this runs in the right time, if this is the solution?

var loaded2,

url = app.api.buildURL('Documents/'+document_id);

self = this;

app.api.call('GET', url, null, {

     success: _.bind( function (data){
         console.log("data get");
         console.log(data);
         loaded2 = data; // after email drawer is charged, it is called
        document1 = SUGAR.App.data.createBean('Documents', data); // is this correct?
    }, this),

    error:_.bind(function(o){

         console.log("Error retrieving Document" + o);

    }, this),

}, {async: false}); // the async: false is essential in this case.

console.log(loaded2); // ok, but still not populating as expected

After this, in compose-email.js I am trying to send it as an attachment:

APPROACH 1:

I get the object and populate prepopulate:

prepopulate.attachments.push(document1);

APPROACH 2:

Trigger the document attachment event

this.context.trigger('email_attachments:document', loaded_document);

Could any of you advise in each case?

Thank you in advance

  • Concerning loading objects, I kept using the APPROACH 5.

    Concerning the generation of attachments (from an existing Document) I did like the following when generating the drawer operation:

    <code>

    // Considering document_id and the subject)in, bodyHtml, body vars and others already populated

    prepopulate = ({

       subject : subject_in,
       html_body : bodyHtml,
       text_body : body,
       // attachment: attachment_pdf,
       // for working with version 7.10.[0.1.2]: added to be compatible -
       // 2018-02-28
       name : subject_in,
       description_html : bodyHtml || body,
       description : body,
       attachments : [],

    });

    var url = app.api.buildURL('Documents/'+document_id),
    document1 = app.data.createBean('Documents', {id:document_id});


    app.api.call('GET', url, null, {

       success: _.bind( function (data){
             console.log("data get");
             document1 = SUGAR.App.data.createBean('Documents', data);
          }, this),

       error:_.bind(function(o){
            console.log("Error retrieving Document" + o);
       }, this),

    }, {async: false});

    var bean = app.data.createBean('Notes', {
       _link: 'attachments',
       upload_id: document1.get('document_revision_id'),
       //name: this.model.get('name'),
       filename: this.model.get('name'),
       name: document1.get('filename') || document1.get('name'),
       filename: document1.get('filename') || document1.get('name'),
       file_mime_type: document1.get('latest_revision_file_mime_type'),
       file_size: document1.get('latest_revision_file_size'),
       file_ext: document1.get('latest_revision_file_ext'),
       file_source: 'DocumentRevisions'
    });

    prepopulate.attachments.push(bean);

    app.utils.openEmailCreateDrawer('compose-email',
       prepopulate,
       _.bind(
           function(context, model) {
               // Reload the BWC window to update subpanels.
               console.log("Email Composer launched")
           }, this));

    </code>

  • Again, concerning attachments created sent by functions from

    shareaction.js

    I used the following for the SugarCRM versions 7.10 and 7.11:

    ** _setShareOptions is deprecated, so:

    ...

    emailOptionSubject: function() {

       var shareParams = this._getShareParams(),

            document_to_attach = <get your document ready from somewhere>,
            subject = this.shareTplSubject(shareParams);

    this.addEmailOptions({

        // Usual vars for email after 7.10.0 - 2018-02-28
         subject: subject,
         // to be solved in the Sidecar Email

          subject: subject,
         obj_document: document_to_attach,

    });

    ...

    --===============================--

    In a custom compose-email.js

    initialize:  function(options) {

    ...

            var bean_attachment;

            

            if(options.context.attributes.obj_document){ 

                    bean_attachment = this.generate_attachment(options.context.attributes.obj_document);

                    // Fix subject (it is blank without it - for sure there is a best way to do so)
                    options.context.attributes.model.attributes.name = options.context.attributes.subject;

             }

    ...

            this._super('initialize', [options]);

    ...

            // Added in 2018-03-16 to attach the attachment - test it before evocking
            if(options.context.attributes.obj_document){
                      this.model.get("attachments_collection").add(bean_attachment, {merge: true});
              }

    }