Skip to main content
I rephrased my question because CRM.$(A-SELECT2-FIELD).select2('val', MY-VAL) seems to give an error on a custom field.
Source Link
AlainB
  • 563
  • 3
  • 12

Fill in custom field of type contact reference field from javascript? returns TypeError

I have a custom field, which is a contact reference field.

Based on what the user fills in on the form, I would like to fill in the contact reference field with a specific contact. Leaving the user the choice of keeping that contact, or selecting another one.

The html code of the reference field is:

<div class="select2-container crm-form-contact-reference huge crm-ajax-select" id="s2id_custom_21_-1" ...

Let's say that based on some values on the form, I want to fill in the contact with ID 188 in that contact reference field.

How can I should be able to do thatthis with:

CRM.$("#s2id_custom_21_-1").select2('val', '188')

However, this returns the error "TypeError: row is undefined" in javascriptCommon.js on line 387.

The error can be reproduced in the CiviCRM demo environment by following these steps:

  • go to Administer > Customize Data and Screens > Custom Fields

  • click on "View and Edit Custom Fields" for the set "Food Preferences" (used for Participants, and linked to the Fall Fundraiser Dinner event)

  • add a custom field of type "contact reference"

  • then go to Events > Register Event Participant

  • select "Fall Fundraiser Dinner" in event

  • then press F12 top open the developer tools of your browser

  • in the console type:

    CRM.$("#s2id_contact_id").select2('val', '188')

  • this should fill in "Wattson, Laree" in the contact field, which is good

  • find out the id of the custom field on that form (it should be at the bottom of the screen in the "Food preference" section). In my case it was s2id_custom_13_-1

  • in the console type:

    CRM.$("#s2id_custom_13_-1").select2('val', '188')

==> you shoud see TypeError: row is undefined [Learn More] in the console

Looks like a bug to me. Or am I doing something wrong?

Fill in contact reference field from javascript?

I have a custom field, which is a contact reference field.

Based on what the user fills in on the form, I would like to fill in the contact reference field with a specific contact. Leaving the user the choice of keeping that contact, or selecting another one.

The html code of the reference field is:

<div class="select2-container crm-form-contact-reference huge crm-ajax-select" id="s2id_custom_21_-1" ...

Let's say that based on some values on the form, I want to fill in the contact with ID 188 in that contact reference field.

How can I do that in javascript?

Fill in custom field of type contact reference field from javascript returns TypeError

I have a custom field, which is a contact reference field.

Based on what the user fills in on the form, I would like to fill in the contact reference field with a specific contact. Leaving the user the choice of keeping that contact, or selecting another one.

The html code of the reference field is:

<div class="select2-container crm-form-contact-reference huge crm-ajax-select" id="s2id_custom_21_-1" ...

Let's say that based on some values on the form, I want to fill in the contact with ID 188 in that contact reference field.

I should be able to do this with:

CRM.$("#s2id_custom_21_-1").select2('val', '188')

However, this returns the error "TypeError: row is undefined" in Common.js on line 387.

The error can be reproduced in the CiviCRM demo environment by following these steps:

  • go to Administer > Customize Data and Screens > Custom Fields

  • click on "View and Edit Custom Fields" for the set "Food Preferences" (used for Participants, and linked to the Fall Fundraiser Dinner event)

  • add a custom field of type "contact reference"

  • then go to Events > Register Event Participant

  • select "Fall Fundraiser Dinner" in event

  • then press F12 top open the developer tools of your browser

  • in the console type:

    CRM.$("#s2id_contact_id").select2('val', '188')

  • this should fill in "Wattson, Laree" in the contact field, which is good

  • find out the id of the custom field on that form (it should be at the bottom of the screen in the "Food preference" section). In my case it was s2id_custom_13_-1

  • in the console type:

    CRM.$("#s2id_custom_13_-1").select2('val', '188')

==> you shoud see TypeError: row is undefined [Learn More] in the console

Looks like a bug to me. Or am I doing something wrong?

Source Link
AlainB
  • 563
  • 3
  • 12

Fill in contact reference field from javascript?

I have a custom field, which is a contact reference field.

Based on what the user fills in on the form, I would like to fill in the contact reference field with a specific contact. Leaving the user the choice of keeping that contact, or selecting another one.

The html code of the reference field is:

<div class="select2-container crm-form-contact-reference huge crm-ajax-select" id="s2id_custom_21_-1" ...

Let's say that based on some values on the form, I want to fill in the contact with ID 188 in that contact reference field.

How can I do that in javascript?