I am running the following batch and I am making a query inside of a for loop, here is the query:
lgm=[select Group.Name, group.type,group.id, group.ownerID from GroupMember where UserOrGroupId =: u.id];
The batch loops through every person in the org, obtaining their permission sets, as well as the public groups and queues to which they are allocated, as well as their name and id, and populating those details into a custom object named ConsolidatedUser.
I haven't yet tested the batch with a big number of records to see if the governor restrictions are reached, and I'd want your input. As it stands, the batch works ok. Can you tell me how many soql queries are allowed per transaction in a batch so that I don't run into any conflicts? Here is my code, and I appreciate your assistance.
global class TDTRMIS_GetUserDetails implements Database.Batchable<sObject>, Database.Stateful {
global string UserPermissionSets='';
global string UserGroups='';
global string UserQueues='';
global integer i;
global Database.QueryLocator start(Database.BatchableContext bc) {
return Database.getQueryLocator(
'SELECT Id, name, (select PermissionSet.Name, AssigneeId FROM PermissionSetAssignments) from user'
);
}
global void execute(Database.BatchableContext bc, List<User> scope){
// process each batch of records
// process each batch of records
List<ConsolidatedUser__c> lcu = new List<ConsolidatedUser__c>();
list<GroupMember> lgm= new list<GroupMember>();
for (User u : scope)
{
ConsolidatedUser__c cu= new ConsolidatedUser__c();
lgm=[select Group.Name, group.type,group.id, group.ownerID from GroupMember where UserOrGroupId =: u.id];
for(PermissionSetAssignment ps : u.PermissionSetAssignments)
{
UserPermissionSets=UserPermissionSets+ps.PermissionSet.name+'|';
}
for(GroupMember gm : lgm)
{
if(gm.group.type=='Regular' )
{
UserGroups=UserGroups+gm.group.Name+'|';
}
else if(gm.group.type=='Queue' )
{
UserQueues=UserQueues+gm.group.Name+'|';
}
}
cu.PermSet__c=UserPermissionSets ;
cu.PublicGroupList__c=UserGroups;
cu.QueueGroupList__c= UserQueues;
cu.User_Lookup__c=u.id;
cu.name=u.name;
lcu.add(cu);
}
try{
upsert lcu;
}
catch(exception e)
{
system.debug(e);
}
}
global void finish(Database.BatchableContext bc){
//to be added later
}
}