I'm setting up a process which will automatically create new Users, but need to set ProfileID to something. I can't use Process Builder because it's a required field and it would error out before it got to the Process, even if it's set to run on create.
It's an Informatica upsert function running off a list of employees. I can't hard code the profile ID into the Informatica task because some of the employess are already created and therefore already have assigned profiles.
The Profile I want to use is a custom profile. I don't think that matters, but I thought I'd mention it.
my current code (thank you @adrian) :
trigger AssignDefaultProfile on User (before insert) {
static Profile fallbackProfile
{
get
{
if (fallbackProfile == null)
fallbackProfile = [SELECT id FROM Profile p WHERE p.Name = 'Service Employee'] ;
return fallbackProfile;
}
private set;
}
public static void setFallbackProfile(List<User> users)
{
for (User user : users)
if (user.ProfileId == null)
user.ProfileId = fallbackProfile.Id;
}
static UserRole fallbackRole
{
get
{
if (fallbackRole == null)
fallbackRole = [SELECT id FROM UserRole r WHERE r.Name = 'General Employee'] ;
return fallbackRole;
}
private set;
}
public static void setFallbackRole(List<User> users)
{
for (User user : users)
if (user.UserRoleId == null)
user.UserRoleId = fallbackRole.Id;
}
}