When I try to create a OutOfOffice record with which the other user is not my own user in apex, Salesforce throw the DML error ("INVALID_ACCESS_LEVEL, Only Self User can update the Out Of Office Message"). Only throw this error if the Profile user isn´t System Administrator.
The class is without sharing.
public without sharing class OutOfOfficeHelper {
This is the class:
public static void set(Id userId, String message, String sStartDate, String sEndDate) {
DateTime startDate = DateTime.ValueofGmt(sStartDate.replace('T', ' '));
DateTime endDate = DateTime.ValueofGmt(sEndDate.replace('T', ' '));
// extract existing record
Id outOfOfficeId;
for(OutOfOffice o: [SELECT Id FROM OutOfOffice WHERE UserId = :userId]) {
outOfOfficeId = o.Id;
}
upsert new OutOfOffice(
Id = outOfOfficeId,
UserId = userId,
IsEnabled = true,
StartDate=startDate,
EndDate=endDate,
message=message);
}
Regards