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
Andrew PosadzyAndrew Posadzy 

Assign Profile for SF Community when badge is created or updated for contact

I am trying to create a trigger that assigns a profile to a customer that will give them access to a SF community. 

Trigger
trigger Assign_Customer_Community_User on Contact (after insert, after Update) {
	set<user> usersId = new set<user>();
    set<id> contactIdsSet = new set<id>();
    
    for (contact c : trigger.new) {
        
        List<OrderAPI__Badge__c> contactBadge = [SELECT id, OrderAPI__Badge_Type__c, OrderAPI__Contact__c FROM OrderAPI__Badge__C WHERE OrderAPI__Contact__c = :c.id];
        
        for (OrderAPI__Badge__c b : contactBadge) {
            if (b.OrderAPI__Badge_Type__c == 'a0k1g000000n4vLAAQ') {
                contactIdsSet.add(c.id);
            } else {
                //do nothing
            }
        }  
    }
    
    For(User usr : [SELECT Id FROM User WHERE ContactID!=null AND ContactId IN : contactIdsSet]) {
        usersId.add(usr);
    }
    
    Assign_Customer_Community_User.AssignProfileToUser(usersId);
    
    
    
    
}

Class
public class Assign_Customer_Community_User {

    public static void assignProfileToUser(set<User> userIds) {
        list<User> updatedUserlst = new list<User>();
        profile p = [SELECT ID FROM profile WHERE name = 'Customer Community User'];
        if (userIds.size() > 0 && userIds !=null) {
            for (user u : userIds) {
                u.profileId = p.id;
                updatedUserlst.add(u);
            } 
        }
        updatedUserlst.addAll(userIds);
        
        update updatedUserlst;
    } 
}

 
pankul guptapankul gupta
Is there any error you are getting in the logs on running the same.
Andrew PosadzyAndrew Posadzy
So what I am trying to do is restrict access to a community for all customer community login users using either another profile or a permission set. Then I want to assign a different profile or permission set that gives those users access to the community when a specific badge is added to their contact.