+ Start a Discussion
Fabio ScheurelFabio Scheurel 

How to retrieve a profile in an APEX unittest in a non-localized way?

Every unit test example for APEX code retrieves the Profile using the following SOQL:
SELECT Id FROM Profile WHERE Name = 'System Administrator'
When I am deploying my unit tests to another Org, which has set another language (e.g. German), this SOQL won't work. The Profile.Name is always returned localized, in German it says "Systemadministrator".

How can I retrieve a profile from the database without being dependant of the user's language? Is there a default System.Label for standard profile names?

Deepak Kumar ShyoranDeepak Kumar Shyoran
Create a custom Label with name “Profile” and put the value "System Administrator" and then add some translation as in your case for German by enable Translation Settings and add some supportive language as in your case English and German for your org. By doing this you'll be able to achieve your task.

You can call custom label in apex as  Label.CustomLabelName ;
Chinmay BhusariChinmay Bhusari
Hi Fabio,

Use toLabel()
SELECT Company, toLabel(Recordtype.Name) FROM Lead
This query returns lead records with the record type name translated into the language for the user who issued the query.

You can use toLabel() to filter records using a translated picklist value. For example:

SELECT Company, toLabel(Status)
FROM Lead
WHERE toLabel(Status) = 'le Draft'


Lead records are returned where the picklist value for Status is 'le Draft.' The comparison is made against the value for the user’s language. If no translation is available for the user’s language for the specified picklist, the comparison is made against the master values.

Regards
AshlekhAshlekh
Hi,

You can Create System label in the org. 

1) Steps are here Click on your name at righ top corner--> Click On setup--> at Right hand side there is a section App Setup --> Create --> Custom label.
2) Create a new label and access that custom label in you code.
3) Custom label are converted in org language.

SELECT Id FROM Profile WHERE Name =: System.label.Label_name


If this post helps you than mark it as solution, Enjoy APEX
Fabio ScheurelFabio Scheurel
Thanks, but TOLABEL does only work for Picklist values and record type names. Additionally I need the other way round, a non-translated value for a profile name.
Fabio ScheurelFabio Scheurel
Hm, is there really no "default way" of how to do this? A label with translation just for my unit tests? Regards Fabio
Chinmay BhusariChinmay Bhusari
Hi Fabio,
You can always use Like 'System%'
nitesh gadkarinitesh gadkari
You Have custom Label Issue.Please refer to the related article in help

https://help.salesforce.com/HTViewHelpDoc?id=cl_about.htm&language=en_US

May be this helps you.
AshwaniAshwani
Use following query to get the profile Id of System Administrator:

Profile p = [SELECT Id, Name, Description
FROM Profile
WHERE UserType = 'Standard' AND PermissionsAuthorApex = true LIMIT 1];

This condition is true for System admins only.