sugarcrm 将电子邮件地址添加到 to_collection

sugarcrm add email address to the to_collection

我一直在尝试通过代码将电子邮件地址添加到电子邮件模型的 to_collection 字段中。 to_collection 已经有了一些方法,比如添加、创建、删除。我正在尝试使用创建一个函数,但每当我使用 to_collection 模型中存在的电子邮件地址时,它都会设置为空白。

所以我正在做的事情

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)
})

这些是第一个控制台日志的属性值,如果我不使用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"

如果我使用 t.create(to_col);

它显​​示空白email_address:""

Preview.js and Preview.hbs

在 Sugar 7.9 中测试

由于我们正在谈论的是抽屉,您可以将地址作为 context.prepopulate.to_addresses 数组传递,Sugar 将用它们预填充 to: 输入栏。如果用户未删除,它们将在保存时添加到 to_collection

例如在浏览器的 JS 控制台中尝试此操作(在 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"},
            ],
        }
    }
})

在 Sugar 7.11 中测试

在 post-7.9 电子邮件模块中,似乎有必要使用 pre-existing Email-Address bean。

有一个 utils 函数将打开抽屉并在 data 参数对象中接受 to/cc/bcc 个 Email-Address Bean 集合(或属性数组)的数组。

openEmailCreateDrawer: function(layout, data, onClose)

因为您已经有一个来自不同型号的 to_collection,您可以这样做:

App.utils.openEmailCreateDrawer(
    'compose',
    {
        to: data.get("to_collection")
    }
)

也可以使用相同的 data 参数预填充主题、html 正文和附件。

例如使用你的变量这可能是这样的:

app.utils.openEmailCreateDrawer(
    'compose-email',
    {
        to: data.get("to_collection"),
        name: subject,
        description_html: body,
        attachments: attachments
    }
)

这是基于 utils.js 中负责解析上述函数的 data 的辅助函数的注释:

    /**                                                                      
     * 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) {

好吧,我确实通过以下操作解决了这个问题

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"
 });

但是,我不知道这是不是正确的方法。