Scheduling > Dispatch Console > Appendix > Invoking MTTS Rule Calculation from Custom Work Order Trigger
Invoking MTTS Rule Calculation from Custom Work Order Trigger
MTTS calculation happens in the managed trigger on the Work Order object. So, if an MTTS rule is defined based on a custom field whose value is set in another custom trigger on Work Order, the rule may not get applied depending on the sequence in which the triggers are executed by Salesforce. This is because Salesforce does not define the order of execution of triggers. In such a scenario, if the MTTS rule is required to be applied after the custom trigger executes, invoke the ServiceMax code in the custom trigger code to do MTTS calculation after the custom trigger has updated the required work order fields.
// Existing custom trigger code that updates Work Order field info goes here
...
//Code to perform pre-calculation, that includes MTTS calculation
list<string> lstSubModules = new list<string>{'GLOB001','OMAX003'};
SVMXC.COMM_Utils_ManageSettings commSettings = new SVMXC.COMM_Utils_ManageSettings();
map<string, Map<String, String>> AllsvmxSettingList = commSettings.SVMX_getSettingList(lstSubModules);
Map<String, String> svmxSettingListOMAX003 = AllsvmxSettingList.get('OMAX003');
svmxSettingListOMAX003.putAll(AllsvmxSettingList.get('GLOB001'));
List<Account> LstAccount = new List<Account>();
List<SVMXC__Installed_Product__c> LstComponent = new List<SVMXC__Installed_Product__c>();
List<Case> LstCase = new List<Case>();
public set<string> setAccIds = new set<string>();
public set<string> setCases = new set<string>();
public set<string> setComponent = new set<string>();
for(Integer i =0; i<Trigger.new.size(); i++)
{
if(Trigger.new[i].SVMXC__Company__c!=null)
setAccIds.add(Trigger.new[i].SVMXC__Company__c);
if(Trigger.new[i].SVMXC__Component__c!=null)
setComponent.add(Trigger.new[i].SVMXC__Component__c);
if(Trigger.new[i].SVMXC__Case__c!=null)
setCases.add(Trigger.new[i].SVMXC__Case__c);
}
if(setAccIds!=null && setAccIds.size()>0)
LstAccount=[select id,SVMXC__Preferred_Technician__c,SVMXC__Access_Hours__c,SVMXC__Access_Hours__r.IsActive from
Account where id in :setAccIds];
if(setCases!=null && setCases.size()>0)

LstCase = [select SVMXC__BW_Time_Zone__c,SVMXC__Preferred_Start_Time__c ,SVMXC__Preferred_End_Time__c,
SVMXC__Booking_Window__c, SVMXC__BW_Territory__c, SVMXC__BW_Date__c, SVMXC__Service_Contract__r.Id,
SVMXC__Service_Contract__r.SVMXC__Primary_Technician__c,Type, SystemModstamp, SuppliedPhone, SuppliedName,
SuppliedEmail, SuppliedCompany, Subject, Status, SVMXC__Scheduled_Date__c, SVMXC__SLA_Clock_Paused__c, SVMXC__SLA_Clock_Pause_Time__c,
SVMXC__SLA_Clock_Pause_Restart_Time__c, SVMXC__SLA_Clock_Pause_Reason__c, SVMXC__SLA_Clock_Pause_Minutes__c,
SVMXC__SLA_Clock_Pause_Hours__c, SVMXC__SLA_Clock_Pause_Days__c, SVMXC__SLA_Clock_Extension_Minutes__c,
SVMXC__SContract_Business_Hour__c, SVMXC__Restoration_Internal_By__c, SVMXC__Restoration_Customer_By__c,
SVMXC__Resolution_Internal_By__c, SVMXC__Resolution_Customer_By__c, SVMXC__PM_Plan__c, SVMXC__Onsite_Response_Internal_By__c,
SVMXC__Onsite_Response_Customer_By__c, SVMXC__Is_PM_Case__c, SVMXC__Initial_Response_Internal_By__c,
SVMXC__Initial_Response_Customer_By__c, SVMXC__Clock_Paused_Forever__c, SVMXC__Actual_Restoration__c, SVMXC__Actual_Resolution__c,
SVMXC__Actual_Onsite_Response__c, SVMXC__Actual_Initial_Response__c, SVMXC__Warranty__c, SVMXC__Top_Level__c, SVMXC__Site__c,
SVMXC__Service_Contract__c, SVMXC__SLA_Terms__c, SVMXC__Product__c, SVMXC__Perform_Auto_Entitlement__c, SVMXC__Entitlement_Notes__c,
SVMXC__Component__c, SVMXC__Component_Zip__c, SVMXC__Component_Street__c, SVMXC__Component_State__c, SVMXC__Component_Country__c,
SVMXC__Component_City__c, SVMXC__Billing_Type__c, SVMXC__Auto_Entitlement_Status__c, Reason, Priority, OwnerId, Origin,
LastModifiedDate, LastModifiedById, IsEscalated, IsDeleted, IsClosed, Id, Description, CreatedDate, CreatedById, ContactId,
ClosedDate, CaseNumber, AccountId From Case where id in : setCases];
if(setComponent!=null && setComponent.size()>0)

