Answers.Body: <p>The way we ended up getting this working is below. Seems you need to send a title or set the flag to false for validation - if you do that it then set a default title.</p>
  <pre><code>Ensuring the element has a title if its type has titles, and giving it a default title in the event that $validateContent is set to false
  </code></pre>
  <p><a href="https://docs.craftcms.com/api/v3/craft-services-elements.html#method-saveelement" rel="nofollow noreferrer">https://docs.craftcms.com/api/v3/craft-services-elements.html#method-saveelement</a></p>
  <pre><code>//User id
  $userId = 7780;
  $user = Craft::$app-&gt;users-&gt;getUserById($userId);
  
  //Could also get the users form something like the below:
  //$user = Craft::$app-&gt;users-&gt;getUserByUsernameOrEmail($usersEmail);//users email
  //$user = Craft::$app-&gt;users-&gt;getUserById($userId);//users ID
  //$user = Craft::$app-&gt;getUser()-&gt;getIdentity();//curent user
  
  //Set the new data to be saved:
  $user-&gt;setFieldValue('userCompanyName', 'update text here');
  
  //Save:
  $save = Craft::$app-&gt;getElements()-&gt;saveElement($user, false);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-08-17T09:05:11.387Z
Answers.LastActivityDate: 2021-08-17T09:05:11.387Z
Answers.UserDisplayName: Green17
Answers.Body: <p>You should just reference the field as an object property of the user instead of using <code>setFieldValue</code>. I also use the getElements function instead of using the property, but I don't think that makes much difference. The code below should work.</p>
  <pre><code>public function actionUpdateuser() {
      // Update user data
      $userId = 67852;
      $user = Craft::$app-&gt;users-&gt;getUserById($userId);
      $user-&gt;userCompanyName = 'New Name 2'; 
      $result = Craft::$app-&gt;getElements()-&gt;saveElement($user);
  
      return $result;
  }
  </code></pre>
  <p>Also make sure to add the Craft Users class at the top of the page with:</p>
  <pre><code>use craft\elements\User;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-09-01T12:51:32.333Z
Answers.Text: Thanks, Steven, sorry some how missed this coming in.
Answers.UserDisplayName: Green17
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-08-18T14:35:35.693Z
Answers.LastActivityDate: 2020-08-18T14:43:17.540Z
Answers.LastEditDate: 2020-08-18T14:43:17.540Z
Answers.UserDisplayName: Steven B
Body: <p>Does anyone know if there is a block on updating users form an action?</p>
  
  <p>Trying to update a users custom field in craft 3 via plugin in the admin. The admin template calls an ajax call to the action when a drop-down is changed. </p>
  
  <p>This works ok in the base of the plugin, but not in an action.</p>
  
  <pre><code>    public function actionUpdateuser()
      {
          // Update user data
          $userId = 67852;
          $user = Craft::$app-&gt;users-&gt;getUserById($userId);
          $user-&gt;setFieldValue('userCompanyName', 'New Name 2'); 
          $result = Craft::$app-&gt;elements-&gt;saveElement($user);
  
          return $result;
      }
  </code></pre>
  
  <p>The action fires ok and can return other things just does not update the user.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-02-09T10:16:05.240Z
Comments.Text: What type of field is your `userCompanyName`?
Comments.UserDisplayName: Oli
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-02-09T14:19:23.687Z
Comments.Text: It's just a plain text field. It works ok if I add the same code to the Custom.php which is the main plugin file.
Comments.UserDisplayName: Green17
Tags: <plugin-development><craft3>
Title: Editing a users custom fields from a plugin action Craft 3
ThreadText: # Editing a users custom fields from a plugin action Craft 3
  
  (Asked by: username_0 on 2020-02-09)
  
  Does anyone know if there is a block on updating users form an action?
  
  Trying to update a users custom field in craft 3 via plugin in the admin. The admin template calls an ajax call to the action when a drop-down is changed.
  
  This works ok in the base of the plugin, but not in an action.
  
  ```
      public function actionUpdateuser()
      {
          // Update user data
          $userId = 67852;
          $user = Craft::$app->users->getUserById($userId);
          $user->setFieldValue('userCompanyName', 'New Name 2'); 
          $result = Craft::$app->elements->saveElement($user);
  
          return $result;
      }
  
  ```
  
  The action fires ok and can return other things just does not update the user.
  
  **Tags:** plugin-development, craft3
  
  > **Comments:**
  > **username_1**: What type of field is your `userCompanyName`?  
  > **username_0**: It's just a plain text field. It works ok if I add the same code to the Custom.php which is the main plugin file.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_0 on 2021-08-17. Score: 2 | Accepted answer
  
  The way we ended up getting this working is below. Seems you need to send a title or set the flag to false for validation - if you do that it then set a default title.
  
  ```
  Ensuring the element has a title if its type has titles, and giving it a default title in the event that $validateContent is set to false
  
  ```
  
  <https://docs.craftcms.com/api/v3/craft-services-elements.html#method-saveelement>
  
  ```
  //User id
  $userId = 7780;
  $user = Craft::$app->users->getUserById($userId);
  
  //Could also get the users form something like the below:
  //$user = Craft::$app->users->getUserByUsernameOrEmail($usersEmail);//users email
  //$user = Craft::$app->users->getUserById($userId);//users ID
  //$user = Craft::$app->getUser()->getIdentity();//curent user
  
  //Set the new data to be saved:
  $user->setFieldValue('userCompanyName', 'update text here');
  
  //Save:
  $save = Craft::$app->getElements()->saveElement($user, false);
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2020-08-18. Score: 3
  
  You should just reference the field as an object property of the user instead of using `setFieldValue`. I also use the getElements function instead of using the property, but I don't think that makes much difference. The code below should work.
  
  ```
  public function actionUpdateuser() {
      // Update user data
      $userId = 67852;
      $user = Craft::$app->users->getUserById($userId);
      $user->userCompanyName = 'New Name 2'; 
      $result = Craft::$app->getElements()->saveElement($user);
  
      return $result;
  }
  
  ```
  
  Also make sure to add the Craft Users class at the top of the page with:
  
  ```
  use craft\elements\User;
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks, username_2, sorry some how missed this coming in.  
  
  ---
  
  