Answers.Body: <p>The solution is to use 'data' instead of 'val':</p>
  
  <p>CRM.$("#s2id_custom_13_-1").select2('data', { "id": 188, "text":"NAME OF THE CONTACT"} );</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-09-26T13:51:45.280Z
Answers.LastActivityDate: 2017-09-26T13:51:45.280Z
Answers.UserDisplayName: AlainB
Body: <p>I have a custom field, which is a contact reference field.</p>
  
  <p>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.</p>
  
  <p>The html code of the reference field is:</p>
  
  <pre><code>&lt;div class="select2-container crm-form-contact-reference huge crm-ajax-select" id="s2id_custom_21_-1" ...
  </code></pre>
  
  <p>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.</p>
  
  <p>I should be able to do this with:</p>
  
  <pre><code>CRM.$("#s2id_custom_21_-1").select2('val', '188')
  </code></pre>
  
  <p>However, this returns the error "TypeError: row is undefined" in Common.js on line 387.</p>
  
  <p>The error can be reproduced in the CiviCRM demo environment by following these steps:</p>
  
  <ul>
  <li>go to Administer > Customize Data and Screens > Custom Fields</li>
  <li>click on "View and Edit Custom Fields" for the set "Food Preferences" (used for Participants, and linked to the Fall Fundraiser Dinner event)</li>
  <li>add a custom field of type "contact reference"</li>
  <li>then go to Events > Register Event Participant</li>
  <li>select "Fall Fundraiser Dinner" in event</li>
  <li>then press F12 top open the developer tools of your browser</li>
  <li><p>in the console type: </p>
  
  <p>CRM.$("#s2id_contact_id").select2('val', '188')</p></li>
  <li><p>this should fill in "Wattson, Laree" in the contact field, which is good</p></li>
  <li><p>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</p></li>
  <li><p>in the console type:</p>
  
  <p>CRM.$("#s2id_custom_13_-1").select2('val', '188')</p></li>
  </ul>
  
  <p>==> you shoud see TypeError: row is undefined [Learn More] in the console</p>
  
  <p>Looks like a bug to me. Or am I doing something wrong?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-08-08T14:29:52.993Z
Comments.Text: Possible duplicates: https://civicrm.stackexchange.com/questions/19813/how-can-i-trigger-a-selection-for-a-select2-entityref-element-via-javascript and https://civicrm.stackexchange.com/questions/18627/how-to-select-multiple-values-in-select2-control-by-jquery
Comments.UserDisplayName: Sean
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-08-08T14:31:23.943Z
Comments.Text: Possible duplicate of [How can I trigger a selection for a Select2 EntityRef element via javascript?](https://civicrm.stackexchange.com/questions/19813/how-can-i-trigger-a-selection-for-a-select2-entityref-element-via-javascript)
Comments.UserDisplayName: Sean
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-08-09T08:50:02.950Z
Comments.Text: @SeanMadsen: I rephrased my question because the solution in the possible duplicates you mentioned, doesn't seem to work on custom fields.
Comments.UserDisplayName: AlainB
Tags: <javascript><contact-reference>
Title: Fill in custom field of type contact reference field from javascript returns TypeError
ThreadText: # Fill in custom field of type contact reference field from javascript returns TypeError
  
  (Asked by: username_0 on 2017-08-08)
  
  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:
  
  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?
  
  **Tags:** javascript, contact-reference
  
  > **Comments:**
  > **username_1**: Possible duplicates: https://civicrm.stackexchange.com/questions/19813/how-can-i-trigger-a-selection-for-a-select2-entityref-element-via-javascript and https://civicrm.stackexchange.com/questions/18627/how-to-select-multiple-values-in-select2-control-by-jquery  
  > **username_1**: Possible duplicate of [How can I trigger a selection for a Select2 EntityRef element via javascript?](https://civicrm.stackexchange.com/questions/19813/how-can-i-trigger-a-selection-for-a-select2-entityref-element-via-javascript)  
  > **username_0**: @username_1Madsen: I rephrased my question because the solution in the possible duplicates you mentioned, doesn't seem to work on custom fields.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2017-09-26. Score: 0 | Accepted answer
  
  The solution is to use 'data' instead of 'val':
  
  CRM.$("#s2id\_custom\_13\_-1").select2('data', { "id": 188, "text":"NAME OF THE CONTACT"} );
  
  
  ---
  
  