0

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;
        }
}
  • That is not how triggers work... – Adrian Larson Dec 15 '16 at 17:44
  • Can you be a more specific please ? – Zoom_v Dec 15 '16 at 23:19
  • 1
    See my updated answer. You don't add methods to a trigger, you add them to a class and call them from a trigger. – Adrian Larson Dec 15 '16 at 23:20
1

It would be simple enough to create a User trigger that sets the ProfileId value to your fallback if it is blank. Something along the lines of:

static Profile fallbackProfile
{
    get
    {
        if (fallbackProfile == null)
            fallbackProfile = [SELECT Id FROM Profile WHERE ...];
        return fallbackProfile;
    }
    private set;
}
public static void setFallbackProfile(List<User> users)
{
    for (User user : users)
        if (user.ProfileId == null)
            user.ProfileId = fallbackProfile.Id;
}

Just call the above in a before insert trigger on the User object and you should be good to go.

If you want to add a fallback UserRole as well, it would look something like:

static UserRole fallbackRole
{
    get
    {
        if (fallbackRole == null)
            fallbackRole = [SELECT Id FROM UserRole WHERE ...];
        return fallbackRole;
    }
    private set;
}
public static void setFallbackProfile(List<User> users)
{
    for (User user : users)
        if (user.UserRoleId == null)
            user.UserRoleId = fallbackRole.Id;
}

Best to keep the methods separate, since you don't want to populate the role just because the profile is blank, or vice versa.

Once you've added these methods to an Apex Class (I would call it UserService), you can reference them from your Apex Trigger.

trigger User on User (before insert)
{
    if (trigger.isBefore)
    {
        if (trigger.isInsert)
        {
            UserService.setFallbackProfile(trigger.new);
            UserService.setFallbackRole(trigger.new);
        }
    }
}

I highly recommend you look into trigger handler patterns and adopt one, the above is just to give you the idea.

  • Adrian - thank you very much. That's definitely the solution, even if it's not a non-code solution. Another part I forgot about was the Role which is also required. I attempted to add it to your code but I'm getting different errors. I put my edited version in my original post. Can you tell what I'm doing wrong with the Role ? As of now I'm getting a 'Variable does not exist : user.Role' error, but I'm guessing there's others as well. – Zoom_v Dec 14 '16 at 18:50
  • Doesn't look like you did edit your post. Anyway, the field you need to set is UserRoleId. Make sure you populate it with the Id of a UserRole, not the record itself. – Adrian Larson Dec 14 '16 at 18:52
  • 1
    @Zoom do not use hard-coded ids! That defeats the whole purpose of performing the query. Filter based on DeveloperName for UserRole, and Name for Profile. – Adrian Larson Dec 14 '16 at 18:53
  • Ok yes, that was pretty stupid to use IDs. I edited it again. Can you see the edit now ? – Zoom_v Dec 14 '16 at 19:01
  • 1
    My guess was correct, the field you need to populate is UserRoleId, not Role. Also note that when you do not use brackets after an if statement, only the next statement is "bracketed", so your role assignment takes place unconditionally as written. – Adrian Larson Dec 14 '16 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.