You need to sign in to do that
Don't have an account?
Nabeel 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.
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.
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
May I know the reason for choosing Apex trigger for the above requriement? This could be done using Process Builder itself.
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.
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
Thanks for the resposne :)