**i have assign permission set dynamically to newly created user ** condition to assign Permission set is:-one has to query the country field from Organization Object and and based on that country field it has to assign permission set. Note:-in our Org all the Permission sets begin with Country name. For example :- User "A" has country as France in Organization object then it should assign "France Community Permission set". Please guide me as to how can i make this work.

public class xR35_CommunitiesSelfRegController
    //public String firstName {get; set;}
    //public String lastName {get; set;}
    public String email {get; set;}
    public Account acc {get;set;}

    public String password {get; set {password = value == null ? value : value.trim(); } }
    public String confirmPassword {get; set { confirmPassword = value == null ? value : value.trim(); } }
    public String communityNickname {get; set { communityNickname = value == null ? value : value.trim(); } }

    public xR35_CommunitiesSelfRegController() 
        String accId=ApexPages.CurrentPage().getParameters().get('accountId');
        acc= [select firstname,lastname from account where id=:accId];


    private boolean isValidPassword() 
        return password == confirmPassword;

    public PageReference registerUser() 
        if (!isValidPassword()) 
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, Label.site.passwords_dont_match);
            return null;

        String profileId = null; // To be filled in by customer.
        String roleEnum = null; // To be filled in by customer.
        String accountId = ''; // To be filled in by customer.

        String userName = email;

        User u = new User();
        u.Username = userName;
        u.Email = email;
        u.FirstName = acc.firstName;
        u.LastName = acc.lastName;
        u.ProfileId = profileId;
        u.CommunityNickname = email.substringBefore('@');

        System.Debug('value in the nickname--->'+u.CommunityNickname);

        String userId;

        try {
                userId = Site.createExternalUser(u, acc.id, password);
            catch(Site.ExternalUserCreateException ex) 
                List<String> errors = ex.getDisplayMessages();
                for (String error : errors)  
                    ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, error));

                // This message is used for debugging. Do not display this in the UI to the end user.
                // It has the information around why the user creation failed.

        if (userId != null) 
            if (password != null && password.length() > 1) 
                return Site.login(userName, password, ApexPages.currentPage().getParameters().get('startURL'));

                PageReference page = System.Page.CommunitiesSelfRegConfirm;
                return page;
        return null;


1 Answer 1


Can you try following:

//Get Country from Organisation
Organization org = [select Country from organization];

//get permission set
String psName = org.Country + '%';
//Get permission set that matches country name
List<PermissionSet> psList = [select id, name, profileid from permissionset where name like : psName];

//assign permission set to user
PermissionSetAssignment psa = new PermissionSetAssignment(AssigneeId = userId, PermissionSetId = psList[0].Id);
insert psa;
  • Thank you so much for your quick reply,, we have change in requirement.
    – Sayera
    Sep 1, 2015 at 6:57
  • Does my suggestion help to resolve your problem, if yes it will be worth to accept the answer so that it can help others with similar problem.
    – Amit Jain
    Sep 1, 2015 at 7:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .