You need to sign in to do that
Don't have an account?

System.LimitException: Too many query rows: 50001 error
cpu limit exceeded
I have master record and n number of child record and have a the field active checkbox in master record and its refer in child record using formula field . when i change the active to inactive in master record i got below error and i have given below the code too. i am not getting this error whennumber of child is less . any suggestion pls. The child target has 2000+ records only

trigger
trigger CyclePlanProcess on Cycle_Plan__c (after delete, after insert, after undelete,
after update, before delete, before insert, before update) {
CyclePlanProcessHandler handler = new ReckittCyclePlanProcessHandler();
handler.initialize(trigger.old, trigger.new, trigger.oldMap, trigger.newMap);
if(Trigger.isInsert && Trigger.isBefore){
handler.onBeforeInsert();
} else if(Trigger.isInsert && Trigger.isAfter){
handler.onAfterInsert();
} else if(Trigger.isUpdate && Trigger.isBefore){
handler.onBeforeUpdate();
} else if(Trigger.isUpdate && Trigger.isAfter){
handler.onAfterUpdate();
} else if(Trigger.isDelete && Trigger.isBefore){
handler.onBeforeDelete();
} else if(Trigger.isDelete && Trigger.isAfter){
handler.onAfterDelete();
}
}
apex code
public with sharing virtual class CyclePlanProcessHandler {
protected Cycle_Plan__c[] newPlans;
protected Cycle_Plan__c[] oldPlans;
protected Map<ID, Cycle_Plan__c> newMap;
protected Map<ID, Cycle_Plan__c> oldMap;
protected Set<String> recordTypeNames;
private static RecordType[] rtList;
protected Map<Id, RecordType> rtMap;
public CyclePlanProcessHandler(){
}
public void initialize(Cycle_Plan__c[] oldPlans, Cycle_Plan__c[] newPlans, Map<ID, Cycle_Plan__c> oldMap, Map<ID, Cycle_Plan__c> newMap) {
this.newPlans = newPlans;
this.oldPlans = oldPlans;
this.newMap = newMap;
this.oldMap = oldMap;
}
public virtual void onBeforeInsert(){
}
public virtual void onAfterInsert(){
}
public virtual void onBeforeUpdate(){
}
public virtual void onAfterUpdate(){
}
public virtual void onBeforeDelete(){
}
public virtual void onAfterDelete(){
}
public RecordType[] getRtList() {
if (rtList == null) {
rtList = [select id, Name, DeveloperName from RecordType where SObjectType = 'Cycle_Plan__c'];
}
return rtList;
}
public Map<Id, RecordType> getRtMap() {
if (rtMap == null) {
rtMap = new Map<Id, RecordType>();
for (RecordType item : getRtList()) {
if (getRecordTypeNames().contains(item.DeveloperName)) {
rtMap.put(item.id, item);
}
}
}
return rtMap;
}
protected virtual Set<String> getRecordTypeNames() {
return new Set<String>();
}
}
-ReckittCyclePlanProcessHandler
public with sharing class ReckittCyclePlanProcessHandler extends CyclePlanProcessHandler{
public ReckittCyclePlanProcessHandler() {
}
public override void onAfterInsert() {
checkDouplicateActiveCycle();
}
public override void onAfterUpdate() {
checkDouplicateActiveCycle();
validateChildTargets();
}
private void validateChildTargets() {
Set<Id> cycleIds = new Set<Id>();
for (Cycle_Plan__c item : newPlans) {
if (!getRtMap().containsKey(item.RecordTypeId)) continue;
if (oldMap != null && oldMap.get(item.Id).Active__c == item.Active__c) continue;
cycleIds.add(item.Id);
}
Cycle_Plan_Target__c[] targets = [select id from Cycle_Plan_Target__c where Cycle_Plan__c in :cycleIds];
if (!targets.isEmpty()) {
update targets;
}
}
public void checkDouplicateActiveCycle() {
Map<Id,Id> ownerMap = new Map<Id,Id>();
for (Cycle_Plan__c item : newPlans) {
if (!getRtMap().containsKey(item.RecordTypeId)) continue;
if (!item.Active__c) continue;
if (oldMap != null
&& item.Active__c == oldMap.get(item.Id).Active__c
&& item.OwnerId == oldMap.get(item.Id).OwnerId) continue;
if (!ownerMap.containsKey(item.OwnerId)) {
ownerMap.put(item.OwnerId, item.Id);
} else {
item.addError('Found duplicate active cycle for some users.');
}
}
Cycle_Plan__c[] existPlans = [select id, OwnerId from Cycle_Plan__c where OwnerId in :ownerMap.keyset() and Active__c = true];
for (Cycle_Plan__c item : existPlans) {
if (ownerMap.get(item.OwnerId) != item.Id) {
if (newMap.containsKey(ownerMap.get(item.OwnerId))) {
newMap.get(ownerMap.get(item.OwnerId)).addError('Found duplicate active cycle for some users.');
}
}
}
}
public override Set<String> getRecordTypeNames() {
/*
Get the list of target Record Types for Reckitt
*/
if (recordTypeNames == null) {
recordTypeNames = new Set<String>();
IME_Custom_Settings__c settings = IME_Custom_Settings__c.getInstance('IME');
if (settings != null && String.isNotEmpty(settings.ReckittCyclePlanProcessHandler_RT__c)) {
recordTypeNames.addAll(settings.ReckittCyclePlanProcessHandler_RT__c.split('\\s*;\\s*'));
}
system.debug(recordTypeNames);
}
return recordTypeNames;
}
}
I have master record and n number of child record and have a the field active checkbox in master record and its refer in child record using formula field . when i change the active to inactive in master record i got below error and i have given below the code too. i am not getting this error whennumber of child is less . any suggestion pls. The child target has 2000+ records only
trigger
trigger CyclePlanProcess on Cycle_Plan__c (after delete, after insert, after undelete,
after update, before delete, before insert, before update) {
CyclePlanProcessHandler handler = new ReckittCyclePlanProcessHandler();
handler.initialize(trigger.old, trigger.new, trigger.oldMap, trigger.newMap);
if(Trigger.isInsert && Trigger.isBefore){
handler.onBeforeInsert();
} else if(Trigger.isInsert && Trigger.isAfter){
handler.onAfterInsert();
} else if(Trigger.isUpdate && Trigger.isBefore){
handler.onBeforeUpdate();
} else if(Trigger.isUpdate && Trigger.isAfter){
handler.onAfterUpdate();
} else if(Trigger.isDelete && Trigger.isBefore){
handler.onBeforeDelete();
} else if(Trigger.isDelete && Trigger.isAfter){
handler.onAfterDelete();
}
}
apex code
public with sharing virtual class CyclePlanProcessHandler {
protected Cycle_Plan__c[] newPlans;
protected Cycle_Plan__c[] oldPlans;
protected Map<ID, Cycle_Plan__c> newMap;
protected Map<ID, Cycle_Plan__c> oldMap;
protected Set<String> recordTypeNames;
private static RecordType[] rtList;
protected Map<Id, RecordType> rtMap;
public CyclePlanProcessHandler(){
}
public void initialize(Cycle_Plan__c[] oldPlans, Cycle_Plan__c[] newPlans, Map<ID, Cycle_Plan__c> oldMap, Map<ID, Cycle_Plan__c> newMap) {
this.newPlans = newPlans;
this.oldPlans = oldPlans;
this.newMap = newMap;
this.oldMap = oldMap;
}
public virtual void onBeforeInsert(){
}
public virtual void onAfterInsert(){
}
public virtual void onBeforeUpdate(){
}
public virtual void onAfterUpdate(){
}
public virtual void onBeforeDelete(){
}
public virtual void onAfterDelete(){
}
public RecordType[] getRtList() {
if (rtList == null) {
rtList = [select id, Name, DeveloperName from RecordType where SObjectType = 'Cycle_Plan__c'];
}
return rtList;
}
public Map<Id, RecordType> getRtMap() {
if (rtMap == null) {
rtMap = new Map<Id, RecordType>();
for (RecordType item : getRtList()) {
if (getRecordTypeNames().contains(item.DeveloperName)) {
rtMap.put(item.id, item);
}
}
}
return rtMap;
}
protected virtual Set<String> getRecordTypeNames() {
return new Set<String>();
}
}
-ReckittCyclePlanProcessHandler
public with sharing class ReckittCyclePlanProcessHandler extends CyclePlanProcessHandler{
public ReckittCyclePlanProcessHandler() {
}
public override void onAfterInsert() {
checkDouplicateActiveCycle();
}
public override void onAfterUpdate() {
checkDouplicateActiveCycle();
validateChildTargets();
}
private void validateChildTargets() {
Set<Id> cycleIds = new Set<Id>();
for (Cycle_Plan__c item : newPlans) {
if (!getRtMap().containsKey(item.RecordTypeId)) continue;
if (oldMap != null && oldMap.get(item.Id).Active__c == item.Active__c) continue;
cycleIds.add(item.Id);
}
Cycle_Plan_Target__c[] targets = [select id from Cycle_Plan_Target__c where Cycle_Plan__c in :cycleIds];
if (!targets.isEmpty()) {
update targets;
}
}
public void checkDouplicateActiveCycle() {
Map<Id,Id> ownerMap = new Map<Id,Id>();
for (Cycle_Plan__c item : newPlans) {
if (!getRtMap().containsKey(item.RecordTypeId)) continue;
if (!item.Active__c) continue;
if (oldMap != null
&& item.Active__c == oldMap.get(item.Id).Active__c
&& item.OwnerId == oldMap.get(item.Id).OwnerId) continue;
if (!ownerMap.containsKey(item.OwnerId)) {
ownerMap.put(item.OwnerId, item.Id);
} else {
item.addError('Found duplicate active cycle for some users.');
}
}
Cycle_Plan__c[] existPlans = [select id, OwnerId from Cycle_Plan__c where OwnerId in :ownerMap.keyset() and Active__c = true];
for (Cycle_Plan__c item : existPlans) {
if (ownerMap.get(item.OwnerId) != item.Id) {
if (newMap.containsKey(ownerMap.get(item.OwnerId))) {
newMap.get(ownerMap.get(item.OwnerId)).addError('Found duplicate active cycle for some users.');
}
}
}
}
public override Set<String> getRecordTypeNames() {
/*
Get the list of target Record Types for Reckitt
*/
if (recordTypeNames == null) {
recordTypeNames = new Set<String>();
IME_Custom_Settings__c settings = IME_Custom_Settings__c.getInstance('IME');
if (settings != null && String.isNotEmpty(settings.ReckittCyclePlanProcessHandler_RT__c)) {
recordTypeNames.addAll(settings.ReckittCyclePlanProcessHandler_RT__c.split('\\s*;\\s*'));
}
system.debug(recordTypeNames);
}
return recordTypeNames;
}
}

What is supposed to happen to the child records when you change the master record from active to inactive?