2

I've been trying to add an Email Address through the code to the to_collection field of Emails model. the to_collection already have some methods like add, create, delete. I'm trying use create a function but whenever I use that my Email address which exists in the to_collection model sets to blank.

So here what I'm doing

var email = app.data.createBean('Emails', {id: <email_id> });
email.fetch({
       view:'record',
       success:__bind(function (data) {
           console.log('[TEST]',data);
           var prefill = app.data.createBean('Emails');
           prefill.copy(data);
           prefill.unset("to_collection");
           var to_col = data.get('from_collection').models[0];
           to_col.link.name = 'to';
           to_col.set('_link','to');               
           var t = data.get('to_collection');
           //t.create(to_col)

      },this)
})

These are the attribute values of the first console log, if I don't use t.create(to_col);

date_modified:"2018-03-14T00:06:36+05:30"
email_address:"xxx@gmail.com"
email_address_id:"69895ac6-e32e-11e7-b6fd-02ee7fb3392f"
email_addresses:
{email_address: "xxx@gmail.com", id: "69895ac6-e32e-11e7-b6fd-02ee7fb3392f"}
id:"a9e537b6-1704-11e8-bf1e-02b02daff135"
parent:[]
parent_id:""
parent_name:""
parent_type:""
_acl:
{fields: {…}}
_link:"to"
_module:"EmailParticipants"

And If I use t.create(to_col);

it shows blank email_address:""

Preview.js and Preview.hbs

Indrajeet Latthe
  • 374
  • 1
  • 8
  • 22
  • what functionality you want can you please explain. – Amitesh Kumar Mar 14 '18 at 13:19
  • I just want to add an email address to the to_collection field to an Email model which I need as reply Email. – Indrajeet Latthe Mar 15 '18 at 13:57
  • did yo try logic Hook – Amitesh Kumar Mar 21 '18 at 06:53
  • Where is that JS code located? What file? – Reisclef Mar 21 '18 at 10:34
  • And also, what version are you using? – Reisclef Mar 21 '18 at 10:57
  • @Reisclef The JS code is located in /custom/modules/Emails/clients/base/views/preview/preview.js and I'm using 7.11 Professional Edition. – Indrajeet Latthe Mar 21 '18 at 17:12
  • I'm going to echo the previous comment and ask what you're actually trying to achieve from a functionality perspective. – Reisclef Mar 22 '18 at 09:27
  • @Reisclef I'm currently working on the custom reply button on preview panel, i.e. I have one reply button on preview.hbs and I want reply-action to be executed(all things that happens when we click reply from Email record). I it clear or should I elaborate more? – Indrajeet Latthe Mar 22 '18 at 13:12
  • Much better. Ideally, is it possible to share the .hbs file, and preferably the whole js file so that my system can mirror yours as much as possible. This will help identify at point point a resolution is possible. – Reisclef Mar 22 '18 at 13:30
  • @Reisclef I don't mind sharing it but it's huge code, as for preview we cant write previewdefs in php so I have done all code for click using jQuery and even handelbars need functions to be registered If I want to use. I will upload it soon just let me add comments (and yes it is kinda spaghetti code so bear me) – Indrajeet Latthe Mar 22 '18 at 15:27
  • @Reisclef I've added a link because it was too long for body. – Indrajeet Latthe Mar 25 '18 at 19:11

2 Answers2

1

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) {
Jay
  • 3,640
  • 12
  • 17
  • Well this doesn't work for me, https://gitlab.com/indrajeet183/just-for-stackoflow/raw/master/Screenshot%20from%202018-03-31%2012-53-49.png – Indrajeet Latthe Mar 31 '18 at 07:35
  • Hum, I see. I only had 7.9 available at that time. They must have changed things up in 7.10 I'll see if I can get a newer instance and get back to you. – Jay Mar 31 '18 at 07:44
  • I got to figure out how it works in 7.11 and updated my answer - please feel free to give that solution a go in 7.10 and let me know if it works :) – Jay Mar 31 '18 at 08:40
  • Damn, I tested that on 7.10.0.0 yourr first code in console which didn't worked for me. Let me check the other once in 7.11 – Indrajeet Latthe Mar 31 '18 at 08:56
  • You mentioned that you're on a newer version in another question, so it was really my bad for even trying on 7.9, knowing that the Emails module was changed a lot in 7.10 (it got transferred from legacy mode to Sugar 7 UI framework). I just assumed/hoped that the drawer itself hadn't changed much, as drawers are a feature of the new UI already anyway, but apparently they reworked everything regarding the Emails module and the drawer did change ^^' So yeah, sorry about wasting your time earlier, I hope the new code does the trick :) – Jay Mar 31 '18 at 09:53
  • Well, I did solve the problem but meh... our client opted salesforce leaving sugar. But yea thank you very much you helped a lot. – Indrajeet Latthe Apr 10 '18 at 12:04
1

Well I did solve this issue by doing following

prefill.get('to_collection').create({
      deleted: false,
      email_address: to_coll.get('email_address'),
      email_address_id: to_coll.get('email_address_id'),
      _link: "to"
 });

But, I don't know this is a proper way or not.

Indrajeet Latthe
  • 374
  • 1
  • 8
  • 22