4

We have a multi language org and the system administrators also use different languages when administrating the Salesforce org.

This creates a problem when running unit tests. Some unit tests want to test functionality as a user with a specific profile, e.g. the standard Salesforce 'System Administrator' profile. So we have to create a user inside the test, with that profile. This is a line from the unit test:

Profile p = [SELECT Id FROM Profile WHERE (Name = 'System Administrator') OR (Name = 'Systeembeheerder')][0];

This way, unit tests can be run by admins who use one of two languages. How can we make this code language independent? I haven't found something like a DeveloperName attribute for Profile.

A solution would be to clone the standard profile to a custom profile, because the names of custom profiles are fixed and thus language independent, but I would like to know if someone has a better idea.

0

1 Answer 1

7

Yes, there is a FullName attribute that is different than what you see in the UI or when you query against the Standard profiles.

SELECT Name FROM Profile

Will return System Administrator (or its translated value) even though Admin is the FullName. You can see a mapping from the below question.

Listing of All Standard Profiles and their Metadata API Names

Unfortunately, there's no way to query on that field for standard profiles in SOQL.

You can see that information, but the ways you can achieve that likely won't help your issue:

  1. Tooling API shows both Name and FullName so you can query it accurately
  2. Metadata API provides the FullName as well.

In your use case, I think you have a couple considerations:

  1. Have specific methods in your TestFactory class that creates/queries AdminUser, SalesUser, etc. Only need to update it in one place. Within that, utilize a custom metadata type to map whatever label anyone wants to use (ex. Admin) to get the right profile Id. You can maintain and update this as necessary but it shouldn't be a frequent occurrence.
  2. You could utilize the PermissionsPermissionName field on Profile to get the admin (assuming admin is the only one with modify all). You could utilize a combo of permissions to get Admin (you can run a describeSObject against the Profile object to see all of them).

SELECT Id FROM Profile WHERE PermissionsModifyAllData= TRUE

or forgo creating a new user and query one:

SELECT Id FROM User WHERE Profile.PermissionsModifyAllData = TRUE

The above might not help with more granular profiles (run as Sales profile), but I presume System Admin is your use case.

You must log in to answer this question.

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