Tested in Sugar 7.9
Since this is a drawer we're talking about, you can just pass the addresses as context.prepopulate.to_addresses
array and Sugar will prefill the to: input bar with them. If not deleted by the user they will be added to the to_collection
on save.
E.g. try this in your browser's JS console (while in Sugar):
App.drawer.open({
layout: 'compose',
context: {
create: true,
module: 'Emails',
prepopulate: {
to_addresses: [
{email: "example@whatever.fake"},
{email: "example2@whatever.fake", name: "Example Two"},
],
}
}
})
Tested in Sugar 7.11
In post-7.9 Emails module it seems necessary to use pre-existing Email-Address beans.
There is a utils function that will open a drawer and accepts to/cc/bcc arrays of Email-Address Bean collections (or arrays of attributes) in a data
parameter object.
openEmailCreateDrawer: function(layout, data, onClose)
So as you already have a to_collection from a different model you could do this:
App.utils.openEmailCreateDrawer(
'compose',
{
to: data.get("to_collection")
}
)
It is also possible to prefill the subject, the html body and the attachments using the same data
parameter.
e.g. using your variables this could look like this:
app.utils.openEmailCreateDrawer(
'compose-email',
{
to: data.get("to_collection"),
name: subject,
description_html: body,
attachments: attachments
}
)
This is based on the comment for the helper function responsible of parsing above function's data
in utils.js
:
/**
* Populates attributes on an email from the data provided.
*
* @param {Date.Bean} email
* @param {Object} data Attributes to set on the email.
* @param {string} [data.name] Sets the subject of the email.
* @param {string} [data.description_html] Sets the HTML body of the
* email.
* @param {Array|Data.BeanCollection|Data.Bean} [data.from_collection]
* The sender to add to the From field.
* @param {Array|Data.BeanCollection|Data.Bean} [data.from] A more
* convenient alternative name for the sender.
* @param {Array|Data.BeanCollection|Data.Bean} [data.to_collection]
* The recipients to add to the To field.
* @param {Array|Data.BeanCollection|Data.Bean} [data.to] A more
* convenient alternative name for the To recipients.
* @param {Array|Data.BeanCollection|Data.Bean} [data.cc_collection]
* The recipients to add to the CC field.
* @param {Array|Data.BeanCollection|Data.Bean} [data.cc] A more
* convenient alternative name for the CC recipients.
* @param {Array|Data.BeanCollection|Data.Bean} [data.bcc_collection]
* The recipients to add to the BCC field.
* @param {Array|Data.BeanCollection|Data.Bean} [data.bcc] A more
* convenient alternative name for the BCC recipients.
* @param {Array|Data.BeanCollection|Data.Bean} [data.attachments_collection]
* The attachments to attach to the email.
* @param {Array|Data.BeanCollection|Data.Bean} [data.attachments] A
* more convenient alternative name for the attachments.
* @param {Data.Bean} [data.related] A convenience for relating a
* parent record to the email.
* @return {Object} Any key-values pairs that could not be assigned are
* returned so the caller can decide what to do with them.
*/
function prepopulateEmailForCreate(email, data) {