Let the platform do the work

Prepopulating the Compose Email View

Overview

When composing an email in Sugar, it may be useful to modify the compose view to better suit your common business practices. In this article, we will use JavaScript to create a code-level customization which causes the To, CC, and Subject fields of the email to prepopulate with data from a related Opportunity.

Steps to Complete

Create a Custom Compose Email View

To modify the default compose email functionality, we will need to create our own compose-email view that extends from the base compose-email view. To accomplish this, we will need to create the following file:

./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 model = this.context.parent.get('model');
        var module = this.context.parent.get('module');
        if (module == 'Opportunities'){
            var contacts;
            var oppName = model.get('name');
            if (this.model.get('to_collection').length === 0) {
                var email = this.model;
                //Get the related contacts to the Opportunity
                contacts = model.getRelatedCollection('contacts');
                contacts.fetch({
                    relate: true,
                    success: function (data) {
                        var ccRecords = [],
                            toRecords = [];
                        data.forEach(function (record) {
                            var parentName = app.utils.getRecordName(record);
                            if (record.attributes.opportunity_role == 'Primary Decision Maker') {
                                //Primary decisions makers are added to the TO Recipients
                                toRecords.push(app.data.createBean('EmailParticipants', {
                                    _link: 'to',
                                    parent: _.extend({type: record.module}, record.attributes),
                                    parent_type: record.module,
                                    parent_id: record.get('id'),
                                    parent_name: parentName
                                }));
                            } else {
                                //All other contacts are added to the CC Recipients
                                ccRecords.push(app.data.createBean('EmailParticipants', {
                                    _link: 'cc',
                                    parent: _.extend({type: record.module}, record.attributes),
                                    parent_type: record.module,
                                    parent_id: record.get('id'),
                                    parent_name: parentName
                                }));
                            }
                        });

                        email.get('to_collection').add(toRecords);
                        email.get('cc_collection').add(ccRecords);
                    },
                    fields: ['id', 'full_name', 'email', 'opportunity_role']
                });
            }
            //Default the Subject of Email to the Opportunity Name
            email.set('name',oppName);
        }
    }
})

Some key things to note about this customization:

  • The custom view should extend from EmailsComposeEmailView
  • When adding recipients to an Email Bean Model, you should create an EmailParticipants Bean Model and specify the _link property that will be used.
  • This view will be used when clicking on the "Create" button in the Emails subpanel, or when clicking on Email Address on a record when the user is configured to use the Sugar Email Client

Once you have added the custom code, execute a Quick Repair and rebuild via Admin > Repair > Quick Repair and Rebuild, and your changes should now be reflected in your instance.