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?


1 Answer 1


The solution is to use 'data' instead of 'val':

CRM.$("#s2id_custom_13_-1").select2('data', { "id": 188, "text":"NAME OF THE CONTACT"} );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.