+ Start a Discussion
Andżela ŚrodaAndżela Środa 

How to add one trigger to existing one?

Hi :) I wrote one trigger which prevent User to Completed Work Order, when Service_Type__c field is empty on Work Order Line Item. I have already existing trigger on Work Order object. How to split it in one trigger?

WorkOrderTrigger: 
trigger WorkOrderTrigger on WorkOrder (after update, before delete) {

  if(Trigger.isBefore) {
        if(Trigger.isDelete) {
            WorkOrderTriggerService.countOpenWorkOrdersOfDeviceFromWoDelete(Trigger.oldMap);
        }
    }

    if(Trigger.isAfter) {
        if(Trigger.isUpdate) {
            WorkOrderTriggerService.autoUpdateNextCalibrationDate(Trigger.new, Trigger.oldMap);
            WorkOrderTriggerService.countOpenWorkOrdersOfDeviceFromWoUpdate(Trigger.new, Trigger.oldMap);
        }
    }

}



My Trigger: 
trigger ValidateWorkOrderLineItem on WorkOrder (before update) {
    Map<String, WorkOrder> mapWoToId = new Map<String,WorkOrder>();
    for(WorkOrder w : Trigger.New) {
      if(w.Status =='Completed'){
        mapWoToId.put(w.Id, w);
      }
    }
    
    List<WorkOrderLineItem> woLineItemList = [select woli.Status, woli.workOrderId from WorkOrderLineItem woli where woli.WorkOrderId IN :mapWoToId.keySet() and woli.Status != 'Completed' and woli.WOLI__c = 'Parent' and woli.Parent_WOLI__c = TRUE and woli.Service_type__c = '']; 
  
    for(WorkOrderLineItem woli : woLineItemList) {
        WorkOrder parentWO = mapWoToId.get(woli.workOrderId);
        parentWO.addError('Please fill the Service Type on each Parent Work Order Line Item');
    } 
  }


Thank you for answears!  
Best Answer chosen by Andżela Środa
Maharajan CMaharajan C
Hi Andżela,

I will suggest you to go with the same handler class and old trigger.  Just add one more method in class to handle your logic . like below:

Update the WorkOrderTriggerService Apex Class:
 
Public class WorkOrderTriggerService{
    // Existing Method
	public static void countOpenWorkOrdersOfDeviceFromWoDelete(List<WorkOrder> wList){
	}
	
	// Existing Method
    public static void autoUpdateNextCalibrationDate(List<WorkOrder> wList, Map<Id,WorkOrder> oldWMap){
	}
	
    // Existing Method
	public static void countOpenWorkOrdersOfDeviceFromWoUpdate(List<WorkOrder> wList, Map<Id,WorkOrder> oldWMap){
	}
	
	public static void validateWOLServiceType(List<WorkOrder> wList){
		Map<String, WorkOrder> mapWoToId = new Map<String,WorkOrder>();
		for(WorkOrder w : wList) {
		  if(w.Status =='Completed'){
			mapWoToId.put(w.Id, w);
		  }
		}
    
		List<WorkOrderLineItem> woLineItemList = [select woli.Status, woli.workOrderId from WorkOrderLineItem woli where woli.WorkOrderId IN :mapWoToId.keySet() and woli.Status != 'Completed' and woli.WOLI__c = 'Parent' and woli.Parent_WOLI__c = TRUE and woli.Service_type__c = '']; 
	  
		for(WorkOrderLineItem woli : woLineItemList) {
			WorkOrder parentWO = mapWoToId.get(woli.workOrderId);
			parentWO.addError('Please fill the Service Type on each Parent Work Order Line Item');
		} 
	}

}

Update the WorkOrderTrigger  like below:
 
trigger WorkOrderTrigger on WorkOrder (after update, before delete, before update) {

  if(Trigger.isBefore) {
        if(Trigger.isDelete) {
            WorkOrderTriggerService.countOpenWorkOrdersOfDeviceFromWoDelete(Trigger.oldMap);
        }
		
		if(Trigger.isUpdate) {
			WorkOrderTriggerService.validateWOLServiceType(Trigger.new)
		}
    }

    if(Trigger.isAfter) {
        if(Trigger.isUpdate) {
            WorkOrderTriggerService.autoUpdateNextCalibrationDate(Trigger.new, Trigger.oldMap);
            WorkOrderTriggerService.countOpenWorkOrdersOfDeviceFromWoUpdate(Trigger.new, Trigger.oldMap);
        }
    }

}


