Customizing prefill when users copy Records in Sugar 7

I was recently asked how to change the values that get entered into the new record when a user copies an existing record.



This post will discuss how to make a simple change to your module's record view in order to change how fields get prefilled into a duplicated record in Sugar 7.

For many modules, it is important to make sure that certain fields are not copied or that the value of certain fields are changed to some other value like a default.  We do this in Sugar 7 in a couple of different places.  For example, if you try to copy an existing Task record in Sugar 7 then you will find that the duplicated Task's status will be set to 'Not Started' regardless of the original value.

There is a function on the Record View called setupDuplicateFields that you can override to modify the fields that are used to prefill the duplicated record.

See the custom RecordView controller below as an example.  This example takes the fields in the blacklist and unsets them or reverts them to their default value if provided in the field's metadata.

custom/modules/MODULE-NAME/clients/base/views/record/record.js

Extending Record View to customize prefill when a user copies a record.js

({
//The below jsdoc was copied from base RecordView controller in Sugar 7.2
    extendsFrom: 'RecordView',
/**
     * Called when current record is being duplicated to allow customization of
     * fields that will be copied into new record.
     *
     * Override to setup the fields on this bean prior to being displayed in
     * Create dialog.
     *
     * @param {Object} prefill Bean that will be used for new record.
     * @template
*/
setupDuplicateFields: function(prefill){
var duplicateBlackList = ["id", "status", "custom_field_c"];
_.each(duplicateBlackList, function(field){
if(field && prefill.has(field)){
//set blacklist field to the default value if exists
if (!_.isUndefined(prefill.fields[field]) && !_.isUndefined(prefill.fields[field].default)) {
prefill.set(field, prefill.fields[field].default);
                } else {
prefill.unset(field);
                }
            }
        });
    }
})

Parents Comment Children