+ Start a Discussion

Is it possible to modify any fields within the User table using Apex?

Hi all,
I've searched for other threads with similar issues, but I couldn't find one with an exact answer. I wrote some Apex code that attempts to update a field within a User record. When I tested my class, just by looking at my debug statements, it seems that the field is getting updated. However, when the trigger is actually fired, I'm getting the following exception:
System.DmlException: Update failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, You cannot update a customizable setup entity, except when running tests

So it seems on this particular table, the field can only be updated on a test class but not on my actual trigger.
I read the Apex Doc, and there's a section that mentions that not all SObjects can be updated using Apex but it doesn't specify which SObjects can or cannot be updated.
If anyone can confirm this, I'd appreciate it.
Thanks in advance. 

it may be a limitation of the fields themselves.  can you tell us what the fields are you're trying to force the update on?  also, what profile is the user your running the trigger as under?
I first tried to update a couple of the native User fields such as IsActive and Title, but I that didn't work. Then I tried to update a custom field that I had created and got the same results.
Currently, I'm trying to fire this trigger myself on sandbox and I'm an SAdmin. 
Originally, I wanted to see if it was possible to update a User record by using the Inbound/Outbound Email Apex capability, but I that didn't work. Again, I had debug statements printing the values of the fields before and after the update and it seems they were updating when my test class excecuted. When I sent an email to my Email Service, an exception was thrown right after the field update (which is the same error message I posted on my original post) and I added it to the body of the email. Since updating the user field didn't work from Email Services, I was curious if it'd work from a normal trigger. Unfortunatelly, it's not quite going well :-)  
hmmm, well, email services and triggers both sit on top of an apex class as the base, so they'd have the same limitations if there are any.

i haven't had a use case come up with the need to modify the user object yet, so i'm unsure if there is a hard limitation on accessing that object in anything but select statements.

Here's where it gets cool though.  in the Force.com IDE, and also the sForce Explorer, you can drill into the schema to verify what you're trying to update can in fact be touched.  When you drill down to User > Fields > > [field name] > attributes, you should see an update-able flag.  It does say that Title and IsActive are updatable, so there's gotta be something else awry here.