You need to sign in to do that
Don't have an account?
Pathak 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 -
and Class Method -
However not doing anything
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
Kindly update your code according to the below changes i have made in the existing code ;
let me know if this will work you !!
Thanks !
Use these : - AssignCustomProfile.assignProfileToUser(usersId); in your trigger instead AssignCustomProfile.assignProfileToUser(userId);
Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com
Apologize for typo in the code.
please get the updated code below.
mark it best answer if this helped you out.
Thanks!
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;
}
}
Thanks !