Override Quotes Edit view to get contact's address instead of account billing address

Hi,
I’m trying to customize Edit view of Quotes to auto fill billing address with the contact’s primary address when selecting a contact from popup or quick search (instead od Account’s billing address).
I have copyed controller.php in \custom\modules\AOS_Quotes\controller.php with these changes:


require_once('modules/AOS_Quotes/controller.php');

class CustomAOS_QuotesController extends AOS_QuotesController {
...

if (isset($_REQUEST['contact_id'])) {
            		$query = "SELECT * FROM contacts WHERE id = '{$_REQUEST['contact_id']}'";
			$result = $this->bean->db->query($query, true);
			$row = $this->bean->db->fetchByAssoc($result);
			$this->bean->billing_contact_id = $row['id'];
			$this->bean->billing_contact = $row['first_name'].' '.$row['last_name'];
			$this->bean->billing_address_street = $row['primary_address_street'];
			$this->bean->billing_address_city = $row['primary_address_city'];
			$this->bean->billing_address_state = $row['primary_address_state'];
			$this->bean->billing_address_postalcode = $row['primary_address_postalcode'];
			$this->bean->billing_address_country = $row['primary_address_country'];
}

It keeps on behaving like the new controller does not exist. Am I missing something in the process?
Thanks in advance for your help

I’m not sure of this solution, but while writing custom controllers you need to extend the sugar controller instead of the module controller. Please try with

class AOS_QuotesController extends SugarController {

and check if it works. Do a Quick Repair and Rebuild.

Thanks for your reply. I have tried as you suggested but it does not work. In sugar doc http://support.sugarcrm.com/02_Documentation/04_Sugar_Developer/Sugar_Developer_Guide_6.5/03_Module_Framework/01_MVC/03_Controller/ it says that the custom controller should extends Controller.

Do you have something else to suggest? Maybe the custom controller is not the right way to address this problem

Oh I skipped AOS_Quotes already has a default controller. What view are you trying to custom code? There is another documentation I found in http://developer.sugarcrm.com/2011/02/07/howto-creating-custom-controller-and-view-classes/ you can see if this helps. It gives you the nomenclature for extending view classes as well.

Yes you may be correct that creating a custom controller might not be the answer.

I don’t think there is any logic look which executes on a relate field or else that could have solved your querry.

Another option is using javascript, you can fetch the id of the value in the contact’s relate field and then fetch its address using AJAX and prefill the address field. A bit of a complex process though.

I was able to solve my problem. I needed to modify both the Contact QuickSearch and the Popup to return the contact’s primary address to the fields billing address. I did as follows (thanks to http://johndopenotes.wordpress.com/2013/03/18/sugarcrm-populating-fields-using-a-relate-field/)

  • Fot the POPUP window: custom\modules\AOS_Quotes\metadata\ediviewdefs.php modified billing_contact field by adding
'displayParams' =>
            array (
              'initial_filter' => '&account_name="+this.form.{$fields.billing_account.name}.value+"',
              'field_to_name_array' =>
              array(
              'id' => 'billing_contact_id',
              'name' => 'billing_contact',
              'primary_address_street' => 'billing_address_street',
              'primary_address_city' => 'billing_address_city',
              'primary_address_state' => 'billing_address_state',
              'primary_address_postalcode' => 'billing_address_postalcode',
              'primary_address_country' => 'billing_address_country'
              ),
            ),
  • For QuickSearch: created a new file in custom\Extension\modules\AOS_Quotes\Ext\Vardefs\vardefs.ext.php
<?php
$dictionary['AOS_Quotes']['fields']['billing_contact']['populate_list'] = array('name', 'id', 'primary_address_street','primary_address_city','primary_address_state','primary_address_postalcode','primary_address_country');
$dictionary['AOS_Quotes']['fields']['billing_contact']['field_list'] = array('billing_contact', 'billing_contact_id', 'billing_address_street','billing_address_city','billing_address_state','billing_address_postalcode','billing_address_country');
?>

Quick Repair and Rebuild.

3 Likes

Good job.

Great! I have spent too much time trying to implement it but without success. Finally a good ( and working) solution.
thank you very very very much!!

Thanks!

I have another problem related to this one. When I start creating a new quote straight from Contacts module, it doesnt populate address fields. So I have to select Contact again (or open pop-up and make selection there) and after that fields are getting filled.

If you haven’t found a solution to this, the following worked for me.

In custom/modules/AOS_Quotes, edit controller.php and change:


if (isset($_REQUEST['contact_id'])) {
            		$query = "SELECT id,first_name,last_name FROM contacts WHERE id = '{$_REQUEST['contact_id']}'";
			$result = $this->bean->db->query($query, true);
			$row = $this->bean->db->fetchByAssoc($result);
			$this->bean->billing_contact_id = $row['id'];
			$this->bean->billing_contact = $row['first_name'].' '.$row['last_name'];
		}

To:


if (isset($_REQUEST['contact_id'])) {
            		$query = "SELECT * FROM contacts WHERE id = '{$_REQUEST['contact_id']}'";
			$result = $this->bean->db->query($query, true);
			$row = $this->bean->db->fetchByAssoc($result);
			$this->bean->billing_contact_id = $row['id'];
			$this->bean->billing_contact = $row['first_name'].' '.$row['last_name'];
			$this->bean->billing_address_street = $row['primary_address_street'];
			$this->bean->billing_address_city = $row['primary_address_city'];
			$this->bean->billing_address_state = $row['primary_address_state'];
			$this->bean->billing_address_postalcode = $row['primary_address_postalcode'];
			$this->bean->billing_address_country = $row['primary_address_country'];
		}

And then do a quick repair and rebuild.

1 Like