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
  • Wished this worked in the Quotes module but sadly it doesn't because the setupDuplicateFields function is never called.

    In the base record view controller (./clients/base/views/record/record.js), the setupDuplicateFields function is attached to a duplicate:before event listener in the initialize. The duplicateClicked function (which is the function attached to the Copy button) triggers the duplicate:before event which in turn calls the setupDuplicateFields function. The problem in the Quotes module is that it extends the record view controller (./modules/Quotes/clients/base/views/record/record.js) and overrides the duplicateClicked function and never calls the parent function or triggers the duplicate:before event so the setupDuplicateFields function never gets called. Wished the developers would have followed the same pattern in the Quotes module so that you could follow the documented example to customize the prefill.

  • Hi Brett,

    This certainly sounds like a bug so I went ahead and filed the following defect:
    https://portal.sugarondemand.com/#supp_Bugs/81885 

    I will have my team investigate to see if we can identify a workaround to this issue and follow up with you.

  • Thanks Jerry for adding a bug for this. I have found a work around so I'm good. For anyone with a similar need, here is my work around. I ended up creating a custom, non-db field that I use to store prefill values in on the model. I overrode the duplicateClicked function on the record view and set all my prefill values as a JSON string into my non-db field and then call the parent duplicateClicked function. The values copy over because they are on the model, so in my create view initialize function, I check for the non-db field to have data in it, and if it does, I parse the JSON string and set the model with the stored prefill values. Would have been much simpler if the Quotes module gets fixed so that it calls the setupDuplicateFields function during a copy, but until then, this is one way around it.

Comment
  • Thanks Jerry for adding a bug for this. I have found a work around so I'm good. For anyone with a similar need, here is my work around. I ended up creating a custom, non-db field that I use to store prefill values in on the model. I overrode the duplicateClicked function on the record view and set all my prefill values as a JSON string into my non-db field and then call the parent duplicateClicked function. The values copy over because they are on the model, so in my create view initialize function, I check for the non-db field to have data in it, and if it does, I parse the JSON string and set the model with the stored prefill values. Would have been much simpler if the Quotes module gets fixed so that it calls the setupDuplicateFields function during a copy, but until then, this is one way around it.

Children
No Data