addEmailOptions

 

I am working on a custom compose, in the Sidecar Email module we can use this.addEmailOptions(options); to set some defaults on an email

I successfully added a body and subject as well as relating the email to the module I am starting from, but I can't seem to set the To, and Cc fields

 

It looks like I need to pass a to_collection and cc_collection but I can't figure out how...

The collection appears to be a set of pairs: Bean, EmailAddressBean

where Bean is a Contact, Lead, User... depending on the situation.

But I don't always have a Bean.

 

A person we never heard of before can send a request to a case queue (our Customer Service uses Cases) so all I have is an email address.

Sometimes I need to include an internal company address that is not stored in Sugar and again, I don't have a Bean that it relates to.

 

How do I include these in my custom compose?

And even if I have a bean, how do I structure my $to_collection when passing it from API?

 

thanks,

FrancescaS

 

  • Hi Francesca,

    Extend the Email module compose.js, Find the below function, through that you can add the to_address and cc_address using this code,

    _populateForCases: function(relatedModel) {
    var config = app.metadata.getConfig(),
    keyMacro = '%1',
    caseMacro = config.inboundEmailCaseSubjectMacro,
    subject = caseMacro + ' ' + relatedModel.get('name');

    subject = subject.replace(keyMacro, relatedModel.get('case_number'));
    this.model.set('subject', subject);
    if (!this.isFieldPopulated('to_addresses')) {
    // no addresses, attempt to populate from contacts relationship
    var contacts = relatedModel.getRelatedCollection('contacts');

    contacts.fetch({
    relate: true,
    success: _.bind(function(data) {
    var toAddresses = _.map(data.models, function(model) {
    return {bean: model};
    }, this);

    this.model.set('to_addresses', toAddresses);
    }, this),
    fields: ['id', 'full_name', 'email']
    });
    }
    },

    If you dont have Bean, for any internal company address call the api and if u get the response append with to_address,

    _render:function()

    {

    PopulaeEmail();

    },

    PopulateEmail:function()

    {

    app.api.call('GET', app.api.buildURL('Contacts/getRelatedContacts?id=' + your_id), null, {
    success: function (data) {
    if (data) {
    _.each(data,function (val)
    {
    var to_addresses = [
    { email: data.emailAddress, name: data.contactName },
    ];
    self.model.get('to_addresses').add(to_addresses);

    });
    }

    }

  • Hi Francesca Shiekh 

    As far I debugged the related features the recipients (to, cc, bcc) are essentially instances of module EmailParticipants, take a look at modules/Emails/clients/base/views/compose-addressbook-list/compose-addressbook-list.js, method _render.

    So I believe somehow you can extends this feature and create a custom ?fake? module EmailParticipants the way you can fetch data from a bean which will not persist in database.

    I hope it can shed some light.

    Good luck!

    André Lopes
    Lampada Global
    Skype: andre.lampada
  • Thank you André Lopes you pointing me to the EmailParticipants module helped a lot.

    I found the structure of an EmailParticipant and from there inferred what to include in each element of the address

    I am a step closer, my API will build an array of recipients:

             $toAddresses[]=array(
                 'email_address'=>$cemail_addr,
                 'email_address_id' => $cemail_addr_id,
                 'parent_type'=> 'Users',
                 'parent_id'=>$cuser->id,
             }

    Then turn each one into a collection of EmailParticipants Beans. For example for the To collection:

          foreach($toAddresses as $address){
            $recipientTo = BeanFactory::newBean('EmailParticipants');
            foreach($address as $key=>$val){
              $recipientTo->$key = $val;
            }
            $to[]=$recipientTo;
          }
          $to_collection = $this->formatBeans($api, $args, $to);

    this gets passed back to my customemailaction field type which extends EmailactionField, together with a body and subject, 

          App.api.call('GET', url, '',{
            success: _.bind(function(o){
              self.addEmailOptions({
                to_collection: o.to_collection,
                cc_collection: o.cc_collection,
                name: o.name,
                description_html: o.description_html,
                description: o.description
              });

    And now I can see the addresses in the To field when the drawer opens!

    André Lopes Update:

    Unfortunately, though they "look" correct, I can't send the email because there are errors.

    "Error There was an internal server error. Please try again.

    I don't think my collection is formatted properly because if I delete the recipients and add one manually the email sends just fine.

    Thank you!

    FrancescaS

  • Thank you Ragukumar Marimuthu

    but I am working with the Sidecar version of Emails module and therefore the compose-email view, the compose is deprecated in the Sidecar version.

    FrancescaS

  • Thanks to Greg Levine I now know that I needed to provide the _link attribute for each model added to the collection.

    hence:

    App.api.call('GET', url, '',{
            success: _.bind(function(o){
              o.to_collection = _.map(o.to_collection, function(attrs) {
                return app.data.createBean('EmailParticipants', _.extend({_link: 'to'}, attrs));
              });
              o.cc_collection = _.map(o.cc_collection, function(attrs) {
                return app.data.createBean('EmailParticipants', _.extend({_link: 'cc'}, attrs));
              });
              self.addEmailOptions({
                to_collection: o.to_collection,
                cc_collection: o.cc_collection,
                name: o.name,
                description_html: o.description_html,
                description: o.description
              });

    Everything works now, no more errors.