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
GailGail 

User role name not found in test user?

I'm testing a trigger and in my test, I need to create a role and then I assign it to a user and insert the user. The test then hinges on that user having that role name. 

 

When I debug, I see that the role is created with that name and a new ID and when the user is created, the role id is the same as the new role created. However, the role name on the user is null. How do I set the role name?

 

Code snippets:

 

UserRole SomeRole = new UserRole(Name = 'Some Role');
      insert SomeRole;

 

[debug shows ID and name]

 

User SomeUser = new User(username = 'GAILTESTING@TEST.COM',
              email = 'test@example.com',
              title = 'test',
              lastname = 'test',
              alias = 'test',
              TimezoneSIDKey = 'America/Los_Angeles',
              LocaleSIDKey = 'en_US',
              EmailEncodingKey = 'UTF-8',
              ProfileId = PROFILEID, //this was set in some other code
              UserRoleId = SMERole.Id, 
              LanguageLocaleKey = 'en_US',)
    insert SomeUser;

 

[debug shows same role id from above but role name is null]

rocwilcoxrocwilcox

Are you attempting to access the role name via the relationshipname?  Its not initialized on insert (only the id is set).

but If you query that user back, and query that rolename via the relationship as well it should have a value.

 

GailGail

thanks roc - I'm actually also trying to make sure it is set as the test depends on the user's role name and it's not being set. I imagine the query will simply allow me to see it but will not actually set the role name? Any ideas on how I can make sure role name is set on the user before I run my tests?