在重定向页面之前获取新创建的记录ID

时间:2017-06-16 13:42:10

标签: sugarcrm suitecrm

当我点击保存按钮并在重定向页面之前,我想使用javascript检索新创建的记录的ID。

你有什么想法吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

Sugar 7中的一种方法是覆盖CreateView。

这是一个CustomCreateView的示例,它在成功创建新帐户后,但在Sugar对创建的记录作出反应之前,在警报消息中输出新ID。

custom/modules/Accounts/clients/base/views/create/create.js

({
    extendsFrom: 'CreateView',

    // This initialize function override does nothing except log to console,
    // so that you can see that your custom view has been loaded.
    // You can remove this function entirely. Sugar will default to CreateView's initialize then.
    initialize: function(options) {
        this._super('initialize', [options]);
        console.log('Custom create view initialized.');
    },

    // saveModel is the function used to save the new record, let's override it.
    // Parameters 'success' and 'error' are functions/callbacks.
    // (based on clients/base/views/create/create.js)
    saveModel: function(success, error) {

        // Let's inject our own code into the success callback.
        var custom_success = function() {
                // Execute our custom code and forward all callback arguments, in case you want to use them.
                this.customCodeOnCreate(arguments)
                // Execute the original callback (which will show the message and redirect etc.)
                success(arguments);
        };

        // Make sure that the "this" variable will be set to _this_ view when our custom function is called via callback.
        custom_success = _.bind(custom_success , this);

        // Let's call the original saveModel with our custom callback.
        this._super('saveModel', [custom_success, error]);
    },

    // our custom code
    customCodeOnCreate: function() {
        console.log('customCodeOnCreate() called with these arguments:', arguments);
        // Retrieve the id of the model.
        var new_id = this.model.get('id');
        // do something with id
        if (!_.isEmpty(new_id)) {
            alert('new id: ' + new_id);
        }
    }
})

我使用Sugar 7.7.2.1的Accounts模块对此进行了测试,但应该可以为Sugar中的所有其他 sidecar 模块实现此功能。 但是, b 确认 w ard- c ompatibility模式(具有{{ 1}}在他们的URL中)。

注意:如果相关模块已有自己的#bwc,您可能应该从Base<ModuleName>CreateView(无<ModuleName>CreateView)而不是默认Base延伸。< / p>

请注意,此代码在Sugar升级期间很可能会中断,例如如果默认CreateView代码收到CreateView函数定义中的更改。

另外,如果您想进一步阅读有关扩展视图的内容,请参阅有关此主题的SugarCRM开发博文:https://developer.sugarcrm.com/2014/05/28/extending-view-javascript-in-sugarcrm-7/

答案 1 :(得分:-1)

我通过使用逻辑钩子(保存后)解决了这个问题,为了您的信息,我使用的是Sugar 6.5,无论是socketcrm的版本。

谢谢!