Answers.Body: <p>If you are going to set the fieldValue, you need to save the element:</p>
  
  <pre><code>    $userObject = Craft::$app-&gt;users-&gt;getUserById($userId);
      $userObject-&gt;setFieldValue('discountPercentage', 15); 
      Craft::$app-&gt;elements-&gt;saveElement($userObject);
  </code></pre>
  
  <p>Should do the trick.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-05T05:54:25.003Z
Answers.Text: That works, the only exception being that I needed to remove `one()` when getting the `$userObject`. Thanks!
Answers.UserDisplayName: Stian Karlsen
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-05-15T22:07:22.980Z
Answers.Text: Good point @StianKarlsen; edited it appropriately...
Answers.UserDisplayName: Jalen Davenport
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-02-04T17:08:01.893Z
Answers.LastActivityDate: 2020-05-15T22:06:38.410Z
Answers.LastEditDate: 2020-05-15T22:06:38.410Z
Answers.UserDisplayName: Brett Moore
Body: <p>I've got this</p>
  
  <pre><code>    // Creating users through frontend
      Event::on(Users::class, Users::EVENT_BEFORE_ACTIVATE_USER, function(UserEvent $event) {
          $userId = $event-&gt;user-&gt;id;
  
          // Allow assigning user groups through the frontend (circumvent elevated user sessions)
          $groupId = Craft::$app-&gt;getRequest()-&gt;getBodyParam('userGroup');
          if ($groupId) {
              Craft::$app-&gt;getUsers()-&gt;assignUserToGroups($userId, [$groupId]);
          }
  
          // This does not work
          Craft::$app-&gt;users-&gt;getUserById($userId)-&gt;setFieldValue('discountPercentage', 15);
      });
  </code></pre>
  
  <p>Assigning the user group works fine, so I don't get why assigning a field value doesn't. The <code>discountPercentage</code> field is a simple Number fieldtype</p>
  
  <p>I also tried <code>$event-&gt;user-&gt;setFieldValue('discountPercentage', 15);</code> without success.</p>
  
  <p>Also I tried surrounding <code>15</code> in quotes, like <code>'15'</code>, but that didn't work either.</p>
  
  <p>Edit:</p>
  
  <p>Submitting the "create user form" through the frontend gives no errors. <code>discountPercentage</code> is just left blank.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-01-20T14:38:11.620Z
Comments.Text: Why the downvote? If there is a better way for me to ask for help, please tell me so I can improve. That would be constructive, rather than downvoting without explaining why. Thanks.
Comments.UserDisplayName: Stian Karlsen
Tags: <plugin-development><craft3>
Title: Can't get setFieldValue to work
ThreadText: # Can't get setFieldValue to work
  
  (Asked by: username_0 on 2019-01-10)
  
  I've got this
  
  ```
      // Creating users through frontend
      Event::on(Users::class, Users::EVENT_BEFORE_ACTIVATE_USER, function(UserEvent $event) {
          $userId = $event->user->id;
  
          // Allow assigning user groups through the frontend (circumvent elevated user sessions)
          $groupId = Craft::$app->getRequest()->getBodyParam('userGroup');
          if ($groupId) {
              Craft::$app->getUsers()->assignUserToGroups($userId, [$groupId]);
          }
  
          // This does not work
          Craft::$app->users->getUserById($userId)->setFieldValue('discountPercentage', 15);
      });
  
  ```
  
  Assigning the user group works fine, so I don't get why assigning a field value doesn't. The `discountPercentage` field is a simple Number fieldtype
  
  I also tried `$event->user->setFieldValue('discountPercentage', 15);` without success.
  
  Also I tried surrounding `15` in quotes, like `'15'`, but that didn't work either.
  
  Edit:
  
  Submitting the "create user form" through the frontend gives no errors. `discountPercentage` is just left blank.
  
  **Tags:** plugin-development, craft3
  
  > **Comments:**
  > **username_0**: Why the downvote? If there is a better way for me to ask for help, please tell me so I can improve. That would be constructive, rather than downvoting without explaining why. Thanks.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-02-04. Score: 7 | Accepted answer
  
  If you are going to set the fieldValue, you need to save the element:
  
  ```
      $userObject = Craft::$app->users->getUserById($userId);
      $userObject->setFieldValue('discountPercentage', 15); 
      Craft::$app->elements->saveElement($userObject);
  
  ```
  
  Should do the trick.
  
  > **Comments:**
  > **username_0**: That works, the only exception being that I needed to remove `one()` when getting the `$userObject`. Thanks!  
  > **username_2**: Good point @username_0; edited it appropriately...  
  
  ---
  
  