LstComponent = [SELECT Id, SVMXC__Preferred_Technician__c,SVMXC__Top_Level__c,SVMXC__Access_Hours__c,SVMXC__Access_Hours__r.IsActive,
SVMXC__Product__c, SVMXC__Longitude__c, SVMXC__Latitude__c from SVMXC__Installed_Product__c where Id IN :setComponent];
SVMXC.OMAX_PreCalculation Omaxp = new SVMXC.OMAX_PreCalculation(svmxSettingListOMAX003,LstAccount,LstCase,LstComponent);
Omaxp.DoPrecalculation(Trigger.new,null);
// Custom trigger code continues
...
* 
This code executes not just MTTS calculation', but also executes all the other applicable dispatch calculations such as SLA, Territory Match rule, Dispatch Process, etc. For the custom trigger, set the ServiceMax version to 10.x or above.
For the above code snippet, the following is the sample UT code:
@isTest(seeAllData = true)
Private with sharing class OMAX_CustomPreCalculation_UT
{
static testMethod void OMAX_CustomPreCalculation_UT()
{
list<BusinessHours> lstBusinessHour = [ select WednesdayStartTime, WednesdayEndTime, TuesdayStartTime, TuesdayEndTime,
ThursdayStartTime, ThursdayEndTime, SundayStartTime, SundayEndTime, SaturdayStartTime, SaturdayEndTime, MondayStartTime,
MondayEndTime, IsDefault, IsActive, Id, FridayStartTime, FridayEndTime from BusinessHours where isDefault = true limit 1];
Account objAccount = new Account(Name = 'test');
insert objAccount;
system.assert(objAccount.Name!=null);
Product2 objProduct = new Product2(Name = 'prodPR',ProductCode='prod',SVMXC__Inherit_Parent_Warranty__c =True);
insert objProduct;
SVMXC__Installed_Product__c objIB = new SVMXC__Installed_Product__c();
objIB.Name = 'Test IB1';
objIB.SVMXC__Status__c = 'Installed';
objIB.SVMXC__Product__c = objProduct.Id;
objIB.SVMXC__Date_Installed__c = System.Today();
objIB.SVMXC__Company__c = objAccount.Id;
objIB.SVMXC__Access_Hours__c = lstBusinessHour[0].id;
insert objIB;
Case objCase = new Case();
objCase.Origin='Web';
objCase.Status = 'New';
objCase.Priority='Medium';
objCase.SVMXC__Preferred_Start_Time__c = system.now();
objCase.SVMXC__Preferred_Start_Time__c = system.now()+10;
insert objCase;
SVMXC__Service_Order__c objWorkOrder = new SVMXC__Service_Order__c();
objWorkOrder.SVMXC__Company__c = objAccount.Id;
objWorkOrder.SVMXC__Case__c = objCase.Id;
objWorkOrder.SVMXC__Component__c = objIB.Id;
insert objWorkOrder;
update objWorkOrder;
}
}
Was this helpful?