How to use UserUtilities.SetCustomStringFieldValue()?

I am writing som custom data to a customer, using this code:

    //Make writeable
    person = GetCurrentPerson().MakeWritableClone();

    //Update the user
    person.Fields["ExtraData"] = "Test";
    UserUtilities.UpdatePerson(person, CurrentState.Current.Token);

This works fine, but I see the function in the accelerator


This seems cleaner but I how it is supposed to work? Am guessing that am missing some basic c# thing, but the function doesn’t seem to do anything? And is a static void.

What is the preferred way to update customerData?

Litium version: 6.2

In pre Litium 6 you was need to set the field by a Guid as identifier and then the helper method was created to convert the name of the field to the Guid before the value was set.

I should have write the code as that will fetch the person, modify the field and then persist (update) the person.

var personService = IoC.Resolve<PersonService>();

var person = personService.Get(userId)?.MakeWritableClone();
person.Fields["ExtraData"] = "Test";

Due to the security in litium the call to the method Update will fail if not the current user have write access to that entity. To go around that you need to act as system instead of the user.

var personService = IoC.Resolve<PersonService>();
var securityContextService = IoC.Resolve<SecurityContextService>();

var person = personService.Get(userId)?.MakeWritableClone();
person.Fields["ExtraData"] = "Test";

using (securityContextService.ActAsSystem())

The method will wrap this act as system-thing to make it easier to remember that part of the code when it is used from multiple places. Just this method is using the legacy code for the SecurityContextService.ActAsSystem().

The usage of `IoC.Resolve´ should be avoided and the service should be injected in the classes ctor instead to make it more testable.