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,



You can not update UserId field on OutOfOffice object. You can only - (Create, Filter, Group, Sort) the UserId of OutOfOffice object.

Refer here:- OutOfOffice Soap Developer Guide

As a workround, You can delete the OutOfOffice record and create a new one with the new userid.

Also, Users can set only their own out-of-office message. An admin can set an out-of-office message for any user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.