您位於此處:
使用 Apex 觸發來限制服務資源無法修改兩週內的排班
服務區域範圍管理員可與其服務資源合作,提前計畫排班。例如,您要讓從目前日期開始兩週內的約會預訂可供使用。因此,您希望避免服務資源在該期間修改排班。若要限制服務資源,使其無法更新兩週內的排班,請建立 Apex 觸發。
//Because the next two weeks of time slots are visible to customers and are bookable,
//we want to prevent any updates to those shifts.
//This trigger doesn't allow to create a shift
//where either start time or end time falls under
// 1. Older than today's date
// 2. Two weeks after today's date
trigger TwoWeeksLimitTrigger on Shift (before insert,before update) {
if(trigger.isInsert || trigger.isUpdate){
if (Trigger.isBefore) {
for(Shift shift : Trigger.New) {
Date todayDate = date.today();
Date twoWeekDate = todayDate.addDays(14);
if(shift.StartTime < todayDate || shift.StartTime > twoWeekDate){
trigger.new[0].StartTime.addError('Customers are already booking slots for this time period and so changing Shifts is not allowed.');
}
else if(shift.EndTime < todayDate || shift.EndTime > twoWeekDate){
trigger.new[0].EndTime.addError('Customers are already booking slots for this time period and so changing Shifts is not allowed.');
}
}
}
}
}
