Answers.Body: <p>This sounds suspiciously like a bug that was fixed in today's <a href="https://github.com/craftcms/cms/blob/develop/CHANGELOG.md#3610---2021-03-09" rel="nofollow noreferrer">Craft 3.6.10</a> release.</p>
  <blockquote>
  <p>Fixed a bug where entries could return the wrong Matrix/Neo/Super Table blocks if the blocks were queried immediately after the entry was created via <code>craft\services\Elements::duplicateElement()</code>.</p>
  </blockquote>
  <p>My guess is if you update to that release, it'll behave like you expect now.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-10T13:13:00.710Z
Answers.Text: Yes I contacted craft support with this question and it was indeed this bug. They fixed it right away.
Answers.UserDisplayName: bartpulse
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-09T22:29:19.353Z
Answers.LastActivityDate: 2021-03-09T22:29:19.353Z
Answers.UserDisplayName: Brad Bell
Body: <p>I'm creating a cron job to automatically import new entries (vacancies).<br />
  I have an entry that I use as a template with some standard data fields and matrix fields. If it's an entry that has not been imported before I create a new entry as a duplicate of the template entry.</p>
  <pre><code>$entry = Craft::$app-&gt;getElements()-&gt;duplicateElement($entryTemplate);
  </code></pre>
  <p>The template already has the matrix blocks but only placeholder data that I want to overwrite like this:</p>
  <pre><code>// existing matrix fields (already created or from template)
  $matrixBlocks = $entry-&gt;myMatrixBlock;
  
  /** @var MatrixBlock $matrixBlock */
  foreach($matrixBlocks as $matrixBlock) {
      $matrixBlock-&gt;setFieldValues(['contactText' =&gt; 'random text']);
  
      $success = Craft::$app-&gt;elements-&gt;saveElement($matrixBlock);
  }
  </code></pre>
  <p>The strange thing is that the ownerId of the matrixBlock isn't the value of the $entry-&gt;id nor is it the value of the template id. It generated a new ownerId.
  It works correctly when I import it a second time because the owner id is now correct.</p>
  
Tags: <craft3>
Title: Saving matrix field of a duplicate of an entry not working
ThreadText: # Saving matrix field of a duplicate of an entry not working
  
  (Asked by: username_0 on 2021-03-08)
  
  I'm creating a cron job to automatically import new entries (vacancies).  
  I have an entry that I use as a template with some standard data fields and matrix fields. If it's an entry that has not been imported before I create a new entry as a duplicate of the template entry.
  
  ```
  $entry = Craft::$app->getElements()->duplicateElement($entryTemplate);
  
  ```
  
  The template already has the matrix blocks but only placeholder data that I want to overwrite like this:
  
  ```
  // existing matrix fields (already created or from template)
  $matrixBlocks = $entry->myMatrixBlock;
  
  /** @var MatrixBlock $matrixBlock */
  foreach($matrixBlocks as $matrixBlock) {
      $matrixBlock->setFieldValues(['contactText' => 'random text']);
  
      $success = Craft::$app->elements->saveElement($matrixBlock);
  }
  
  ```
  
  The strange thing is that the ownerId of the matrixBlock isn't the value of the $entry->id nor is it the value of the template id. It generated a new ownerId.
  It works correctly when I import it a second time because the owner id is now correct.
  
  **Tags:** craft3
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-03-09. Score: 1 | Accepted answer
  
  This sounds suspiciously like a bug that was fixed in today's [Craft 3.6.10](https://github.com/craftcms/cms/blob/develop/CHANGELOG.md#3610---2021-03-09) release.
  
  > Fixed a bug where entries could return the wrong Matrix/Neo/Super Table blocks if the blocks were queried immediately after the entry was created via `craft\services\Elements::duplicateElement()`.
  
  My guess is if you update to that release, it'll behave like you expect now.
  
  > **Comments:**
  > **username_0**: Yes I contacted craft support with this question and it was indeed this bug. They fixed it right away.  
  
  ---
  
  