How do you prepopulate to recipient with email address in email compose 

How can we auto-populate to address with only email address.

There is sugar support link which show how can we add record in to address.

https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_9.1/Cookbook/Prepopulating_the_Compose_… 

But i want to address get populate with email address not with specific record.

I am opening compose email drawer on click of button where i want to auto-populate field of email compose.

To collection get populate with record as per above share link but i want to populate to address with simple email address.

Please check attached image for more details.

If we can add just email address in to collection of email recipient.

Thanks

Zeeshan shaikh 

Parents
  • Zeeshan,

    The clue to what you want to do is in the last line of your post I believe. When calling the drawer popout to bring up the Email Compose window you can pass in a new EmailParticipants bean and within the construct of that you can simply set the "email_address" and "email_address_id" fields to be what you need, although it does need to be an existing record in the email_addresses table so you have to create that first if it doesn't exist. I don't think it specifically needs to be linked to any particular bean though. For example, in your javascript file where you are calling to open the drawer, you want something like:

    [Edit: just for completeness, here is a mostly complete function call for opening a compose email pre-populated with a To: address that may or may not already exist. If it exists then the existing one is used as Sugar does not create duplicates in the email_addresses table, if not then a new entry is created and used]

    var self = this;
    var newEmailAddress = app.data.createBean('EmailAddresses',{email_address: 'your-email-address-here'});
    newEmailAddress.save();
    newEmailAddress.fetch({
      fields: ['id','email_address'],
      success:function(email){
        var emailAddressId = email.get('id');
        var emailAddress = email.get('email_address');
        var emailSubject = '';
        app.utils.openEmailCreateDrawer('compose-email', {
          to:[app.data.createBean('EmailParticipants', {
            email_address: emailAddress,
            email_address_id: emailAddressId
          })],
          related: self.model,
          skip_prepopulate_with_case: true,
          name: emailSubject
        });
      }
    });

    You can also add further items to the EmailParticipants bean if you are linking an existing record to use as the email source, such as "parent_type", "parent_id" etc. but you are asking to get round that  

    Also, I believe you can pass in multiple EmailParticipants items as the to_collection parameter. In this way you should be able to add existing Contact records alongside your new address, as per your image.

    For more info on what you can pass in check the EmailParticipants.php file in the module directory. All the params are public so you just pass in the ones you want to set.

    Hopefully this is what you need, sorry the reply is a bit late.

    Thanks,

    JH.

  • Zeeshan (and anyone else who wants to know this),

    I have found through additional testing that my solution above does not always populate the pop up email compose with a valid email address. It may be to do with the speed it is all running at but I have created a more robust solution which uses parts of my code alongside the quoted code you linked to from the Sugar docs.
    So, for completeness, here is how I create a pop up email compose from a click on a custom field in a Contact / Lead record that holds a valid email address (it is just a text field, the Javascript below formats it to be a clickable link).
    The are two custom files needed here. The first, record.js, goes into both the Contacts and Leads location as I want this functionality in both those places. You can adjust and add more or less modules as required.

    custom/modules/<module>/clients/base/views/record/record.php

    ({
         extendsFrom: 'RecordView',
         initialize: function(options) {
              this._super('initialize', [options]);
              this.collection.on('data:sync:complete', this.emailAsLink, this);
              this.events['click a[name=email_compose]'] = 'openComposerModal';
         },
             emailAsLink: function(){
              var emailtoLink = $("span[data-fieldname='custom_email_address_c'] span div").text();
              var rLink = ''+emailtoLink+'';
              $("span[data-fieldname='custom_email_address_c'] span div").html(rLink);
         },
         openComposerModal: function(){
              var self = this;
              app.utils.openEmailCreateDrawer('compose-email', {
                   fromCustomEmail: true,
                   related: self.model,
                   skip_prepopulate_with_case: true
              });
         }
    })

    and in the file (as in the link above):
    custom/modules/Emails/clients/base/views/compose-email/compose-email.js

    ({
         extendsFrom: 'EmailsComposeEmailView',
          initialize: function(options){
             this._super('initialize',[options]);
             this.configureDefaults();
         },
          /**
          * Configure the default data
          */
         configureDefaults: function(){
             var module = this.context.parent.get('module');
             if ((module == 'Contacts' || module == 'Leads') && this.context.attributes.fromCustomEmail && this.model.get('to_collection').length === 0){
                 var customEmail = this.context.parent.get('model').get('custom_email_address_c');
                 var newEmailAddress = app.data.createBean('EmailAddresses',{email_address: customEmail});
                 newEmailAddress.save();
                 var email = this.model;
                 emailAddressFilter = [{'email_address':customEmail}];
                 newEmailAddress.fetch({
                     fields: ['id','email_address'],
                     filter: emailAddressFilter,
                     success:function(newEmail){
                         var emailAddressId = newEmail.get('id');
                         var emailAddress = newEmail.get('email_address');
                         var toRecords = [];
                         toRecords.push(app.data.createBean('EmailParticipants', {
                             _link: 'to',
                             email_address: emailAddress,
                             email_address_id: emailAddressId
                         }));
                         email.get('to_collection').add(toRecords);
                     }
                 });
             }
         }
    })

    This will create you a new EmailAddress record for the supplied textual email address if none yet exists but it does not require any linked record. So it can send an email just to the direct address(es) given.

    Hope this helps someone.

    JH.

  • Hi John,

    Thank you, your replies were very helpful. I managed to complete my own customization thanks to the info you provided.

    Cheers,

    Samuel.

Reply Children
No Data