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