Thanks,
Maharajan.C

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Andzela,

You can modify the existing trigger as below.
 
trigger WorkOrderTrigger on WorkOrder (after update, before delete,before update) {

  if(Trigger.isBefore) {
        if(Trigger.isDelete) {
           WorkOrderTriggerService.countOpenWorkOrdersOfDeviceFromWoDelete(Trigger.oldMap);
        }
    }

    if(Trigger.isAfter) {
        if(Trigger.isUpdate) {
           WorkOrderTriggerService.autoUpdateNextCalibrationDate(Trigger.new, Trigger.oldMap);
            WorkOrderTriggerService.countOpenWorkOrdersOfDeviceFromWoUpdate(Trigger.new, Trigger.oldMap);
        }
    }
    if(Trigger.isupdate && Trigger.isbefore)
    {
    Map<String, WorkOrder> mapWoToId = new Map<String,WorkOrder>();
    for(WorkOrder w : Trigger.New) {
      if(w.Status =='Completed'){
        mapWoToId.put(w.Id, w);
      }
    }
    
    List<WorkOrderLineItem> woLineItemList = [select woli.Status, woli.workOrderId from WorkOrderLineItem woli where woli.WorkOrderId IN :mapWoToId.keySet() and woli.Status != 'Completed' and woli.WOLI__c = 'Parent' and woli.Parent_WOLI__c = TRUE and woli.Service_type__c = ''];
  
    for(WorkOrderLineItem woli : woLineItemList) {
        WorkOrder parentWO = mapWoToId.get(woli.workOrderId);
        parentWO.addError('Please fill the Service Type on each Parent Work Order Line Item');
    } 
    }

}

Or else you can write the method in handler as well. 

If this solution helps, please mark it as best answer.

Thanks,

 
Maharajan CMaharajan C
Hi Andżela,

I will suggest you to go with the same handler class and old trigger.  Just add one more method in class to handle your logic . like below:

Update the WorkOrderTriggerService Apex Class:
 
Public class WorkOrderTriggerService{
    // Existing Method
	public static void countOpenWorkOrdersOfDeviceFromWoDelete(List<WorkOrder> wList){
	}
	
	// Existing Method
    public static void autoUpdateNextCalibrationDate(List<WorkOrder> wList, Map<Id,WorkOrder> oldWMap){
	}
	
    // Existing Method
	public static void countOpenWorkOrdersOfDeviceFromWoUpdate(List<WorkOrder> wList, Map<Id,WorkOrder> oldWMap){
	}
	
	public static void validateWOLServiceType(List<WorkOrder> wList){
		Map<String, WorkOrder> mapWoToId = new Map<String,WorkOrder>();
		for(WorkOrder w : wList) {
		  if(w.Status =='Completed'){
			mapWoToId.put(w.Id, w);
		  }
		}
    
		List<WorkOrderLineItem> woLineItemList = [select woli.Status, woli.workOrderId from WorkOrderLineItem woli where woli.WorkOrderId IN :mapWoToId.keySet() and woli.Status != 'Completed' and woli.WOLI__c = 'Parent' and woli.Parent_WOLI__c = TRUE and woli.Service_type__c = '']; 
	  
		for(WorkOrderLineItem woli : woLineItemList) {
			WorkOrder parentWO = mapWoToId.get(woli.workOrderId);
			parentWO.addError('Please fill the Service Type on each Parent Work Order Line Item');
		} 
	}

}

Update the WorkOrderTrigger  like below:
 
trigger WorkOrderTrigger on WorkOrder (after update, before delete, before update) {

  if(Trigger.isBefore) {
        if(Trigger.isDelete) {
            WorkOrderTriggerService.countOpenWorkOrdersOfDeviceFromWoDelete(Trigger.oldMap);
        }
		
		if(Trigger.isUpdate) {
			WorkOrderTriggerService.validateWOLServiceType(Trigger.new)
		}
    }

    if(Trigger.isAfter) {
        if(Trigger.isUpdate) {
            WorkOrderTriggerService.autoUpdateNextCalibrationDate(Trigger.new, Trigger.oldMap);
            WorkOrderTriggerService.countOpenWorkOrdersOfDeviceFromWoUpdate(Trigger.new, Trigger.oldMap);
        }
    }

}


Thanks,
Maharajan.C
This was selected as the best answer