Auto Populate Contact Phone Number When User Is Selected

Just finished the Developer Master Class Series … extremely useful for anyone that is thinking of it.

But here is my specific question.

I have added a relate field to the Cases Module and Added it to the Edit View Template.

This works great. I am able to select a contact using the little arrow beside the contact_c field in cases

The issue is that our technicians need the contact phone number as well not just the First Name and Last Name of the Contact.

So in the Cases module I have created another custom field contact_phone_work_c

What I would like the system to do is when the user clicks the arrow and selects the contact from the popup menu contact_phone_work_c is populated with the selected contacts work phone number.

I have been able to use the bean and do a query that populates this information AFTER it has been saved and reloaded by populating it with a query. But this is not an acceptable solution for our needs.

I have also found a bunch of outdated and dead links on this so if someone could help with a current solution that would be greatly appreciated.

I am assuming I am going to have to do some Javascript work similar to line_items.js but I am not really sure where to go with this because of the conflicting information in the google searches.

Thanks in advance.
Scott

Hi Scott,

Glad you enjoyed the master class!

You can have fields from the contact populate the fields in case. In the editviewdefs.php of the case you can set the field_to_name_array.

As an example (from memory, may be slightly incorrect):

0 => array(
                    'name' => 'contact_c',//Contact field
                    'displayParams' =>
                        array (
                            'field_to_name_array' => array(
                                'id'=>'contact_c_id',//Check the case vardef cache for the exact id field
                                'name'=>'contact_c_name',//Check the case vardef cache for the exact name field
                                'phone_work' => 'contact_phone_work_c',// contact field => case field to populate
                            ),
                            'additionalFields' => array(
                                'phone_work' => 'contact_phone_work_c',
                            ),
                        ),
                ),

Hope this helps,
Jim


  3 => 
        array(
            0 => '',
            1 =>
                 array(
                        'name' => 'contact_c',
                        'studio' => 'visible',
                        'label' => 'LBL_CONTACT',
                        'displayParams' =>
                        array(
                            'field_to_name_array' => array(
                                'id' => 'contact_c_id', //Check the case vardef cache for the exact id field
                                'name' => 'contact_c_name', //Check the case vardef cache for the exact name field
                                'phone_work' => 'primary_phone_c', // contact field => case field to populate
                                'phone_mobile' => 'secondary_phone_c' // add mobile as well
                            ),
                            'additionalFields' => array(
                                'phone_work' => 'primary_phone_c',
                                'phone_mobile' => 'secondary_phone_c',
                            ),
                        ),
                    ),
                ),
// next field...
        4 => 

This worked for me perfectly. And I added the mobile as well. Thank you.

above post added the phone numbers but not the contact name:

updated code


  3 => 
        array(
            0 => '',
            1 =>
                 array(
                        'name' => 'contact_c',
                        'studio' => 'visible',
                        'label' => 'LBL_CONTACT',
                        'displayParams' =>
                        array(
                            'field_to_name_array' => array(
                                'id' => 'contact_c_id', //Check the case vardef cache for the exact id field
                                'name' => 'contact_c', //Check the case vardef cache for the exact name field
                                'phone_work' => 'primary_phone_c', // contact field => case field to populate
                                'phone_mobile' => 'secondary_phone_c' // add mobile as well
                            ),
                            'additionalFields' => array(
                                'phone_work' => 'primary_phone_c',
                                'phone_mobile' => 'secondary_phone_c',
                            ),
                        ),
                    ),
                ),

upon further testing this doesn’t quite work.

It appears to work. Changes the name. Populates the phone number(s) which is the behavior I am after.

however when I click save and then go back into the record. The proper phone numbers show up but the Account still has the old account information.

also if i create a new invoice and select a name and click save I get the error message

“No match for field: Contact”

please help

It sounds like maybe the field for id (contact_c_id) isn’t quite correct. Can you have a look in the source of the page for the contact relate field. There should be a hidden input which stores the contact id. Ensure that the id of the field is the id that you are using for contact_c_id.

Thanks,
Jim

