Using the Tooling API to create trigger, getting Error No such column ‘Email’ on entity ‘User’

I’m using the Tooling API to dynamically create an Apex Trigger. The trigger itself consists of one line, calling a method in a Util class.

Creating this trigger by hand works fine.

When I try to create the trigger using the Tooling API, Salesforce tries to recompile the Util class (not sure why it needs to do this). During the recompile of the Util class, it stumbles across this line in the Util class:

    List<User> admins = [Select Email from User where ProfileID = :profiles[0].id];

The entire response from the Tooling API is:

[{“message”:”Dependent class is invalid and needs recompilation:\nutil: line 614, column 23: No such column ‘Email’ on entity ‘User’. If you are attempting to use a custom field, be sure to append the ‘__c’ after the custom field name. Please reference your WSDL or the describe call for the appropriate names.”,”errorCode”:”INVALID_FIELD_FOR_INSERT_UPDATE”,”fields”:[]}

I’ve verified that this is indeed the problematic line. If I comment out the entire method that has the “Select Email from User …” in in (and some other similar problems), the Metadata API call succeeds, and the trigger is created.

I’m really baffled why Salesforce would think that Email is not a field on User and would be glad for any suggestions. There are other parts to this (Salesforce also claims that ParentID is not a field on the FieldPermissions object, etc), so unfortunately just doing a database.query instead (which seems to work) isn’t an option.

I’m thinking it has to do with the fact that the code to create the trigger is called asynchronously from another trigger, with the SessionId passed through. OTOH, the trigger is created fine with the offending code commented out, so API access as well as Author Apex permissions clearly come through.

Answer

Right now the Tooling API doesn’t support some things. Please use Metadata API.

Attribution
Source : Link , Question Author : Jochen , Answer Author : Nisar Ahmad

Leave a Comment