Can any one help me on this issue:
Error:
Debug Log: 35.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO 09:28:13.141 (141263478)|EXECUTION_STARTED 09:28:13.141 (141316469)|CODE_UNIT_STARTED|[EXTERNAL]|01pK0000000BJee|UpdateClientTeamSharing 09:28:13.158 (158830974)|SOQL_EXECUTE_BEGIN|[49]|Aggregations:0|SELECT Id, Parent.Household__c, UserOrGroupId FROM Client_Insight__Share WHERE Parent.Household__c = :tmpVar1 09:28:16.001 (3001616622)|SOQL_EXECUTE_END|[49]|Rows:50001 09:28:16.001 (3001692173)|EXCEPTION_THROWN|[49]|System.LimitException: Too many query rows: 50001 09:28:16.001 (3001919769)|FATAL_ERROR|System.LimitException: Too many query rows: 50001
Class.UpdateClientTeamSharing.execute: line 49, column 1
Here is the Code:
global class UpdateClientTeamSharing implements database.Batchable<sObject>{
global Integer intSuccessCount = 0;
global Integer intErrorCount = 0;
global integer totalUserCount = 0;
private string query;
public list<Id> lstId;
global updateClientTeamSharing(){
list<string> pIds = new list<string>();
list<ProfileIds__c> lstP = ProfileIds__c.getall().values();
for(ProfileIds__c pro:lstP){
pIds .add(pro.ProfileId__c);
}
map<id,User> userslist= new map<id,user> ([Select id, isactive, User.profile.name from User where isactive=True and user.profileId in :pIds]);
lstId = new list<Id>();
lstId.addAll(userslist.keyset());
string query = 'Select id,User__c,Household__c FROM HouseholdTeamMember__c where isActiveFlag__c = true and User__c in :lstId';
this.query = query;
}
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope){
Set<id> HHids= new Set<id>();
for(sObject s:scope){
HouseholdTeamMember__c htm = (HouseholdTeamMember__c)s;
HHids.add(htm.Household__c);
}
List<Client_Insight__Share> CIS = [SELECT Id, Parent.Household__c, UserOrGroupId FROM Client_Insight__Share
WHERE Parent.Household__c IN :HHids];
Set<HouseholdTeamMember__c> UpdateHMT= new Set<HouseholdTeamMember__c >();
for(sObject s:scope){
totalUserCount++;
boolean bfound=false;
HouseholdTeamMember__c htmNew = (HouseholdTeamMember__c)s;
for(Client_Insight__Share cs : CIS){
if(htmNew.User__c == cs.UserOrGroupId && htmNew.Household__c == cs.Parent.Household__c){
bfound=true;
}
if(!bfound){
UpdateHMT.add(htmNew);
}
}
}
if(!UpdateHMT.isEmpty()){
list<HouseholdTeamMember__c> UpdateHMTNew = new list<HouseholdTeamMember__c>();
UpdateHMTNew.addall(UpdateHMT);
Database.SaveResult[] lsr = Database.update(UpdateHMTNew, false);
for(Database.SaveResult sr: lsr)
{
if(sr.isSuccess())
{
intSuccessCount = intSuccessCount+1;
}
else
{
intErrorCount = intErrorCount+1;
}
}
system.debug('***intSuccessCount = ' + intSuccessCount);
system.debug('***intErrorCount = ' + intErrorCount);
system.debug('****totalCount ='+totalUserCount);
}
}
global void finish(Database.BatchableContext BC){
//Get results for this job
SystemLogSvc.generateResultsLog(BC.getJobId(), intSuccessCount, intErrorCount);
SystemLogSvc.generateResultsLog(BC.getJobId(), totalUserCount,intSuccessCount );
}
}