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
Pathak 1Pathak 1 

Apex trigger to Change user Profile

We have Communities Set Up and thus Community user.

The requirement is , If A Checkbox on Contact is Checked then , It should Update the Profile of that COmmunity User.

I am using below trigger - 
 
trigger AssignPermissionSet on Contact (after Update){
Set<ID> usersId = new Set<Id>();
Set<Id> contactIdsSet = new Set<Id>();
for (contact c :trigger.new) {
    if(c.Is_SUper_User__c == true) {
       contactIdsSet.add(c.Id);
    }
}
For(User usr : [Select Id From User Where ContactId!=null AND ContactId IN : contactIdsSet]){
	usersId.add(usr.Id);
}
  AssignCustomProfile.assignProfileToUser();
}

and Class Method -
 
public class AssignCustomProfile {

    public static void assignProfileToUser () {
        
        profile p = [SELECT ID FROM profile WHERE name='Custom12'];
        user u = new user(profileID = p.Id);
        update u;
    }
}

However not doing anything
Lokesh KumarLokesh Kumar
HI,

Kindly update your code according to the below changes i have made in the existing code ;
 
trigger AssignPermissionSet on Contact (after Update){
Set<ID> usersId = new Set<Id>();
Set<Id> contactIdsSet = new Set<Id>();
for (contact c :trigger.new) {
    if(c.Is_SUper_User__c == true) {
       contactIdsSet.add(c.Id);
    }
}
For(User usr : [Select Id From User Where ContactId!=null AND ContactId IN : contactIdsSet]){
	usersId.add(usr.Id);
}
  AssignCustomProfile.assignProfileToUser(userId);
}
 
public class AssignCustomProfile {

    public static void assignProfileToUser (List<User> userIds) {
        List<User> updatedUserlst = new List<User>();
        profile p = [SELECT ID FROM profile WHERE name='Custom12'];
		if(userIds.size() > 0 && userIds != null){
			for(User u : userIds)
			{
				u.profileID = p.id;
				updatedUserlst.add(u);
			}
		}
		update updatedUserlst ;
    }
}

let me know if this will work you !!
Thanks ! 
Pathak 1Pathak 1
@lokesh kumar - ERROR - userId Variable does not exists
rajat Maheshwari 6rajat Maheshwari 6
Hi Rutvij,

Use these : -  AssignCustomProfile.assignProfileToUser(usersId); in your trigger instead AssignCustomProfile.assignProfileToUser(userId);

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com
Lokesh KumarLokesh Kumar
Hi Pathak,

Apologize for typo in the code.

please get the updated code below.
trigger AssignPermissionSet on Contact (after Update){
Set<ID> usersId = new Set<Id>();
Set<Id> contactIdsSet = new Set<Id>();
for (contact c :trigger.new) {
    if(c.Is_SUper_User__c == true) {
       contactIdsSet.add(c.Id);
    }
}
For(User usr : [Select Id From User Where ContactId!=null AND ContactId IN : contactIdsSet]){
	usersId.add(usr.Id);
}
  AssignCustomProfile.assignProfileToUser(usersId);
}

mark it best answer if this helped you out.
Thanks!

 
Pathak 1Pathak 1
Hi @lokesh Kumar, With your input, I have created it as below  This is working fine as of now but it needs an addition.

If Contact Checkbox is checked and if Community user is not already created then it should first auto Create a User Record with Profile - Administrator. Is this possible? can 

Trigger - 

trigger updateCustomerUserProfileTrigger on contact (after insert, after update) {
    
    Set <Id> ContactIdsSet = new Set <Id> ();
    
     for (contact c : trigger.new) {
         ContactIdsSet.add(c.id);
     }
    
    updateCustomerUserProfile.assignProfileToUser(ContactIdsSet);

}


Class - 

public class updateCustomerUserProfile {

 public static ID profile1;
 public static ID profile2;
  
  @future
    
    public static void assignProfileToUser(set<ID> ContactIDListNew){
       
       List <Contact> contactListNew = new List<contact>();
       contactListNew = [SELECT ID , Customer_Admin__c FROM Contact WHERE ID IN :ContactIDListNew];
       List<user> usertoUpdate = new List <user>();
       for (profile p: [SELECT ID ,Name FROM Profile]){
           if(p.name == 'Administrator'){
               profile1 = p.id;
           } else if(p.name == 'Portal User'){
               profile2 = p.id;
            }
       }
       List <user> userList = [SELECT ID , Name,ContactID FROM user WHERE ContactID IN : contactListNew];
       Map<ID,user> userMap = new Map<ID,user>();
       for (user u :userList) {
           userMap.put(u.contactID, u);
       }
       for (contact c : contactListNew) {
           if (c.Customer_Admin__c == true) {
               user u = userMap.get(c.id);
               u.profileID = profile1;
               usertoUpdate.add(u);
           } else if (c.Customer_Admin__c == false) {
               User u = userMap.get(c.id);
               u.profileID = profile2;
               usertoUpdate.add(u);
           }
           
       } 
       update usertoUpdate;
   }
 }
Lokesh KumarLokesh Kumar
Yes, you can so what is the challenge now doing this please share on this thread,

Thanks !