1

So i have a trigger as below, what it does is after inserting or updating the OutOfOffice record it will create a Out_of_Office__c custom Object record

OutOfOfficeTrigger.apxt

trigger CreateOutOfOfficeRecord on OutOfOffice (after insert, after update) {
    List<Out_of_Office__c> outOfOfficeRecords = new List<Out_of_Office__c>();
    
    for (OutOfOffice ooo : Trigger.new) { 
        // Check if the Start Date has been updated or a new record has been created
        if (Trigger.isInsert || ooo.StartDate != Trigger.oldMap.get(ooo.Id).StartDate) {
            Out_of_Office__c oof = new Out_of_Office__c();

            oof.Start_Date_Time__c = ooo.StartDate;
            oof.End_Date_Time__c = ooo.EndDate;
            oof.Out_of_Office_Message__c = ooo.Message;
            oof.User__c = ooo.UserId;
            // Add any other fields you want to populate here
            outOfOfficeRecords.add(oof);
        }
    }
    
    if (outOfOfficeRecords.size() > 0) {
        insert outOfOfficeRecords;
    }
}

I have Test Class as below

@isTest
private class CreateOutOfOfficeRecordTest {
    @testSetup static void setup() {
        // Insert a test user
        User testUser = new User(
            FirstName = 'John',
            LastName = 'Doe',
            Email = 'johndoe@test.com',
            Username = 'johndoe@test.com',
            Alias = 'johndoe',
            CommunityNickname = 'johndoe',
            ProfileId = UserInfo.getProfileId(),
            TimeZoneSidKey = 'America/Los_Angeles',
            LocaleSidKey = 'en_US',
            EmailEncodingKey = 'UTF-8',
            LanguageLocaleKey = 'en_US'
        );
        insert testUser;
    }
    
    @isTest static void testCreateOutOfOfficeRecord() {
        // Insert a test OutOfOffice record
        OutOfOffice ooo = new OutOfOffice(
            StartDate = Date.today(),
            EndDate = Date.today().addDays(1),
            Message='My message',
            UserId = UserInfo.getUserId()
        );
        insert ooo;
        
        // Verify that a corresponding Out_of_Office__c record was created
        Out_of_Office__c oof = [
            SELECT Start_Date_Time__c, End_Date_Time__c, Out_of_Office_Message__c, User__c
            FROM Out_of_Office__c
            WHERE Start_Date_Time__c = :ooo.StartDate
            AND End_Date_Time__c = :ooo.EndDate
            AND User__c = :ooo.UserId
        ];
        System.assertEquals(ooo.StartDate, oof.Start_Date_Time__c);
        System.assertEquals(ooo.EndDate, oof.End_Date_Time__c);
        System.assertEquals(ooo.UserId, oof.User__c);
    }
}

But when i run this test class and insert OOO in testCreateOutOfOfficeRecord method.

error comes as

FATAL_ERROR System.DmlException: Insert failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, Out of office message is already set, please try to update the message.: [Message]

Really Appreciate any help

1 Answer 1

1

After some trials , it appears that OutOfOffice object is a part of metadata and visible in test classes. I was not able to find documentation supporting the same.

In this case , current logged in user must be having out of office message set.

In case of this particular test class , we can create a user and assign its id while creating OutOfOffice record.

You can assign the id of the testUser created in your testSetup method as below :

OutOfOffice ooo = new OutOfOffice(
            StartDate = Date.today(),
            EndDate = Date.today().addDays(1),
            Message='My message',
            UserId = testUser.id //This should be a new user created in test class 
        );
        insert ooo;
2
  • Thanks Rohit, that worked!
    – Aman Patil
    Mar 8 at 15:49
  • 1
    Welcome , learned something new today.
    – Rohit
    Mar 8 at 15:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .