function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Abraham kumar 4Abraham kumar 4 

Need to Bulkify Trigger Not Supporting Mass update

Hi All,

I have the below Trigger which updates a Guest(Object) Email ID when the Contact(Object) Email ID is updated. The Error now is Im getting an Apex Time Limit Exceeded Error,As the Number of Guests is more for a single contact it is not able to change the email id to all the related guests email id.ie)not able to update the email id of all the guests associated with the single contact so the time limit exceeded error.

Please suggest ways to Support this for many Records. In a way to bulkify.
rigger CopycontactAddressToguestAddress on contact (after update) {
    Map<Id, String> m = new Map<Id, String>();
for (contact c : Trigger.new) {

        contact old = Trigger.oldMap.get(c.Id);
 if (c.Email != old.Email) {
            m.put(c.Id, c.Email);
        }
    }
    if (m.size() > 0) {
        Guest_New__c[] guests = [
                select Id, contact__c
                from Guest_New__c
                where contact__c in :m.keySet()
                ];
        for (Guest_New__c c : guests) {
            c.Email__c = m.get(c.Contact__c);
        }
       
        update guests;
    }
    }

Thanks
Abraham
Nitish KumarNitish Kumar
Hi Abraham,

You can use Process builder to achieve this functionality. Is there any specific need to use trigger ?
Abraham kumar 4Abraham kumar 4
No Nitish, There is nothing specific to use trigger. Please let me know how to do this in process builder. 

Thanks
Abraham
Vivek DeshmaneVivek Deshmane
Hi,
Try below coe 
rigger CopycontactAddressToguestAddress on contact (after update) {
    Map<Id, String> m = new Map<Id, String>();
for (contact c : Trigger.new) {

        contact old = Trigger.oldMap.get(c.Id);
 if (c.Email != old.Email) {
            m.put(c.Id, c.Email);
        }
    }
    if (m.size() > 0) {
        Guest_New__c[] guests = [
                select Id, contact__c
                from Guest_New__c
                where contact__c in :m.keySet() and contact__c!=null
                ];
        for (Guest_New__c c : guests) {
            c.Email__c = m.get(c.Contact__c);
        }
       
        update guests;
    }
    }

 
Nitish KumarNitish Kumar
This video shows how to implement process builder for updating child record when parent record is changing.

https://www.youtube.com/watch?v=1pECPZ0kqpQ
Abraham kumar 4Abraham kumar 4
Hi Vivek,

Sorry Im new..., Can you give me some Expanations on the code By adding this can the problem be solved contact__c!=null.

Thanks
Abraham

 
Abraham kumar 4Abraham kumar 4
Hi Nitish,

For now Ill stick with fixing the error in the Trigger nitish.. Lets not change it fully now to process builder now . please let me know any thoughts on bulkifying this so that it does not give error.
many thanks in Advance
Thanks
Abraham
Nitish KumarNitish Kumar
Hi Abraham , the above code written by you is already bulkified.The issue might be happening in some other contact trigger (check if there are multiple triggers on contact). Try to put a debug statement to find the number of Guest_New__c whenever the email id of contact is changed.
Abraham kumar 4Abraham kumar 4
Hi Nitish,
Yes I use contact update on a different trigger also on the contact object,but Im getting below Details in Debug log. Related to this trigger only i guess
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

Many Thanks in Advance
Abraham
Abraham kumar 4Abraham kumar 4
Hi All,

The issue is with the 3rd line of below code shown in above debug log which gives this error. 
or(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. Many thanks
Abraham
Nitish KumarNitish Kumar
Can you post the full trigger code ?
Abraham kumar 4Abraham kumar 4
Yes Nitish,
Pls find full code
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;

}