Page 1 of 2 12 LastLast
Results 1 to 10 of 16
Like Tree1Likes

Thread: calculated field: count of relationship occurrences

  1. #1
    davideps is offline Member
    Join Date
    May 2011
    Posts
    15

    Default calculated field: count of relationship occurrences

    Hello,

    I'm using SugarCRM 6.1.4 on Ubuntu 10.04.

    I've created a one to many relationship between Documents and Notes. When I click "view documents", I'd like the list to contain the number of notes related to each document. Some will have no notes. Some may have 30 or more. I'm guessing I need to create a new variable that displays a count of rows matching an SQL query against an association table. I've found various tips on "calculated fields" from different versions of SugarCRM, but not one that counts a type of relation. I'm hoping a guru may be able to point me in the right direction.

    thank you,
    -david

  2. #2
    Angel's Avatar
    Angel is offline Sugar Community Member
    Join Date
    Jul 2005
    Location
    Los Angeles
    Posts
    4,813

    Default Re: calculated field: count of relationship occurrences

    Take a look at the "Custom Aging Field" example on the developers site (http://developer.sugarcrm.com). You should be able to adapt it to use a COUNT query that retrieves the number of notes that are linked to the specific Document.
    Regards,

    Angel Magaña
    Co-Author: Implementing SugarCRM 5.x (Packt Publishing -- Sept. 2010)
    Blog: http://cheleguanaco.blogspot.com.
    Twitter: @cheleguanaco.

    ________
    | Projects: |_____________________________________
    |
    | CandyWrapper (.NET Wrapper for SugarCRM SOAP API). Source now available on GitHub!
    | GoldMine to SugarCRM Express Conversion. Latest: 1.0.1.7 (Nov. 3, 2009)
    | CRM SkyDialer (Skype Integration). Latest: 1.0.2 (Feb. 17, 2010)
    | Round Robin Leads Assignment
    | Phone Number Formatter
    | CaseTwit (Twitter Integration)
    ______________________________________________

  3. #3
    davideps is offline Member
    Join Date
    May 2011
    Posts
    15

    Default Re: calculated field: count of relationship occurrences

    Angel, thank you.

  4. #4
    dmontminy is offline Member
    Join Date
    Jan 2011
    Posts
    8

    Default Re: calculated field: count of relationship occurrences

    I need the very same functionality implemented, and I'm at a complete lost when it comes to modifying the actual "CustomCode.php" from the AgingField example.

    The SQL itself is easy enough:
    SELECT COUNT(*) as cnt FROM relationship_table WHERE idA = id AND deleted = '0';

    The only thing I need now is the correct names of "relationship_table" and "idA" in a manner that is upgrade_safe.

    I can't find any sort of API documentation that would list objects and functions I can rely on...

  5. #5
    Angel's Avatar
    Angel is offline Sugar Community Member
    Join Date
    Jul 2005
    Location
    Los Angeles
    Posts
    4,813

    Default Re: calculated field: count of relationship occurrences

    The general idea is that the calculated field calls a custom function within a custom PHP file. The function in question in turn returns a value/data that matches the data type of the field.

    So, for a situation like this, we would typically want to return an integer value, to be displayed in a field that is also of type integer.

    Your function would look something like this:

    PHP Code:
    function calculatedValue($focus$field$value$view)
    {
       
    $temp 10;

       
    $temp2 2;
     
       
    $result $temp $temp2;

       return 
    $result;


    This is a very simple example, but the important part is the return portion as it gives us the result of 10 / 2, which in turn is passed on to the calculated field. Now to extend that to use SQL queries, we simply add our logic for that before the return and leverage the already existing Sugar connection, as such:

    PHP Code:
    function caculatedValue($focus$field$value$view)
    {
               
    //Calculate record count
            
    $query "SELECT COUNT(*) as the_count FROM notes ";
                
            
    $results $focus->db->query($querytrue);
            
    $row $focus->db->fetchByAssoc($results);
            
            
    $total $row['the_count'];

                    return 
    $total;


    Regards,

    Angel Magaña
    Co-Author: Implementing SugarCRM 5.x (Packt Publishing -- Sept. 2010)
    Blog: http://cheleguanaco.blogspot.com.
    Twitter: @cheleguanaco.

    ________
    | Projects: |_____________________________________
    |
    | CandyWrapper (.NET Wrapper for SugarCRM SOAP API). Source now available on GitHub!
    | GoldMine to SugarCRM Express Conversion. Latest: 1.0.1.7 (Nov. 3, 2009)
    | CRM SkyDialer (Skype Integration). Latest: 1.0.2 (Feb. 17, 2010)
    | Round Robin Leads Assignment
    | Phone Number Formatter
    | CaseTwit (Twitter Integration)
    ______________________________________________

  6. #6
    dmontminy is offline Member
    Join Date
    Jan 2011
    Posts
    8

    Default Re: calculated field: count of relationship occurrences

    Thanks for the reply.

    Everything you mentioned is very helpful (and will be in the future for anyone searching for this topic), but not exactly what I am missing in term of information.

    What type of objet is $focus? Where is it defined? Can I get a list of possible function/Objets call I can make?
    I'm sure I can manage to do everything myself, but I can't find the right documentation.

  7. #7
    Angel's Avatar
    Angel is offline Sugar Community Member
    Join Date
    Jul 2005
    Location
    Los Angeles
    Posts
    4,813

    Default Re: calculated field: count of relationship occurrences

    $focus is a representation of the SugarBean, which in turn represents the current record, connection and other valuable parameters.

    Check out data/SugarBean.php if you want details on all the functions, etc. I am not sure if they are documented in the Developer's manual, but you should give that a look as well. It is available at http://developers.sugarcrm.com
    Regards,

    Angel Magaña
    Co-Author: Implementing SugarCRM 5.x (Packt Publishing -- Sept. 2010)
    Blog: http://cheleguanaco.blogspot.com.
    Twitter: @cheleguanaco.

    ________
    | Projects: |_____________________________________
    |
    | CandyWrapper (.NET Wrapper for SugarCRM SOAP API). Source now available on GitHub!
    | GoldMine to SugarCRM Express Conversion. Latest: 1.0.1.7 (Nov. 3, 2009)
    | CRM SkyDialer (Skype Integration). Latest: 1.0.2 (Feb. 17, 2010)
    | Round Robin Leads Assignment
    | Phone Number Formatter
    | CaseTwit (Twitter Integration)
    ______________________________________________

  8. #8
    dmontminy is offline Member
    Join Date
    Jan 2011
    Posts
    8

    Default Re: calculated field: count of relationship occurrences

    I have hard-coded the SQL query and it seems to work fine. I can add the Calculated field to the Detail view but not to the listview. If I do Save&Deploy in the studio, it removes the field. I can add any other field, just not that one. Any idea?

    (side-note: this thread should be moved to the 'help' section of the forums)

  9. #9
    andopes's Avatar
    andopes is offline A Sugar Hero | Help Forum Moderator
    Join Date
    Jul 2006
    Location
    São Paulo - Brazil
    Posts
    8,335

    Default Re: calculated field: count of relationship occurrences

    Probably you created a after_retrieve logic hook.
    For List View you need to configure a process_record logic hook.

    Cheers
    André Lopes
    DevToolKit / Project of the Month - June 2009
    Lampada Global Services- Open Source Solutions
    Avenida Ipiranga, 318
    Bloco B - CJ 1602
    São Paulo, SP 01046-010
    Brazil
    Office: +55 11 3237-3110
    Mobile: +55 11 7636-5859
    e-mail: andre@lampadaglobal.com

    Lampada Global delivers offshore software development and support services to customers around the world.
    Lampada is proud to be a SugarCRM Gold Partner, revolutionizing Customer Relationship Management.

    I DO NOT answer questions through PM and Email. If you need some help post your question into SugarForum.

  10. #10
    tkendo is offline Junior Member
    Join Date
    Jun 2011
    Posts
    2

    Default Re: calculated field: count of relationship occurrences

    I am trying to do something very similar that you guys are doing, but have one question. I have two modules "Districts" and "Schools" and the one-to-many is set up so there are multiple schools for each district, and I am looking to pull the number of schools per district into an easy to read field. I am using the same setup as above with a "CustomCode.php" file.

    I have everything working except for the proper SQL query I think. Right now the function is returning '306' which is the total number of records in the school module. I was thinking I could add a WHERE argument to the query to reference the name of the district which I could store as a separate field in each schools record, but I am not sure how to get that kind of data from the sugarbean. I don't understand exactly what $focus and such do and what they return....

    Here is my function:
    Code:
    function getBldgCount($focus, $field, $value, $view)
    {
            $query = "SELECT COUNT(*) as the_count FROM dists_schools ";
    
            $results = $focus->db->query($query, true);
            $row = $focus->db->fetchByAssoc($results);
    
            $total = $row['the_count'];
    
                    return $total;
    
    }

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Calculated field
    By agey in forum Developer Help
    Replies: 0
    Last Post: 2010-01-25, 08:41 AM
  2. Count field for data in contacts
    By gullevek in forum Developer Help
    Replies: 1
    Last Post: 2009-07-07, 10:37 AM
  3. Calculated field
    By royke in forum Developer Help
    Replies: 2
    Last Post: 2009-01-22, 05:26 AM
  4. Add a contact field with tasks count
    By piccirm2 in forum Help
    Replies: 2
    Last Post: 2008-06-26, 12:12 PM
  5. Calculated Field
    By avagarwal in forum Developer Help
    Replies: 1
    Last Post: 2008-06-25, 06:35 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •