Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Accessing Field Data on Current Record

  1. #1
    BCdev is offline Junior Member
    Join Date
    Jan 2012
    Posts
    3

    Default Accessing Field Data on Current Record

    I am using Sugar Enterprise 6.2.1 and I am trying to populate a custom drop down in the Accounts module that is populated from the database for all the contacts relating to the current account. I have added some custom code to the custom/include/language/en_us.lang.php file that populates the dropdown for all contacts and that all works fine.

    But where I come unstuck is when I try to filter the contacts by the current account because I don't know how to access the account_id for the current record so that I can do something along the lines of:

    $myQuery = "SELECT CONTACT_ID FROM mydatabase.accounts_contacts WHERE account_id = "+$current_account_id

    My question is how do you access the value of the account_id field(or any field on the record) on both creation and edit of a record(in this case an account) when populating a custom drop down.

  2. #2
    jmertic is offline Sugar Community Manager
    Join Date
    Dec 2007
    Posts
    2,224

    Default Re: Accessing Field Data on Current Record

    Quote Originally Posted by BCdev View Post
    I am using Sugar Enterprise 6.2.1 and I am trying to populate a custom drop down in the Accounts module that is populated from the database for all the contacts relating to the current account. I have added some custom code to the custom/include/language/en_us.lang.php file that populates the dropdown for all contacts and that all works fine.

    But where I come unstuck is when I try to filter the contacts by the current account because I don't know how to access the account_id for the current record so that I can do something along the lines of:

    $myQuery = "SELECT CONTACT_ID FROM mydatabase.accounts_contacts WHERE account_id = "+$current_account_id

    My question is how do you access the value of the account_id field(or any field on the record) on both creation and edit of a record(in this case an account) when populating a custom drop down.
    Where are you doing this query from? There's lots of options depending upon where you are coming from.
    John Mertic
    Sugar Community Manager

  3. #3
    BCdev is offline Junior Member
    Join Date
    Jan 2012
    Posts
    3

    Default Re: Accessing Field Data on Current Record

    I am doing the query inside the custom/include/language/en_us.lang.php where I define my drop down but this is only because that is the only way I know how if there are better ways any suggestions would be appreciated.

  4. #4
    SagarCRM's Avatar
    SagarCRM is offline Sugar Community Member
    Join Date
    Oct 2011
    Location
    Mumbai, Maharashtra, India
    Posts
    111

    Default Re: Accessing Field Data on Current Record

    Hi BCDev,

    when you fire query dynamically in custom/include/language/en_us.lang.php it executes on each and every page load, so its a drawback, you can use query in all views of the current module.

    and it'll be easy for you to pass the current record fields too.

    like

    PHP Code:

    $myQuery 
    "SELECT CONTACT_ID FROM mydatabase.accounts_contacts WHERE account_id = ".$this->bean->account_id

    Quote Originally Posted by BCdev View Post
    I am using Sugar Enterprise 6.2.1 and I am trying to populate a custom drop down in the Accounts module that is populated from the database for all the contacts relating to the current account. I have added some custom code to the custom/include/language/en_us.lang.php file that populates the dropdown for all contacts and that all works fine.

    But where I come unstuck is when I try to filter the contacts by the current account because I don't know how to access the account_id for the current record so that I can do something along the lines of:

    $myQuery = "SELECT CONTACT_ID FROM mydatabase.accounts_contacts WHERE account_id = "+$current_account_id

    My question is how do you access the value of the account_id field(or any field on the record) on both creation and edit of a record(in this case an account) when populating a custom drop down.
    --

    Thanks & Regards,

    Sagar Salunkhe
    Mumbai, Maharashtra, INDIA

    sagar.salunkhe1991@gmail.com

    ~~ IT professionals never dies, they just go Offline... ~~


    sugar development and support services:- indian.sugarbean@gmail.com

  5. #5
    jmertic is offline Sugar Community Manager
    Join Date
    Dec 2007
    Posts
    2,224

    Default Re: Accessing Field Data on Current Record

    Quote Originally Posted by SagarCRM View Post
    Hi BCDev,

    when you fire query dynamically in custom/include/language/en_us.lang.php it executes on each and every page load, so its a drawback, you can use query in all views of the current module.

    and it'll be easy for you to pass the current record fields too.

    like

    PHP Code:

    $myQuery 
    "SELECT CONTACT_ID FROM mydatabase.accounts_contacts WHERE account_id = ".$this->bean->account_id
    I would follow the same guide shown in this post:

    SugarCRM Developer Blog » Blog Archive » HOWTO: Have a dropdown field that get’s it’s options from a function.

    The only change you need to make is to the function definition for the function you are calling to get the dropdown values. That function takes up to 4 parameters, and the first is an object reference to the current bean object. So, you could write the dropdown that grabs the values something like this:

    PHP Code:
    function getAccountContacts($focus)
    {
        
    $query "SELECT id, name from contacts where id in ( select CONTACT_ID FROM mydatabase.accounts_contacts WHERE account_id = \"{$focus->id}\" )";
        
    $result $this->db->query($queryfalse);

        
    $list = array();
        
    $list['']='';
        while ((
    $row $this->db->fetchByAssoc($result)) != null) {
            
    $list[$row['id']] = $row['name'];
        }

        return 
    $list;

    I'll do a dev blog post about this in the next few weeks...
    John Mertic
    Sugar Community Manager

  6. #6
    BCdev is offline Junior Member
    Join Date
    Jan 2012
    Posts
    3

    Default Re: Accessing Field Data on Current Record

    Thanks John I got It working!
    So for anyone following this post or in a similar boat here are the steps I followed:

    Based on Johns response and the tutorial he posted I created a custom_utils.php file in my custom\include\ which according to the documentation is upgrade safe and added the following code

    PHP Code:
    <?php

    function getAccountContacts($focus)
    {
        
    $query "SELECT id, first_name,last_name FROM database.contacts where id in ( SELECT contact_id FROM database.accounts_contacts WHERE account_id = \"{$focus->id}\" )";
            
        
    $result $focus->db->query($queryfalse);
        
    $list = array();
            
    $list['']='';

        while ((
    $row $focus->db->fetchByAssoc($result)) != null)
        {
                
    $list[$row['id']] = $row['first_name']." ".$row['last_name'];
            }

        return 
    $list;
    }

    ?>
    I got a "$this not set to an object context error" so I had to change the $this in johns post to $focus(or the current bean) because the custom_utils.php must not be in a context where $this exists.

    I then created a custom field called "accountcontact" in the Accounts module using the admin>studio and set its type to drop down, added it to the edit and detail view and saved and deployed the layouts.

    then in my \custom\Extension\modules\Accounts\Ext\Vardefs\sug arfield_accountcontact_c.php I changed the $dictionary statement to:

    PHP Code:
    $dictionary['Account']['fields']['accountcontact_c']['function']='getAccountContacts'
    I had to do a admin>quick rebuild and repair before my code was executing.

    I am a total beginner at sugar development and PHP so please correct me if I have made any incorrect statements or assumptions.
    Goodluck.

  7. #7
    nazmul87 is offline Sugar Community Member
    Join Date
    Feb 2012
    Location
    Pune
    Posts
    21

    Default Re: Accessing Field Data on Current Record

    How to get Accounts ID of Current Detail View,My Code Snippets below.
    __________________________________________________ ___________

    <?php
    require_once('modules/Accounts/Account.php');

    class AccountDetailView extends Account {
    function AccountDetailView() {
    parent::Account();
    }
    function get_your_stuff()
    {

    echo "Accounts ID=".$this->id; // It giving me a wrong id. Question is Why, If i am wrong so what is the best way for finding account id of current detail view.

    $select = " SELECT contacts.id,contacts.first_name, contacts.primary_address_city, contacts.primary_address_state from contacts ";

    return $select;

    }
    }
    ?>


    Thanks.

  8. #8
    jmertic is offline Sugar Community Manager
    Join Date
    Dec 2007
    Posts
    2,224

    Default Re: Accessing Field Data on Current Record

    Quote Originally Posted by nazmul87 View Post
    How to get Accounts ID of Current Detail View,My Code Snippets below.
    __________________________________________________ ___________

    <?php
    require_once('modules/Accounts/Account.php');

    class AccountDetailView extends Account {
    function AccountDetailView() {
    parent::Account();
    }
    function get_your_stuff()
    {

    echo "Accounts ID=".$this->id; // It giving me a wrong id. Question is Why, If i am wrong so what is the best way for finding account id of current detail view.

    $select = " SELECT contacts.id,contacts.first_name, contacts.primary_address_city, contacts.primary_address_state from contacts ";

    return $select;

    }
    }
    ?>


    Thanks.
    Try using $this->bean->id instead.
    John Mertic
    Sugar Community Manager

  9. #9
    garnok22@hotmail.com is offline Junior Member
    Join Date
    Mar 2012
    Posts
    5

    Default Re: Accessing Field Data on Current Record

    Quote Originally Posted by jmertic View Post
    Try using $this->bean->id instead.

    Hello, i have the same kind of problem.

    i add this following code in /modules/Opportunities/Opportunity.php

    Code:
    public static function get_current_opportunity_id()
    {
    
    $sql = "SELECT sales_stage FROM opportunities where id = " .$this->bean->id;
    $result = $GLOBALS['db']->query($sql);
    $row = $GLOBALS['db']->fetchByAssoc($result);
    
    	if($row != null)
    	{ $sales_stage_status = $row['sales_stage']; }
    	else { echo "error"; }
    
    
    return $sales_stage_status;
    
    }
    after that i call this function in /custom/modules/Opportunities/metadata/detailviewdefs.php

    Code:
    <?php
    
    $sales_stage_status= Opportunity::get_current_opportunity_id() ;
    
    /*echo "sales_stage_status =" .$sales_stage_status	 ;*/
    
    if ($sales_stage_status == 'Closed Won' || is_admin($GLOBALS['current_user']))	
    {
    $viewdefs ['Opportunities'] = 
    array (...........
    And i have the following error message :
    Fatal error: Using $this when not in object context in /home/garnok/Sites/crm616/modules/Opportunities/Opportunity.php on line 458

    How can i get the current opportunity record id ?

  10. #10
    jmertic is offline Sugar Community Manager
    Join Date
    Dec 2007
    Posts
    2,224

    Default Re: Accessing Field Data on Current Record

    Try using $GLOBALS['FOCUS']->id perhaps? The problem is that you doing a static call, thus $this has no context.
    John Mertic
    Sugar Community Manager

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. Modify Related Record Field/Label to link to custom data.
    By DemonBob in forum Developer Help
    Replies: 0
    Last Post: 2011-12-30, 10:13 PM
  2. accessing pro data with expired license
    By ogfomk in forum Installation and Upgrade Help
    Replies: 1
    Last Post: 2011-02-08, 07:01 PM
  3. Replies: 4
    Last Post: 2008-12-30, 12:05 PM
  4. can I have the value of the current record in the window?
    By omegaing in forum Developer Help
    Replies: 0
    Last Post: 2006-06-14, 08:16 AM

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
  •