Selecting quote line items from intelligence pane catalog does not trigger on change event on mft_part_num

In Quotes I have an on-change event on ProductsQuoteDataRelateField to set price and tax from an ERP.

custom/modules/Products/clients/base/fields/quote-data-relate/quote-data-relate.js

({
  extendsFrom:'ProductsQuoteDataRelateField',
  initialize: function(options){
    this._super('initialize', [options]);
    this.model.on('change:mft_part_num', this.triggerERPLookup,this); //takes care of user-added/modified lines
  },
  triggerERPLookup: function(){
    //do the lookup and set tax and price for the line item
  }
})

But this does not trigger when the user selects the product using the product catalog dashlet.

Any suggestions on how to trigger the same logic when the product catalog dashlet is used?

thanks,
FrancescaS

  • Hi Francesca Shiekh 

    By adding an Item from Product Catalog Dashlet it will create an entire new model and add it into a new row, so no event is triggered, but you can track/listen that by extending the method _onProductCatalogDashletAddItem from modules/Quotes/clients/base/layouts/quote-data-list-groups/quote-data-list-groups.js.

    Regards

    André Lopes
    Lampada Global
    Skype: andre.lampada
  • Actually you may need to extend the method onAddNewItemToGroup from modules/ProductBundles/clients/base/views/quote-data-group-list/quote-data-group-list.js once the previous I mentioned is a private one.

    In this case you need to decide whether to invoke the ERP integration from inside this extended method if product_template_id is populated as per variable prepopulateData.

    Good luck!

    André Lopes
    Lampada Global
    Skype: andre.lampada
  • Thank you André Lopes, I am "playing" with it now, I'll let you know how it goes.

  • André Lopes, I went with your first instinct.

    I distinctly recall trying to add my ERP lookup to  

    custom/modules/ProductBundles/clients/base/views/quote-data-group-list/quote-data-group-list.js

    when Sidecar Quotes were first introduced and after getting nowhere for a long time I gave up and used the on change in custom/modules/Products/clients/base/fields/quote-data-relate/quote-data-relate.js instead.

     

    For the Product Catalog Dashlet I proceeded as follows and this works:

    In: 

    custom/modules/Quotes/clients/base/layouts/quote-data-list-groups/quote-data-list-groups.js

    ({
      extendsFrom: 'QuotesQuoteDataListGroupsLayout',
      initialize: function(options){
        this._super('initialize',[options]);
      },
      _onProductCatalogDashletAddItem: function(productData) {
        this.triggerERPLookup(productData);
        //note that we don't call the parent _onProductCatalogDashletAddItem here
        //we need to wait until the API is done fetching Tax and Price
      },
      triggerERPLookup: function(productData){
        //I need users to fill in the Quote completely before adding products
        //we have some params that are set when the quote is saved that are needed for the lookup
        //and I need them to click the save on the Quoted Line Item to trigger some more stuff...
        //not ideal but it's the best I could do
       
        if(this.context.get('layout') == 'create'){
          app.alert.show('message-id', {
            level: 'warning',
            messages: 'Please save quote before adding line items',
            autoClose: false
          });
          return;
        }
        //at this point "this" is the Quote
        //productData is an array with field=>value for each of the fields of the Quoted Line Item we just selected

        //I will be using quote_attr in my parameters passed to the API
        var quote_attr = this.model.attributes;

        //set the quoted line item currency. It is not copying the currency from the quote.
        productData['currency_id']=quote_attr.currency_id;

        //go get the Price using the quote attributes and the productData
        this.getPrice(quote_attr, productData);
      },
      getPrice:function(quote_attr,productData){
        //I am taking some specific details out so you don't get lost in my particular case
        var self = this;

        //tell the user we are looking up price, this may take a while they need to know something is happening
        app.alert.show('erp_price', {
          level: 'process',
          title: 'Looking up Price in ERP...'
        });

        //set Price from ERP
        var price_url = app.api.buildURL('ERPGetPrice/.<a bunch of parameters>);
        app.api.call('GET', price_url, null, {
          success: function (price){
            //set the productData discount price to the price we found
            productData['discount_price'] = price;
            //dismiss the alert
            app.alert.dismiss('erp_price');
            //go get the tax
            self.getTax(quote_attr,productData);
          },
          error: function(e){
            app.alert.dismiss('erp_price');
            console.log(e);
            //if there is an error just put the product in, the price and tax will be zero but our sales team knows what to do
            self._super('_onProductCatalogDashletAddItem',[productData]);
          },
        });
      },
      getTax: function(quote_attr, productData){
        // in the US the tax depends on the item, service is different from physical products
        // so we added tax at line item level - it seems very odd that that is not how it is out of the box
        //also see Quotes/record/record.js for changes in tax exemption
        var self = this;
        if(quote_attr.tax_exempt_c == 1){
          //no sense wasting time looking up tax if they are tax exempt. We have a flag for that on the Quote (one of the params that are set when the quote is saved)
          productData['line_tax_percent_c']= 0;
          //call the parent to render the line
          self._super('_onProductCatalogDashletAddItem',[productData])
        }else{
          //warn the user that we're doing something
          app.alert.show('erp_tax', {
            level: 'process',
            title: 'Looking up Tax in ERP...'
          });
          //go look up the tax
          var tax_url = app.api.buildURL('ERPGetTax/'.<a bunch of params here>);
          app.api.call('GET', tax_url, null, {
            success: function (tax){
              //set the productData tax to what we've found
              productData['line_tax_percent_c'] = app.math.round(tax, 3, true);//format to precision matching field def
              //dismiss the alert
              app.alert.dismiss('erp_tax');
              //now we have both Price and Tax we can call the parent to render the data
              self._super('_onProductCatalogDashletAddItem',[productData])
            },
            error: function(e){
              app.alert.dismiss('erp_tax');
              console.log(e);
              //if getting tax errors out just render what we have our sales team knows what to do
              self._super('_onProductCatalogDashletAddItem',[productData])
            },
          });
        }
      },
    })