<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>

CC BY-SA 3.0
2016-08-31T19:36:24.890Z
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
user2996243
CC BY-SA 3.0
2016-08-31T19:39:45.913Z
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
user2996243
CC BY-SA 3.0
2016-09-01T03:36:47.307Z
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.
Brad Bell
CC BY-SA 3.0
2016-08-31T19:15:57.773Z
2016-08-31T19:15:57.773Z
Brad Bell
<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>

<entries-field>
How can I programmatically set the value of an Entries field type?
# 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.  

---

