Answers.Body: <p>Yes, it's possible via the special Twig <a href="https://twig.symfony.com/doc/2.x/tags/do.html" rel="nofollow noreferrer"><code>do</code></a> tag, which basically enables you to call methods without returning (i.e. outputting) anything (which is what happens with nearly every other Twig expression).</p>
  <p>Assuming the &quot;users field&quot; is a custom field attached to the users' field layout (and you've got a <code>user</code> variable in your Twig template referring to a <a href="https://docs.craftcms.com/api/v3/craft-elements-user.html#public-properties" rel="nofollow noreferrer">User</a> element), something like the below should work:</p>
  <pre><code>{% do user.setFieldValue('fieldHandle', 123456) %}
  {% do craft.app.elements.saveElement(user) %}
  </code></pre>
  <p>What the above does, is first setting the custom field value for a field with the handle <code>fieldHandle</code> to a value <code>123456</code> on the <code>user</code> element, via the <a href="https://docs.craftcms.com/api/v3/craft-base-element.html#public-methods" rel="nofollow noreferrer"><code>setFieldValue()</code></a> method (which is available for all Craft element types). Then, the user is (re)saved using the <a href="https://docs.craftcms.com/api/v3/craft-services-elements.html#public-methods" rel="nofollow noreferrer"><code>Elements::saveElement()</code></a> method (which is what Craft uses to save elements under the hood).</p>
  <p>In PHP, the same thing would look like this:</p>
  <pre class="lang-php prettyprint-override"><code>$user-&gt;setFieldValue('fieldHandle', 123456);
  Craft::$app-&gt;getElements()-&gt;saveElement($user);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-03-04T00:50:56.433Z
Answers.Text: Thank you so much!
Answers.UserDisplayName: Keatyn Garton
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-23T14:03:58.980Z
Answers.Text: Sorry to hijack - is it possible to create a user in this way?
Answers.UserDisplayName: iamkeir
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-23T20:43:16.363Z
Answers.Text: @iamkeir Technically yes, I think so. Probably not a good idea, in any case it’s going to be difficult to really answer in a comment so you should probably create a new question for it :)
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-25T10:06:04.117Z
Answers.Text: @MatsMikkelRummelhoff ah the old "just because you can, doesn't mean you should" :) yeah I'll post a new question, thanks.
Answers.UserDisplayName: iamkeir
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-25T11:16:39.337Z
Answers.Text: For anyone interested, specific query about creating a user: https://craftcms.stackexchange.com/questions/39591/create-user-in-twig-without-form
Answers.UserDisplayName: iamkeir
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-03-03T23:42:21.577Z
Answers.LastActivityDate: 2022-03-04T00:20:08.130Z
Answers.LastEditDate: 2022-03-04T00:20:08.130Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>I basically just want to set the value of a users field to a different number if something happens, so that the user field is actually updated in the CMS. Can I do that without creating a form? So like if I had a custom user field I wanted to update in twig and actually have it change the data in the CMS. I know you can do this with a user/save-user form, but I don't really want a form for this because I don't want it to actually be changed by the user.</p>
  
Tags: <templating><users><form><custom-fieldtype><custom-field>
Title: Can I change a user field with twig without using a form?
ThreadText: # Can I change a user field with twig without using a form?
  
  (Asked by: username_0 on 2022-03-03)
  
  I basically just want to set the value of a users field to a different number if something happens, so that the user field is actually updated in the CMS. Can I do that without creating a form? So like if I had a custom user field I wanted to update in twig and actually have it change the data in the CMS. I know you can do this with a user/save-user form, but I don't really want a form for this because I don't want it to actually be changed by the user.
  
  **Tags:** templating, users, form, custom-fieldtype, custom-field
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-03-03. Score: 3 | Accepted answer
  
  Yes, it's possible via the special Twig [`do`](https://twig.symfony.com/doc/2.x/tags/do.html) tag, which basically enables you to call methods without returning (i.e. outputting) anything (which is what happens with nearly every other Twig expression).
  
  Assuming the "users field" is a custom field attached to the users' field layout (and you've got a `user` variable in your Twig template referring to a [User](https://docs.craftcms.com/api/v3/craft-elements-user.html#public-properties) element), something like the below should work:
  
  ```
  {% do user.setFieldValue('fieldHandle', 123456) %}
  {% do craft.app.elements.saveElement(user) %}
  
  ```
  
  What the above does, is first setting the custom field value for a field with the handle `fieldHandle` to a value `123456` on the `user` element, via the [`setFieldValue()`](https://docs.craftcms.com/api/v3/craft-base-element.html#public-methods) method (which is available for all Craft element types). Then, the user is (re)saved using the [`Elements::saveElement()`](https://docs.craftcms.com/api/v3/craft-services-elements.html#public-methods) method (which is what Craft uses to save elements under the hood).
  
  In PHP, the same thing would look like this:
  
  ```php
  $user->setFieldValue('fieldHandle', 123456);
  Craft::$app->getElements()->saveElement($user);
  
  ```
  
  > **Comments:**
  > **username_0**: Thank you so much!  
  > **username_2**: Sorry to hijack - is it possible to create a user in this way?  
  > **username_1**: @username_2 Technically yes, I think so. Probably not a good idea, in any case it’s going to be difficult to really answer in a comment so you should probably create a new question for it :)  
  > **username_2**: @username_1 ah the old "just because you can, doesn't mean you should" :) yeah I'll post a new question, thanks.  
  > **username_2**: For anyone interested, specific query about creating a user: https://craftcms.stackexchange.com/questions/39591/create-user-in-twig-without-form  
  
  ---
  
  