Answers.Body: <p>After taking a look again, it turned out to simply be an issue with the scope of the array and a therefore empty array as a result. Works as expected now.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-07-17T12:28:41.473Z
Answers.LastActivityDate: 2017-07-17T12:35:13.000Z
Answers.LastEditDate: 2017-07-17T12:35:13.000Z
Answers.UserDisplayName: justussimon91
Body: <p>I'm trying to add an asset to an entry's assetfield when the entry is saved by using the <code>onBeforeSaveEntry</code> service provided by Craft.</p>
  
  <pre><code>craft()-&gt;on('entries.onBeforeSaveEntry', function(Event $event) use ($fileIds)
    {
        $entry = $event-&gt;params['entry'];
        if ($entry-&gt;section['handle'] == "bildupload" &amp;&amp; $event-&gt;params["isNewEntry"]){
          $entry-&gt;getContent()-&gt;useruploads = $fileIds;
        }
    });
  </code></pre>
  
  <p>Contents of the <code>$fileIds</code>-array are created like so:</p>
  
  <pre><code>$copyrightedImage = craft()-&gt;assets-&gt;insertFileByLocalPath(
        $tempPath,
        $originalFile,
        $myAssetFolderId,
        AssetConflictResolution::KeepBoth
  );
  
  if ($copyrightedImage-&gt;isSuccess()){
        $fileIds[] = $copyrightedImage-&gt;getDataItem('fileId');
  }
  </code></pre>
  
  <p>This works pretty well for fields such as a formatted text field or the title, but I cant figure out how to change the asset inside an asset field. 
  The assetfield of the entries in the section <code>bildupload</code> goes by the handle <code>useruploads</code>. How would I go about fetching the assetId, the full Url to the photo or whatever is used here and add the desired photo into the assetfield as an additional used asset?</p>
  
  <p>Thanks for any help or hints.</p>
  
  <p>Edit: I tried following what Brandon described <a href="https://craftcms.stackexchange.com/questions/4175/create-entry-with-newly-uploaded-asset-programatically">here</a>, but to no avail.
  There are no errors thrown, however the assetfield of the entry remains completely empty and does not have any image assigned to it. I'm using closures to use the array <code>$fileIds</code> in the functions shown below.</p>
  
Tags: <assets><services><onsaveentry>
Title: Changing asset of entry's assetfield via entries.onBeforeSaveEntry
ThreadText: # Changing asset of entry's assetfield via entries.onBeforeSaveEntry
  
  (Asked by: username_0 on 2017-07-14)
  
  I'm trying to add an asset to an entry's assetfield when the entry is saved by using the `onBeforeSaveEntry` service provided by Craft.
  
  ```
  craft()->on('entries.onBeforeSaveEntry', function(Event $event) use ($fileIds)
    {
        $entry = $event->params['entry'];
        if ($entry->section['handle'] == "bildupload" && $event->params["isNewEntry"]){
          $entry->getContent()->useruploads = $fileIds;
        }
    });
  
  ```
  
  Contents of the `$fileIds`-array are created like so:
  
  ```
  $copyrightedImage = craft()->assets->insertFileByLocalPath(
        $tempPath,
        $originalFile,
        $myAssetFolderId,
        AssetConflictResolution::KeepBoth
  );
  
  if ($copyrightedImage->isSuccess()){
        $fileIds[] = $copyrightedImage->getDataItem('fileId');
  }
  
  ```
  
  This works pretty well for fields such as a formatted text field or the title, but I cant figure out how to change the asset inside an asset field.
  The assetfield of the entries in the section `bildupload` goes by the handle `useruploads`. How would I go about fetching the assetId, the full Url to the photo or whatever is used here and add the desired photo into the assetfield as an additional used asset?
  
  Thanks for any help or hints.
  
  Edit: I tried following what Brandon described [here](https://craftcms.stackexchange.com/questions/4175/create-entry-with-newly-uploaded-asset-programatically), but to no avail.
  There are no errors thrown, however the assetfield of the entry remains completely empty and does not have any image assigned to it. I'm using closures to use the array `$fileIds` in the functions shown below.
  
  **Tags:** assets, services, onsaveentry
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2017-07-17. Score: 1 | Accepted answer
  
  After taking a look again, it turned out to simply be an issue with the scope of the array and a therefore empty array as a result. Works as expected now.
  
  
  ---
  
  