Salesforce Asked by marvv on December 27, 2020
I need Test Class to test the standard functionality :
When you change a record’s owner, some items that are related to the record are also transferred to the new owner.
In my case
Case owner changed to ‘Y’ => Event owner changed to ‘Y’
You can check the documentation here : https://help.salesforce.com/articleView?id=account_owner_transfer.htm&type=5
My issue is when i execute the test class, the owner of the event doesn’t change.
Below my TestClass :
(i’ll get the users from my org)
@isTest
private class EventTest {
@isTest
static void ownerEventChangedTest(){
User[] user1 = [SELECT Id FROM User WHERE Username='YYYYY1' LIMIT 1];
System.debug('***** user1 : ' + JSON.serializePretty(user1));
System.runAs(user1[0]){
User[] user2 = [SELECT Id FROM User WHERE Username='YYYYY2' LIMIT 1];
System.debug('***** user2 : ' + JSON.serializePretty(user2));
// Create Case
Case[] cases = new Case[]{};
for(Integer i=0; i<1; i++){
cases.add(new Case());
}
insert cases;
System.debug('***** cases before update : ' + JSON.serializePretty(cases));
// Create Event
Event[] events = new Event[]{};
for(Integer i=0; i<1; i++){
events.add(new Event(WhatId = cases[0].Id,
StartDateTime = Date.today().addDays(2), // Future Event
DurationInMinutes = 60));
}
insert events;
System.debug('***** events inserted : ' + JSON.serializePretty(events));
// Update Event with a new Owner (user2)
Case[] caseToUpdate = new Case[]{};
for(Case c : [SELECT Id, OwnerId FROM Case WHERE Id IN : cases]){
if(!user2.isEmpty()){
System.debug('***** inside for old Owner: ' + JSON.serializePretty(c.OwnerId));
System.debug('***** inside for new Owner : ' + JSON.serializePretty(user2[0].Id));
c.OwnerId = user2[0].Id;
caseToUpdate.add(c);
}
}
// Update the case
Test.startTest();
update caseToUpdate;
Test.stopTest();
// Get the new Owner
String newCaseOwner = [SELECT Id, OwnerId FROM Case LIMIT 1].OwnerId;
System.debug('***** case after update : ' + JSON.serializePretty([SELECT Id, OwnerId FROM Case]));
// Get Events
Event[] events2 = [SELECT Id, Owner.Name, OwnerId, WhatId, StartDateTime, EndDateTime FROM Event];
System.debug('***** event after update of the Case : ' + JSON.serializePretty(events2));
// Check if new owner of Event is the owner of the Case
// ===>
// The owner of the Event IS NOT changed
System.assertEquals(newCaseOwner, events2[0].OwnerId, 'Wrong Owner');
}
}
}
Any idea please?
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP