Untitled
unknown
plain_text
2 years ago
3.7 kB
2
Indexable
Never
public with sharing class ALIA_SM_ServiceAppointmentItinerario { public ALIA_SM_ServiceAppointmentItinerario() { public static void ItinerarioAgganciato(List<ServiceAppointment> listNew, List<ServiceAppointment> listOld) { //Dichiarazione liste List<id> MainAssignedResource = new list<id>(); List<AssignedResource> AssignedResourceList = new list<AssignedResource>(); List<AssignedResource> AssignedResourceValid = new list<AssignedResource>(); List<id> ParentRecordIdList = new list<id>(); List<WorkOrder> WorkOrderList = new list<WorkOrder>(); List<id> ParentWorkOrderIdList = new list<id>(); List<WorkOrder> ParentWorkOrderList = new list<WorkOrder>(); list<WorkOrder> ParentWorkOrderToUpdate = new list<WorkOrder>(); integer SchedTime; for(ServiceAppointment sa : listNew) { MainAssignedResource.add(sa.ALIA_MainAssignedResource__c); } //Query per recuperare le assigned resource i cui ServiceResourceId rientrano nei SA triggerati AssignedResourceList = [SELECT id, ServiceAppointment.ParentRecordId, ServiceAppointment.SchedStartTime, ServiceAppointment.SchedEndTime, ServiceResourceId FROM AssignedResource WHERE ServiceResourceId in : MainAssignedResource]; SchedTime = 10; ALIA_FieldServiceSettings__c cs = ALIA_FieldServiceSettings__c.getInstance('DefaultValues'); if(cs != null && cs.ALIA_SchedTime__c != null && !String.isEmpty(cs.ALIA_SchedTime__c)) { SchedTime = cs.ALIA_SchedTime__c; } for(AssignedResource ar : AssignedResourceList) { for(ServiceAppointment satrigger : listNew) { if(ar.ServiceResourceId == satrigger.ALIA_MainAssignedResource__c) { if( (ar.ServiceAppointment.SchedStartTime > (satrigger.SchedStartTime.addMinutes(- SchedTime)) && ar.ServiceAppointment.SchedStartTime < (satrigger.EndStartTime.addMinutes(SchedTime))) || (ar.ServiceAppointment.SchedEndTime > (satrigger.SchedStartTime.addMinutes(- SchedTime)) && ar.ServiceAppointment.SchedEndTime < (satrigger.EndStartTime.addMinutes(SchedTime)))) { AssignedResourceValid.add(ar); ParentRecordIdList.add(satrigger.ParentRecordId); } } } } WorkOrderList = [SELECT id, ALIA_IDOrdineSAPFather__c, ParentWorkOrderId FROM WorkOrder WHERE id in : ParentRecordIdList]; for(WorkOrder wo : WorkOrderList) { ParentWorkOrderIdList.add(wo.ParentWorkOrderId) } ParentWorkOrderList = [SELECT id, ALIA_IDOrdineSAP__c FROM WorkOrder WHERE id in : ParentWorkOrderIdList]; for(WorkOrder wo : WorkOrderList) { for(WorkOrder pwo : ParentWorkOrderList) { if(wo.ParentWorkOrderId == pwo.id) { pwo.ALIA_IDOrdineSAP__c = ALIA_IDOrdineSAPFather__c; ParentWorkOrderToUpdate.add(pwo); } } } if(ParentWorkOrderToUpdate.size()>0) { Database.update(ParentWorkOrderToUpdate); //DA RIVEDERE } } } }