+ Start a Discussion
Nabeel Munir 9Nabeel Munir 9 

Passing same record in trigger.newMap and trigger.oldMap

Hello,
I am trying to prevent trigger from creating duplicate requests.
For example, if the case status is changed from something else to close, then create a new request. but, if the case status is not changed from something else to closed but it was already closed and edit button was clicked and without doing any changes, it was saved, in this case it should not create a new request.

I have trigger and handler class all set up for this but, i don't know why old values are being passed in both of my maps in handler class even though inside the trigger, it is fetching new and old values. still in my map inside handler class, it is passing old values where status is New.
I checked the log.

Below is what i have tried
public class MaintenanceRequestHelper {
 
    public static Boolean isFirstTime = true;

public static void updateWorkOrders(Map<Id,Case> newVal, Map<Id,Case> oldVal) {
    Map<Id,Case> maintenance_Req_List =new Map<Id,Case>([SELECT id,Origin,Status,Date_Due__c,priority, Case.vehicle__c,Equipment__c,Equipment__r.Maintenance_Cycle__c,
                                         Type,Subject,Date_Reported__c FROM Case Where Status in:newVal.KeySet()]);
  Map<Id,Case> maintenance_Req_Listt =new Map<Id,Case>([SELECT id,Origin,Status,Date_Due__c,priority, Case.vehicle__c,Equipment__c,Equipment__r.Maintenance_Cycle__c,
                                         Type,Subject,Date_Reported__c FROM Case Where id in:oldVal.KeySet()]);
        closedMaintenanceRequestList(maintenance_Req_List,maintenance_Req_Listt);
 }
        public static void closedMaintenanceRequestList(Map<Id, Case> newMap,  Map<Id, Case> oldMap)
         {
            List<Case> new_Maintenance_Req_Lst = new List<Case>();
            for (Case c: newMap.Values()) {
                if(c.Type=='Repair'||c.Type=='Routine Maintenance'){
                     System.debug('routine if is true');
                 if (c.Status == 'Closed' && oldMap.get(c.Id).Status != 'Closed' ) {
                System.debug('if executed after being true');
                  
                  Case new_req = new Case();
                  new_req.Subject='Maintenance request';
                  new_req.Type='Routine Maintenance';
                  new_req.Vehicle__c=c.Vehicle__c;
                  new_req.Equipment__c=c.Equipment__c;
                  new_req.Date_Reported__c=Date.today();
                  new_req.Date_Due__c=Date.today().addDays(Integer.valueOf(c.Equipment__r.Maintenance_Cycle__c));
                  new_req.Status='New';
                  new_req.Origin='Phone';
                  new_Maintenance_Req_Lst.add(new_req);
                
            }
        }
            }
        if(new_Maintenance_Req_Lst.size()>0)
        {
         insert new_Maintenance_Req_Lst;
        }
      }
    
    }
and trigger

