+ Start a Discussion
Abraham kumar 4Abraham kumar 4 

CUMULATIVE_LIMIT_USAGE

im Getting  CUMULATIVE_LIMIT_USAGE at line 3 of this code
for(integer i = 0;i < eventIdsList.size(); i++){
    count = 0;
    for(Guest_New__c guest : existingGuestList ){
        if((guest.Event__c == eventIdsList[i]) && eventAndStatusValues.get(eventIdsList[i]).Contains(guest.Status__c)){
            count ++;
        }
    }
Please help to resolve this issue bit urgent.. Please let me know if more inputs required.

Below attached debug log details
16:27:12.347 (36347838249)|EXCEPTION_THROWN|[37]|System.LimitException: Apex CPU time limit exceeded
16:27:12.347 (36347939235)|FATAL_ERROR|System.LimitException: Apex CPU time limit exceeded

Trigger.TotalResponses: line 37, column 1
16:27:12.348 (36348089236)|CUMULATIVE_LIMIT_USAGE
16:27:12.348 (36348089236)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 4 out of 100
  Number of query rows: 26854 out of 50000 ******* CLOSE TO LIMIT
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 33 out of 10000
  Maximum CPU time: 15012 out of 10000 ******* CLOSE TO LIMIT
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 1 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

16:27:12.348 (36348089236)|CUMULATIVE_LIMIT_USAGE_END

16:27:12.348 (36348207142)|CODE_UNIT_FINISHED|TotalResponses on Guest_New trigger event AfterUpdate for [a0sD0000008ju0T, a0sD0000008jule, a0sD0000008jusR, a0sD0000008jvdu, a0sD0000008jwE2, a0sD0000008jxLq, a0sD0000008jyIu, a0sD0000008jyST, a0sD0000008jzkY, a0sD0000008k0Wy, a0sD0000008k14w, a0sD0000008k1sD, a0sD0000008k20q, a0sD0000008k2GF, a0sD0000008k3kC, a0sD0000008k4FZ, a0sD0000008k4Xx, a0sD0000008k4Zy, a0sD0000008k4r8, a0sD0000008k8qb, a0sD0000008k9iE, a0sD0000008kAEG, a0sD0000008kB8n, a0sD0000008kBIj, a0sD0000008kBp0, a0sD0000008kCYr, a0sD0000008kCb8, a0sD0000008kD5g, a0sD000000ApmVI, a0sD000000EstXy, a0sD000000Fr8Nf, a0sD000000GiAxg, a0sD000000LXsEf]
16:27:12.358 (36358504114)|DML_END|[20]
16:27:12.361 (36361896680)|FATAL_ERROR|System.LimitException: Apex CPU time limit exceeded

Trigger.TotalResponses: line 37, column 1
16:27:12.361 (36361931440)|FATAL_ERROR|System.LimitException: Apex CPU time limit exceeded

Trigger.TotalResponses: line 37, column 1
16:27:12.362 (36362032885)|CUMULATIVE_LIMIT_USAGE
16:27:12.362 (36362032885)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 2 out of 100
  Number of query rows: 34 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 33 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 1 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

16:27:12.362 (36362032885)|CUMULATIVE_LIMIT_USAGE_END

16:27:12.362 (36362143925)|CODE_UNIT_FINISHED|CopycontactAddressToguestAddress on Contact trigger event AfterUpdate for [0032000000CK502]
16:27:12.364 (36364104454)|EXECUTION_FINISHED



Thanks
Abraham
 
Abraham kumar 4Abraham kumar 4
I have also attached the full trigger below apart from just the lines of error in the question
trigger TotalResponses on Guest_New__c (after insert, after update, after delete) {
Integer count;
List<Event__c> ev = new List<Event__c>();
set<Id> eventIds = New set<Id>();
list<Id> eventIdsList = New List<Id>();
List<Event__c> eventToUpdate = New List<Event__c>();

map<Id,set<String>> eventAndStatusValues = new map<Id,set<String>>();
if(!Trigger.isDelete){
    for(Guest_New__c guestRecord : trigger.new){
        eventIds.add(guestRecord.Event__c);    
    }
}
else{
    for(Guest_New__c guestRecord : trigger.old){
        eventIds.add(guestRecord.Event__c);    
    }
}
eventIdsList.addAll(eventIds);

List<Advanced_Setup__c> statusValues =  [SELECT Status_values__c, Event__c FROM Advanced_Setup__c WHERE Responded__c = True AND Event__c =: eventIds];
List<Guest_New__c> existingGuestList = [Select Id,Status__c,Event__c from Guest_New__c where Event__c =: eventIds];

for(integer i = 0;i < eventIdsList.size(); i++){
    set<String> eventSpecificStatus = New set<String>();
    for(Advanced_Setup__c statusRecord: statusValues  ){
       if(eventIdsList[i] == statusRecord.Event__c){ 
           eventSpecificStatus.add(statusRecord.Status_values__c);
       }
    }
    eventAndStatusValues.put(eventIdsList[i],eventSpecificStatus);
}
system.debug('check tghe event Ids set'+eventIds );
system.debug('check tghe event Ids'+ eventIdsList);
for(integer i = 0;i < eventIdsList.size(); i++){
    count = 0;
    for(Guest_New__c guest : existingGuestList ){
        if((guest.Event__c == eventIdsList[i]) && eventAndStatusValues.get(eventIdsList[i]).Contains(guest.Status__c)){
            count ++;
        }
    }
    Event__c eventRecord = New Event__c();
        eventRecord.Id=eventIdsList[i];
        eventRecord.Total_Responses__c = count;
        eventToUpdate.add(eventRecord);
}
system.debug('Check map '+ eventAndStatusValues);
system.debug('check to updates event'+ eventToUpdate);
update eventToUpdate;
/*
List<Advanced_Setup__c> ab = [SELECT Status_values__c, Event__c FROM Advanced_Setup__c WHERE Responded__c = True AND Event__c =: eventIds];
    
    for(Guest_New__c g : trigger.new)
    {
    system.debug('entered first for loop'+ab+'with guest'+g);
        for(Advanced_Setup__c a : ab)
        {
        system.debug('entered secornd for loop'+a+'with guest'+g);
            if(g.Status__c == a.Status_values__c)
            {
            system.debug('entered if loop'+a+'with guest'+g);
               count++;
            system.debug('after count'+count);   
            }
        }
        system.debug('after inner for loop'+count);  
        system.debug('event id on guest'+g.Event__r.Id); 
        system.debug('event id on guest name'+g.Event__r.NAme);  
        Event__c event = new Event__c();
        event.Id = g.Event__r.Id;
        event.Total_Responses__c = 0;
        event.Total_Responses__c += count;
        ev.add(event);
        system.debug('after add event'+event); 
    }
    update ev;
    system.debug('updated event'+ev);
    */
}

 
viruSviruS
Hi 

1.Cumulative resource usage is logged at the end of many code units, such as triggers, executeAnonymous, batch Apexmessage processing, @future methods, Apex test methods, Apex web service methods, and Apex lead convert.

2.Cumulative profiling information is logged once at the end of the transaction and contains information about the most expensive queries (used the most resources), DML invocations, and so on.

for reference >> https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_debugging_debug_log.htm
Abraham kumar 4Abraham kumar 4
Any Ideas how i can resolve the issues from the above Code.

Many Thanksin advance
Abraham
William TranWilliam Tran
Try this:  I don't know why your count is in between the loop in the first place, I would think it would be outside, no?

count = 0;

for(Guest_New__c guest : existingGuestList ){

	for(integer i = 0;i < eventIdsList.size(); i++){

        	if((guest.Event__c == eventIdsList[i]) && eventAndStatusValues.get(eventIdsList[i]).Contains(guest.Status__c)){
            		count ++;
        
	}
    
}

 
viruSviruS
Are you using bulk data load on sfdc or .. it is happening for single record of  Guest_New__c 
SaranSaran
Hi Abraham,

Instead of line no 24 - 32 you can use the below code
 
for(Advanced_Setup__c statusRecord: statusValues){
	if(eventAndStatusValues.containsKey(statusRecord.Event__c)){
		eventAndStatusValues.get(statusRecord.Event__c).add(statusRecord.Status_values__c);
	}else{
		eventAndStatusValues.put(statusRecord.Event__c , new set<string>{statusRecord.Status_values__c})
	}
}

Which works exactly as what you have done.

Hope this might help you.

Thanks
Abraham kumar 4Abraham kumar 4
It is hapenning for a single record Viru. Not allowing the changes in the record to save throwing Apex CPU time limit exceeded
SaranSaran
Hi Abraham,

Also instead of line no 35 - 46 you can use the below code,
 
for(Guest_New__c guest : existingGuestList)
{
	if(eventAndStatusValues.containsKey(guest.Event__c)){
		Event__c eventRecord = New Event__c();
        eventRecord.Id = guest.Event__c;
        eventRecord.Total_Responses__c = eventAndStatusValues.get(guest.Event__c).size();
        eventToUpdate.add(eventRecord);
	}
}

Using a for loop inside a for loop may be reason for CPU limit exceed.

You have to avoid using many for loops.

Hope this might solve your issue

Thanks
SaranSaran
So youe entire trigger may look like this.
 
trigger TotalResponses on Guest_New__c (after insert, after update, after delete) {

set<Id> eventIds = New set<Id>();
List<Event__c> eventToUpdate = New List<Event__c>();
map<Id,set<String>> eventAndStatusValues = new map<Id,set<String>>();

if(!Trigger.isDelete){
    for(Guest_New__c guestRecord : trigger.new){
        eventIds.add(guestRecord.Event__c);    
    }
}
else{
    for(Guest_New__c guestRecord : trigger.old){
        eventIds.add(guestRecord.Event__c);    
    }
}

List<Advanced_Setup__c> statusValues =  [SELECT Status_values__c, Event__c FROM Advanced_Setup__c WHERE Responded__c = True AND Event__c =: eventIds];

for(Advanced_Setup__c statusRecord: statusValues){
    if(eventAndStatusValues.containsKey(statusRecord.Event__c)){
        eventAndStatusValues.get(statusRecord.Event__c).add(statusRecord.Status_values__c);
    }else{
        eventAndStatusValues.put(statusRecord.Event__c , new set<string>{statusRecord.Status_values__c})
    }
}

for(Guest_New__c guest : [Select Id,Status__c,Event__c from Guest_New__c where Event__c =: eventIds])
{
    if(eventAndStatusValues.containsKey(guest.Event__c)){
        Event__c eventRecord = New Event__c();
        eventRecord.Id = guest.Event__c;
        eventRecord.Total_Responses__c = eventAndStatusValues.get(guest.Event__c).size();
        eventToUpdate.add(eventRecord);
    }
}

update eventToUpdate;
}

Thanks