function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
car ramrod.ax1126car ramrod.ax1126 

SOQL queries fail when user changes their language - multi-language issues

I've brought this up with Premier support and keep getting told that I need to modify my code and the following issue I am having is working as intended.

 

I have this query in a User Trigger: Profile p1 = [SELECT ID from Profile WHERE Name = 'System Administrator'];

 

When the trigger fires with a user that has "English" as their language on their user record, the query returns one row, the Standard System Administrator profile.

 

When the trigger fires with a user that has "Spanish" as their language on their user record, a "System.QueryException: List has no rows for assignment to SObject" exception is thrown. That's because the API has translated "System Administrator" into "'administrador del sistema"

 

Up until this point, Salesforce is suggesting I add a "or Name =''administrador del sistema' " to my where clause or that I specifically check what locale the running user is in and execute a different query specifically for them, but neither of these seem like a reasonable solution. What if I have users that use 10 different languages? Do I really need to handle all these additional or clauses in my SOQL statements? What happens when even more additional languages are available natively?

 

I bring this up because it's not really a big deal to change the one line of code, but it is a big deal to go through all of our custom code, duplicate/modify it for each and every language, and then update all the test classes to check it. 

 

Is there a way or a function I can use to say that regardless of the user's selected langauge, always run this Trigger or Class in English?

 

Thanks in advance.

Saikishore Reddy AengareddySaikishore Reddy Aengareddy

May be you can use custom settings to mark system admin profile... I mean use hierarchy custom setting and a checkbox field to true for system admin and then use this setting in your code... I knkow this is not a fair way but trying to make it simpler...

car ramrod.ax1126car ramrod.ax1126

Sam, thanks for the reply; what you mention could solve this isolated issue, but the same problem exists for more than just the Standard System Admin Profile. This issue can crop up on any Standard field value that has been translated by the API.

ASpinASpin

Hi car,  we had the same issue and the best solution we found was this:

 

Profile admin = [SELECT Id, Name FROM Profile Where UserType = 'Standard' AND PermissionsCustomizeApplication = true];

 

The system Administrator is the only one that has customizeApplication permissions from the standard profiles.

 

Regards,

ASpin

ASpinASpin

I've tested in a develer org without any custom profile and worked correct. The problem i found was when you have any other custom profile with the same attributes. The solution is to add an order by Created Date. The code should be the following:

 

Profile admin = [SELECT Id, Name FROM Profile Where UserType = 'Standard' AND PermissionsCustomizeApplication = true ORDER BY CreatedDate ASC limit 1];

 

Regards,

ASpin

JeeedeeeJeeedeee

Aspin, thanks! 

 

Found out that the UserType has nothing todo with the type of the profile (Standard or Custom), but has to do with the fact that it is a guest, chatter, or standard profile...  So adding that to the query makes not much sense. Below code should be enough. 

 

Profile admin = [SELECT Id, Name FROM Profile Where PermissionsCustomizeApplication = true ORDER BY CreatedDate ASC limit 1];