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
MimicaMimica 

Issue with Maps that uses String keys with accentuation?

Hi,

 

After the go live of Spring 12 a lot of tests code failed in our Org and the error was always the same: to create an user, we

used the profile name 'Usuário Padrão' that failed to be retrieved from a Map.

 

I developed a small test class (testMap) that shows the error, testMethod01, and another that shows a workaround, testMethod02.

 

Could anyone confirm that it's a bug or if I'm doing something wrong?

 

Thanks in advance!

 

@isTest 
public class testMap {
@isTest 
public static void myTestMethod1() {
  Map<String,ID> profNameId= new Map<String,ID>();
  for (Profile prof : [select Id, Name from Profile where Name in ('Usuário padrão','Administrador Funcional')]) 
  { 
    profNameId.put(prof.Name , prof.Id); 
  }
  System.assert(profNameId.size() == 2, 'The map should have 2 elements'); 
  System.assert(profNameId.get('Administrador Funcional') <> null, 'The map for Administrador Funcional should not be null');
  System.assert(profNameId.get('Usuário padrão') <> null, 'The map for Usuário padrão should not be null'); 
}
@isTest 
public static void myTestMethod2() {
  Map<String,ID> profNameId= new Map<String,ID>();
  for (Profile prof : [select Id, Name from Profile where Name in ('Usuário padrão','Administrador Funcional')]) 
  { 
    if (prof.Name == 'Usuário padrão') { 
      profNameId.put('Usuario padrao', prof.Id); 
    } else { 
      profNameId.put(prof.Name , prof.Id); 
    } 
  }
  System.assert(profNameId.size() == 2, 'The map should have 2 elements'); 
  System.assert(profNameId.get('Administrador Funcional') <> null, 'The map for Administrador Funcional  should not be null');
  System.assert(profNameId.get('Usuario padrao') <> null, 'The map for Usuário padrão should not be null'); 
}
}

 

KRKKRK

 

Use the below code
Map<Id, Profile> profNameId= new Map<ID, Profile>();
Instead of the 'Map<String,ID> profNameId= new Map<String,ID>();'
MimicaMimica

Hi Ramakrishna,

 

Thank you for your response, but I would like after querying the Profile object, create some test users, like this one below.

 

User u = new User(FirstName = 'User',
		  LastName = 'Test #1',
		  Alias = 'tstusr1',
		  Email = 'tstusr1@testorg.com',
		  UserName = 'tstusr1@testorg.com',
		  ProfileId = profNameId.get('Usuario padrao'),
		  Country = 'BR',
		  EmailEncodingKey='UTF-8',
		  LanguageLocaleKey='pt_BR',
		  LocaleSidKey='pt_BR',
		  TimeZoneSidKey='America/Sao_Paulo');

 

Note that I used  profNameId.get('Usuario padrao')  to retrieve from the Map the Id for that Profile name given his name.

 

If I understood correctly your suggestion I will not be able to retrive the Profile Id.

 

Best Regards,

Mimica

KRKKRK
Try this
Map<String,Profile> profNameId= new Map<String,Profile>(); for (Profile prof : [select Id, Name from Profile where Name in ('Usuário padrão','Administrador Funcional')]) { profNameId.put(prof.Name , prof); }