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