it appears to be contact_id_c and it does contact the hidden field as you describe.

I am wondering if is has something to do with the autocomplete being off?


<td valign="top" width="37.5%" colspan="3">
<input type="text" name="contact_c" class="sqsEnabled yui-ac-input" tabindex="0" id="contact_c" size="" value="Test Contact" title="" autocomplete="off"><div id="EditView_contact_c_results" class="yui-ac-container"><div class="yui-ac-content" style="display: none;"><div class="yui-ac-hd" style="display: none;"></div><div class="yui-ac-bd"><ul><li style="display: none;"></li><li style="display: none;"></li><li style="display: none;"></li><li style="display: none;"></li><li style="display: none;"></li><li style="display: none;"></li><li style="display: none;"></li><li style="display: none;"></li><li style="display: none;"></li><li style="display: none;"></li></ul></div><div class="yui-ac-ft" style="display: none;"></div></div></div>
<input type="hidden" name="contact_id_c" id="contact_id_c" value="134334ec-193e-e61c-1cbc-5579ee700ccc">
<span class="id-ff multiple">
<button type="button" name="btn_contact_c" id="btn_contact_c" tabindex="0" title="Select Contact" class="button firstChild" value="Select Contact" onclick="open_popup(
&quot;Contacts&quot;, 
600, 
400, 
&quot;&quot;, 
true, 
false, 
{&quot;call_back_function&quot;:&quot;set_return&quot;,&quot;form_name&quot;:&quot;EditView&quot;,&quot;field_to_name_array&quot;:{&quot;id&quot;:&quot;contact_c_id&quot;,&quot;name&quot;:&quot;contact_c&quot;,&quot;phone_work&quot;:&quot;primary_phone_c&quot;}}, 
&quot;single&quot;, 
true
);"><img src="themes/default/images/id-ff-select.png?v=8_tq6BOS19ZfSQowFK0TYg"></button><button type="button" name="btn_clr_contact_c" id="btn_clr_contact_c" tabindex="0" title="Clear Contact" class="button lastChild" onclick="SUGAR.clearRelateField(this.form, 'contact_c', 'contact_id_c');" value="Clear Contact"><img src="themes/default/images/id-ff-clear.png?v=8_tq6BOS19ZfSQowFK0TYg"></button>
</span>
<script type="text/javascript">
SUGAR.util.doWhen(
		"typeof(sqs_objects) != 'undefined' && typeof(sqs_objects['EditView_contact_c']) != 'undefined'",
		enableQS
);
</script>
</td>

i was using contact_c_id instead of contact_id_c

this works hope it helps someone


 3 =>
        array (
          0 => '',
		  1 =>
          array (
            'name' => 'contact_c',
            'studio' => 'visible',
            'label' => 'LBL_CONTACT',
			  
			 'displayParams' =>
                        array(
                            'field_to_name_array' => array(
                                'id' => 'contact_id_c', //Check the case vardef cache for the exact id field
                                'name' => 'contact_c', //Check the case vardef cache for the exact name field
                                'phone_work' => 'primary_phone_c', // contact field => case field to populate
                            ),
                            'additionalFields' => array(
                                'phone_work' => 'primary_phone_c',
                            ),
                        ),  
          ),
        ),

Hello,

Thank you for your post, I am trying for 3 days but no use. Finally your post is helped me allot.

And for phone number it is working fine but i have a problem with email.

How can i populate email from contact.

Please help me out.

Regards,
Udaya

Hello,

Thank you for your post, I am trying for 3 days but no use. Finally your post is helped me allot.

And for phone number it is working fine but i have a problem with email.

How can i populate email from contact.

Please help me out.

Regards,
Udaya

I tried this solution but it does not work, my code is

‘displayParams’ => array (
‘field_to_name_array’ => array (
‘id’ => ‘contact_id_c’,
‘name’ => ‘client’,
‘age_c’ => ‘client_age_c’,
),
‘additionalFields’ => array (
‘age_c’ => ‘client_age_c’,
),

I use suitecrm 7.9.2

Please help me.