How can I run a function when a parent Record View function runs?

I am building a customisation that extends a view (BaseProductBundlesQuoteDataGroupListView) that already extends the BaseRecordView and I need one of my custom functions to run after a new record is saved.

I've found that there are save() and handleSave() functions in BaseRecordView, but I have been unable to extend or _.wrap() them in any way I could find. Running my function on this.context.get('parentModel').on('save') or 'saved' does not work at all. I have also tried runing it on this.parent.context.on('button:save_button:click'), but nothing happens.

Do you know how to do this efficiently without messing around with creating an additional view and duplicating my custom methods?

May I should build this the other way around and extend  the Quotes record view, getting and using the child view of BaseProductBundlesQuoteDataGroupListView?

  • What is your ultimate goal?

    Are you trying to run something when the Quote is saved or when the Product on the quote is saved?

    FrancescaS

  • When a new Quote is saved.

    I can make a function run when a product in the quote is saved and when a field in the quote itself is changed, but not after an entirely new quote is saved.

  • Extend the create controller on the Quote:

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

     

    HTH

    FrancescaS

     

  • Do I need to copy/paste the function from record.js into that view? Can both views not use the same function?

  • It used to be that the views were separate, so I have all my functions duplicated in record and create views if I need them in both. But judging from a very recent post here on the Community by Enrico Simonetti the create view extends the record view so you should be able to keep the function in record.js but I think you still need to define the event in create.js for it to trigger appropriately.

    Enrico Simonetti correct me if I'm wrong.

    FrancescaS

  • Can you use a before_save logic hook?

  • Hey Francesca Shiekh,

    I guess you are asking me this question because of my answer to this thread:How are record and create views supposed to work? 

    I am linking it so that other users have come context.

    Right this moment (as far as I know) the custom record.js and custom create.js of a module (custom/modules/<modulename>/clients/base/views/create|record/create.js|record.js) do not seem to extend each other directly, down the chain of extension.

    create.js extends record.js controller on all the non-module specific (as in clients/base/views and custom/clients/base/views) controllers.

    The order of extension of non-module specific controllers for creation is:

    • Custom create view
    • Core create view
    • Custom record view
    • Core record view

    In light of that, depending on if there is a core module create and record controller, you might be able to make them derive from each other on a case by case basis. You should be able to do that pretty easily for newly created custom modules.

    Just to double check, please give me few days to confirm the state of things but I think the above should help you get started

    Cheers

    ====================

    Update

    ====================

    After further investigation the above is correct. If functionality needs to be shared between a module's custom create.js and record.js controller, the most appropriate path forward is by leveraging a plugin that is added on both controllers.

    Plugins can be found on clients/base/plugins normally within the specific module, and then it needs to be added within the appropriate view's controller. An example can be 

    modules/KBContents/clients/base/plugins/KBContent.js used in multiple places including the record.js and create.js views controllers

    --

    Enrico Simonetti

    Sugar veteran (from 2007)

    www.naonis.tech


    Feel free to reach out for consulting regarding:

    • API Integration and Automation Services
    • Sugar Architecture
    • Sugar Performance Optimisation
    • Sugar Consulting, Best Practices and Technical Training
    • AWS and Sugar Technical Help
    • CTO-as-a-service
    • Solutions-as-a-service
    • and more!

    All active SugarCRM certifications

    Actively working remotely with customers based in APAC and in the United States

  • Why do you need to do it on the client side? I agree with Jeff Bickart below. Wouldn't it be much easier and cleaner to achieve your logic on a before save logic hook?

    It would also be consistently applied if the same action is completed by API's, and not only via UI.

    --

    Enrico Simonetti

    Sugar veteran (from 2007)

    www.naonis.tech


    Feel free to reach out for consulting regarding:

    • API Integration and Automation Services
    • Sugar Architecture
    • Sugar Performance Optimisation
    • Sugar Consulting, Best Practices and Technical Training
    • AWS and Sugar Technical Help
    • CTO-as-a-service
    • Solutions-as-a-service
    • and more!

    All active SugarCRM certifications

    Actively working remotely with customers based in APAC and in the United States

  • It actually would probably be better to use an Advance Workflow to acheive your logic

  • The first and main part of the customisation is that the related models are changed as the user changes the current model. So my goal is to re-use the same function on a new save.

    The reason it needs to happen after save is that when you add Quoted Line Items in create view, they are not really added before you save. In the record view they can be added individually.

    I can indeed use a before_save logic hook, but that would mean re-writing the JS function into PHP, which is not an efficient practice.

    As an alternative, I tried to run the same function on record view initialise, but that didn't work.

1 2