Counting Related Records in Javascript

Hi all.

I'm new to SugarCRM dev/javascript. Can anyone push me in the right direction for learning more about the model. I cannot find any pure docs on the model/controllers themselves, just examples of it being used (Which don't delve into more detail) and almost all of the articles I can find don't link into the new sugarclub.

What I want to do, is on Field Validation on a record, check the related records of Contacts from Opportunities and count them. And see if the number is greater than 0.

I have a brief understanding and can do something similar to this:

cont_id = this.model.get('contacts_cases_1contacts_ida');

       

        var ContactBean = SUGAR.App.data.createBean('Contacts', {id:cont_id}); 

        ContactBean.fetch({'success':function () { 

              designation = ContactBean.get('title'); 

              comp_type  = ContactBean.get('type_of_account_c');

           

              acc_id = ContactBean.get('account_id');

              acc_name = ContactBean.get('account_name');

            model.set('sup_desgn_c',designation);

            model.set('company_type_c',comp_type);

            model.set('account_id',acc_id);

            model.set('account_name',acc_name);

          

       }});

But if anyone can push me into more information on the model and how the information gets displayed that'd be fantastic. I have a good understanding of Bean and believe it's fairly similar.

Thank you,


Daniel

  • I found it difficult initially to find some good documentation on the model itself - but it's part of Sidecar, so you can see the Sidecar Documentation or the code itself (sidecar/src/data/bean.js).

    The model is actually called "Data Bean" in Sidecar. Here is the full class documentation - https://apidocs.sugarcrm.com/Sidecar/10.1.0/Data_Bean.html

    The method that you're looking for is called "getRelatedCollection". You can pass the link name and this will return you a backbone collection. The data might not be fetched by default, and you can fetch the related collection like:

    var contacts = opportunity.getRelatedCollection('contacts');

    if(!contacts.dataFetched) { contacts.fetch({ relate: true, success: function(data) {}, error: function(e) {});
    }

    You can then operate on this collection just like your regular backbone collection, by using .each or use the .length property.

    I found the sidecar documentation to be pretty useful, atleast to skim through initially:

    https://apidocs.sugarcrm.com/Sidecar/10.1.0/index.html

    It has documentation on the BeanCollection (sugar extension of backbone collection) and the App.

    Also, I just noticed that you're using Sugar.App - You can just directly use the "app" variable which is available by default in your component. I started writing some documentation about App object, just sharing it here in case it helps - https://seventunes.com/sugarcrm-javascript-snippets-1/

  • Another solution would be to create two calculated fields on the module in question. One that counts the number of related Opportunities and one that counts the number of related Contacts. Then all you have to do in your validation code is check if those fields have a 0 value.

  • Sorry for the delay, thanks very much. I'm struggling still but this has helped a lot! Thank you :)