trigger MaintenanceRequest on Case (before update,after update) {
    
    
        if(MaintenanceRequestHelper.isFirstTime)
        {
            MaintenanceRequestHelper.isFirstTime=false;
            System.debug('Trigegr fired');
            MaintenanceRequestHelper.updateWorkOrders(Trigger.newMap, Trigger.oldMap);          
        }
    
}
Here are the values from log.
For new values:
SSIGNMENT [5]|newVal|{"5002o00002Br9G9AAJ":{"Origin":"Phone","Status":"Closed","LastModifiedDate":"2019-08-05T12:29:24.000Z","IsDeleted":false,"Date_Due__c":"2019-08-09T00:00:00.000Z","Priority":"Medium","Equipment__c":"01t2o000007zDQfAAM","IsClosed":false,"SystemModstamp":"2019-08-05T12:29:24.000Z","BusinessHoursId":"01m2o000000U4OdAA

For old values:
IGNMENT [5]|oldVal|{"5002o00002Br9G9AAJ":{"Origin":"Phone","Status":"New","LastModifiedDate":"2019-08-05T12:29:24.000Z","IsDeleted":false,"Date_Due__c":"2019-08-09T00:00:00.000Z","Priority":"Medium","Equipment__c":"01t2o000007zDQfAAM","IsClosed":false,"SystemModstamp":"2019-08-05T12:29:24.000Z","BusinessHoursId":"01m2o000000U4OdAAK&q

But in my map inside handler class. both values are where status is New. 

Please help. I am totally lost here.
Best Answer chosen by Nabeel Munir 9
Maharajan CMaharajan C
Hi Nabeel,

try the below changes:

Apex Trigger:

trigger MaintenanceRequest on Case (before update,after update) {
        if(MaintenanceRequestHelper.isFirstTime)
        {
            MaintenanceRequestHelper.isFirstTime=false;
            System.debug('Trigegr fired');
            MaintenanceRequestHelper.closedMaintenanceRequestList(Trigger.newMap, Trigger.oldMap);          
        }
    
}


============
Apex Class:

public class MaintenanceRequestHelper {
 
    public static Boolean isFirstTime = true;

        public static void closedMaintenanceRequestList(Map<Id, Case> newMap,  Map<Id, Case> oldMap)
         {
            Map<Id,Case> maintenance_Req_List =new Map<Id,Case>([SELECT id,Equipment__r.Maintenance_Cycle__c  FROM Case Where id in:newMap.KeySet()]);

            List<Case> new_Maintenance_Req_Lst = new List<Case>();
            for (Case c: newMap.Values()) {
                if(c.Type=='Repair'||c.Type=='Routine Maintenance'){
                     System.debug('routine if is true');
                 if (c.Status == 'Closed' && oldMap.get(c.Id).Status != 'Closed' ) {
                System.debug('if executed after being true');
                  
                  Case new_req = new Case();
                  new_req.Subject='Maintenance request';
                  new_req.Type='Routine Maintenance';
                  new_req.Vehicle__c=c.Vehicle__c;
                  new_req.Equipment__c=c.Equipment__c;
                  new_req.Date_Reported__c=Date.today();
                 new_req.Date_Due__c=Date.today().addDays(Integer.valueOf(maintenance_Req_List.get(c.Id).Equipment__r.Maintenance_Cycle__c));
                  new_req.Status='New';
                  new_req.Origin='Phone';
                  new_Maintenance_Req_Lst.add(new_req);
                
            }
        }
            }
        if(new_Maintenance_Req_Lst.size()>0)
        {
         insert new_Maintenance_Req_Lst;
        }
      }
    
    }

Thanks,
Maharajan.C

 

All Answers

Krishnaveni A 2Krishnaveni A 2
Hi Nabeel,

        May I know the reason for choosing Apex trigger for the above requriement? This could be done using Process Builder itself.

                                     User-added image

                                     User-added image

                                    User-added image

I also checked it by edting the case status , edited the case status which is already closed and saved as it is. It is working properly in all scenarios. Please try it out and let me know if any issues? Thanks.
Maharajan CMaharajan C
Hi Nabeel,

try the below changes:

Apex Trigger:

trigger MaintenanceRequest on Case (before update,after update) {
        if(MaintenanceRequestHelper.isFirstTime)
        {
            MaintenanceRequestHelper.isFirstTime=false;
            System.debug('Trigegr fired');
            MaintenanceRequestHelper.closedMaintenanceRequestList(Trigger.newMap, Trigger.oldMap);          
        }
    
}


============
Apex Class:

public class MaintenanceRequestHelper {
 
    public static Boolean isFirstTime = true;

        public static void closedMaintenanceRequestList(Map<Id, Case> newMap,  Map<Id, Case> oldMap)
         {
            Map<Id,Case> maintenance_Req_List =new Map<Id,Case>([SELECT id,Equipment__r.Maintenance_Cycle__c  FROM Case Where id in:newMap.KeySet()]);

            List<Case> new_Maintenance_Req_Lst = new List<Case>();
            for (Case c: newMap.Values()) {
                if(c.Type=='Repair'||c.Type=='Routine Maintenance'){
                     System.debug('routine if is true');
                 if (c.Status == 'Closed' && oldMap.get(c.Id).Status != 'Closed' ) {
                System.debug('if executed after being true');
                  
                  Case new_req = new Case();
                  new_req.Subject='Maintenance request';
                  new_req.Type='Routine Maintenance';
                  new_req.Vehicle__c=c.Vehicle__c;
                  new_req.Equipment__c=c.Equipment__c;
                  new_req.Date_Reported__c=Date.today();
                 new_req.Date_Due__c=Date.today().addDays(Integer.valueOf(maintenance_Req_List.get(c.Id).Equipment__r.Maintenance_Cycle__c));
                  new_req.Status='New';
                  new_req.Origin='Phone';
                  new_Maintenance_Req_Lst.add(new_req);
                
            }
        }
            }
        if(new_Maintenance_Req_Lst.size()>0)
        {
         insert new_Maintenance_Req_Lst;
        }
      }
    
    }

Thanks,
Maharajan.C

 
This was selected as the best answer
Nabeel Munir 9Nabeel Munir 9
Krishnaveni, This task was from apex specialist super badge that i finished. But, when i actually tried in UI to create requests, it worked fine for all those cases whose status was changed from "New" to "Closed". But, when i went ahead and edit already closed case and save it without any changes, it was creating duplicate requests which bothered me. So, I was trying to make it like the way, so, it does not create duplicate request if the case is already closed and for example, user edits it and save it as it was. 
 
Nabeel Munir 9Nabeel Munir 9
Maharajan, Thank you. Your answer helped.
Krishnaveni A 2Krishnaveni A 2
Hi Nabeel,
      Thanks for the resposne :)