howto auto-populate custom fields from product templates

I have added a custom field to product templates and to products as well. How can I achieve that the custom field in products gets auto-populated from the product template field when the product is added to the quote?

I have version 7.6.

  • Hi Alexander,

    You can customization

    - modules/Quotes/tpls/EditViewFooter.tpl to custom/modules/Quotes/tpls/EditViewFooter.tpl

    in tpl file <script type="text/javascript" src="{sugar_getjspath file='modules/Quotes/EditView.js'}"></script> change path to custom folder <script type="text/javascript" src="{sugar_getjspath file='custom/modules/Quotes/EditView.js'}"></script>

    in EditView.js file 

    function set_product_return(popup_reply_data)

    {

       ..... so on code below your custom field auto populated

         quotesManager.lookup_item('tax_class_name_' + row_id, window.document).value = name_to_value_array['tax_class_name'];

        quotesManager.lookup_item('CUSTOM FIELD NAME_' + row_id, window.document).value = name_to_value_array['CUSTOM FIELD NAME']; 

      

    }

    Regards,

    Dipesh

    Offshore Evolution Pvt Ltd

  • Hi,

    I'd recommend not updating the front-end and doing this customization on the back-end for two reasons:

    1) If you end up using the API, these customizations would not work

    2) Eventually Quotes are going to be moved from BWC  to Sidecar, at which point your customizations will stop working

    Instead, I recommend using a logic hook.

    In custom/modules/Products/logic_hooks.php, put the following file:

    <?php

        $hook_array['before_save'][] = Array(
            //Processing index. For sorting the array.
            1,
          
            //Label. A string value to identify the hook.
            'Update Custom Fields',
          
            //The PHP file where your class is located.
            'custom/modules/Products/before_save_class.php',
          
            //The class the method is in.
            'before_save_class',
          
            //The method to call.
            'updateCustomFields'
        );

    ?>

    Then, in custom/modules/Products/before_save_class.php, put the following code:

    <?php

    if (!defined('sugarEntry') || !sugarEntry)
        die('Not A Valid Entry Point');
      
    class before_save_class {
            function updateCustomFields($bean, $event, $arguments)
            {
                $productTemplate = BeanFactory::getBean('ProductTemplates', $bean->product_template_id);
                if (!empty($productTemplate->id)){
                    $bean->custom_field_on_product = $productTemplate->custom_field_on_product_template;
                }       
            }
    }

    ?>

    This assumes that custom_field_on_product is the custom field on the product, and custom_field_on_product_template is the custom field on the product template.

    Let me know if you have any issues with this.

  • Hi,

    thank you both for your help. Alan's solution works fine for me.

    Regards,
    Alexander