You need to sign in to do that
Don't have an account?
Ashish2802
Update Role on Partner User using apex for bulk data upload
Hello, I have a requirement to update roles on partner user record using trigger where the user records will be created using bulk upload process.
I am stuck with the Role id assignment as it requires querying the UserRoleID using the Role Name based on conditions.
Below is my code snippet.
@future
public static void updatePartnerUserRoles(Set<Id> userIds){
String roleName = '';
List<User> updatedUsers = new List<User>();
for(User u : [Select Id, Contact.Agent_Type__c, Account.Name From User Where Id IN : userIds]){
if(u.Contact.Agent_Type__c == 'Admin'){
roleName = u.Account.Name + ' Partner Manager';
u.UserRoleId = [Select Id From UserRole Where Name = :roleName LIMIT 1].Id;
updatedUsers.add(u);
}
else if(u.Contact.Agent_Type__c == 'Non-Admin'){
roleName = u.Account.Name + ' Partner User';
u.UserRoleId = [Select Id From UserRole Where Name = :roleName LIMIT 1].Id;
updatedUsers.add(u);
}
}
Database.update(updatedUsers);
}
The code snippet has a SOQL query inside for Loop and I am not able to figure out an alternative solution.
I am stuck with the Role id assignment as it requires querying the UserRoleID using the Role Name based on conditions.
Below is my code snippet.
@future
public static void updatePartnerUserRoles(Set<Id> userIds){
String roleName = '';
List<User> updatedUsers = new List<User>();
for(User u : [Select Id, Contact.Agent_Type__c, Account.Name From User Where Id IN : userIds]){
if(u.Contact.Agent_Type__c == 'Admin'){
roleName = u.Account.Name + ' Partner Manager';
u.UserRoleId = [Select Id From UserRole Where Name = :roleName LIMIT 1].Id;
updatedUsers.add(u);
}
else if(u.Contact.Agent_Type__c == 'Non-Admin'){
roleName = u.Account.Name + ' Partner User';
u.UserRoleId = [Select Id From UserRole Where Name = :roleName LIMIT 1].Id;
updatedUsers.add(u);
}
}
Database.update(updatedUsers);
}
The code snippet has a SOQL query inside for Loop and I am not able to figure out an alternative solution.
Pradeep Singh
Hi, you can refer below code:-