0

So, I'm trying to access the Address field on the User object in an after update Trigger but I keep getting the error when I try to update a User record.

Invalid field Address for SObject User

I am trying to access the dynamically like so.

... u.get(fieldApiName);

where u is a User object and fieldApiName has value Address

My handler class has API Version 37. My trigger was initially API Version 29, but I updated it to API Version 37. I was hoping that updating the Trigger would resolve the issue but no luck.

I also tried to do it non-dynamically but that doesn't work either.

u.Address

Oddly when I try it this way and update the User record I get failed recompilation error.

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger UserTriggers caused an unexpected exception, contact your administrator: UserTriggers: execution of AfterUpdate caused by: line 4, column 2: trigger body is invalid and failed recompilation: Dependent class is invalid and needs recompilation: UserHandler: line 92, column 82: Invalid field Address for SObject User

It let me save the code, and when I go to Setup > Develop > Apex Class and click on the compile all classes link I get no errors. But when I try to update a User record I still get the compilation error.

I would try querying the field via SOQL but I need to get both the new and old values, so SOQL won't help me.

6
  • You must have a hard-coded reference for it to throw an invalid trigger body error, no?
    – Adrian Larson
    Oct 19, 2016 at 15:05
  • @AdrianLarson Correct. Only when I hard code it does it give the invalid trigger body error. When I do it dynamically I only get the Invalid field Address for SObject User error
    – dBeltowski
    Oct 19, 2016 at 15:06
  • Did you check that your running user has read permission on that field?
    – Adrian Larson
    Oct 19, 2016 at 15:09
  • @AdrianLarson So your question lead me to the answer. I do have read/edit permissions on the field (I am Sys Admin, and runnig user). My handler class had with sharing, once I changed it to without sharing it worked.
    – dBeltowski
    Oct 19, 2016 at 15:21
  • Not an ideal fix. You should try moving the logic to an inner class at least to minimize the amount of code you run without sharing if you can't find a better workaround.
    – Adrian Larson
    Oct 19, 2016 at 15:22

1 Answer 1

2

The compound Address fields have always been really flaky for me.

I tried to use it in an Apex class that I was developing around a year ago, (ended up in me discovering a bug in the JSON encoder) and it would constantly give me errors about the field not existing, and dependent classes needing recompilation.

As far as I can tell, to actually be able to use one of the compound fields, everything accessed directly and indirectly from a piece of code that uses one of these compound fields needs to have an API version of 30 or greater (which is when these compound field types were introduced).

It's a chore to figure out exactly what ends up being executed, and updating the API version in a production environment requires a deployment.

I'd just stick with the individual fields if at all possible. For the User sObject, these are:

  • Street
  • City
  • State
  • PostalCode
  • Country
2
  • Not exactly ideal as I did also want to capture Latitude and Longitude from the Address field. However this will be my fall back if I can't get Address to work.
    – dBeltowski
    Oct 19, 2016 at 15:30
  • 1
    Latitude and Longitude are also available as separate fields. Those are the API names, so User u = foo; u.Latitude = bar; will work.
    – Derek F
    Oct 19, 2016 at 15:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .