Answers.Body: <p>For relationship fields (Entries, Assets, Users, Tags, etc.) you need to pass in an array of element IDs.</p>
  
  <p>Something like this should work.</p>
  
  <pre><code>$entry-&gt;setAttribute('companyField', array($companyEntry-&gt;id));
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-31T19:36:24.890Z
Answers.Text: cool, that works fine, i'll mark your answer as correct but could you tell me how can i do if i'm creating a new entry, i mean, i don't know how can i get the fields from that new entry i'm about to create. thanks
Answers.UserDisplayName: user2996243
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-31T19:39:45.913Z
Answers.Text: for entries that are currently created i can use the entryId to get the entry and update that entry field, but in this case i don't have an entryId
Answers.UserDisplayName: user2996243
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-01T03:36:47.307Z
Answers.Text: I don't know your front-end workflow, but either a user is selecting an already existing entry for the relationship to work, or your plugin is creating it at runtime before you save it, so your main `$entry` should have knowledge of it.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-31T19:15:57.773Z
Answers.LastActivityDate: 2016-08-31T19:15:57.773Z
Answers.UserDisplayName: Brad Bell
Body: <p>I need to set programmatically the value of an Entries field type when I'm going to create a new entry from the admin form, after the form loads I'm searching for the entry I need to set in the Entries field, so I'm making something like <code>setAttribute('companyField', $companyEntry)</code> but it didn't work.</p>
  
  <p>I need to find a way to set/lookup that value in the Entries field type so the user can see it in the UI.</p>
  
Tags: <entries-field>
Title: How can I programmatically set the value of an Entries field type?
ThreadText: # How can I programmatically set the value of an Entries field type?
  
  (Asked by: username_0 on 2016-08-30)
  
  I need to set programmatically the value of an Entries field type when I'm going to create a new entry from the admin form, after the form loads I'm searching for the entry I need to set in the Entries field, so I'm making something like `setAttribute('companyField', $companyEntry)` but it didn't work.
  
  I need to find a way to set/lookup that value in the Entries field type so the user can see it in the UI.
  
  **Tags:** entries-field
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-08-31. Score: 1 | Accepted answer
  
  For relationship fields (Entries, Assets, Users, Tags, etc.) you need to pass in an array of element IDs.
  
  Something like this should work.
  
  ```
  $entry->setAttribute('companyField', array($companyEntry->id));
  
  ```
  
  > **Comments:**
  > **username_0**: cool, that works fine, i'll mark your answer as correct but could you tell me how can i do if i'm creating a new entry, i mean, i don't know how can i get the fields from that new entry i'm about to create. thanks  
  > **username_0**: for entries that are currently created i can use the entryId to get the entry and update that entry field, but in this case i don't have an entryId  
  > **username_1**: I don't know your front-end workflow, but either a user is selecting an already existing entry for the relationship to work, or your plugin is creating it at runtime before you save it, so your main `$entry` should have knowledge of it.  
  
  ---
  
  