Created
December 22, 2017 16:54
-
-
Save dsilvadeepal/1f87a9618cf649240cf0fe2926d1e574 to your computer and use it in GitHub Desktop.
A trigger that sets the contact owner to whomever most recently created a case on the record. Also sets the account owner to whomever most recently created a case on it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Write a trigger that sets the contact owner to whomever most recently created a case on the record | |
In the same trigger set the account owner to whomever most recently created a case on it | |
*/ | |
//After trigger to access Case createdById | |
trigger CaseContactOwner on Case (after insert) { | |
for(Case myCase : Trigger.new) { | |
//make sure there's a contact for null pointer exceptions | |
if(myCase.ContactId != null){ | |
Contact myCon = [SELECT Id | |
FROM Contact | |
WHERE Id = :myCase.ContactId]; | |
//Update the contact | |
myCon.OwnerId = myCase.CreatedById; | |
update myCon; | |
} | |
//Repeat for the account | |
if(myCase.AccountId != null) { | |
Account myAcc = [SELECT Id | |
FROM Account | |
WHERE Id = :myCase.AccountId]; | |
//update the account | |
myAcc.OwnerId = myCase.CreatedById; | |
update myAcc; | |
} | |
} | |
} | |
//Test Class | |
@isTest | |
private class CaseContactOwnerTest { | |
//Only one scenario | |
@isTest static void createCaseOnContact(){ | |
//Create a user | |
User myUser = new User(); | |
myUser.FirstName = 'Matt'; | |
myUser.LastName = 'Lacey'; | |
myUser.Email = 'mlacey@trello.com'; | |
myUser.UserName = 'mlacey@trello.com'; | |
myUser.Alias = 'mlacey'; | |
myUser.ProfileId = [SELECT Id | |
FROM Profile | |
WHERE Name = 'System Administrator'].Id; | |
myUser.TimeZoneSidKey = 'America/Denver'; | |
myUser.LocaleSidKey = 'en_US'; | |
myUser.EmailEncodingKey = 'UTF-8'; | |
myUser.LanguageLocaleKey = 'en_US'; | |
myUser.ManagerId = null; | |
insert myUser; | |
Contact myCon = new Contact(); | |
myCon.FirstName = 'Tessa'; | |
myCon.LastName = 'Trimble'; | |
myCon.OwnerId = myUser.Id; | |
insert myCon; | |
Case myCase = new Case(); | |
myCase.Subject = 'Windsor Hotels Repair AC Vent'; | |
myCase.ContactId = myCon.Id; | |
insert myCase; | |
Contact updatedCon = [SELECT OwnerId | |
FROM Contact | |
WHERE Id = :myCon.Id]; | |
System.assertEquals(UserInfo.getUserId(), updatedCon.OwnerId); | |
} | |
@isTest static void createCaseOnAccount(){ | |
//Create a user | |
User myUser = new User(); | |
myUser.FirstName = 'Matt'; | |
myUser.LastName = 'Lacey'; | |
myUser.Email = 'mlacey@trello.com'; | |
myUser.UserName = 'mlacey@trello.com'; | |
myUser.Alias = 'mlacey'; | |
myUser.ProfileId = [SELECT Id | |
FROM Profile | |
WHERE Name = 'System Administrator'].Id; | |
myUser.TimeZoneSidKey = 'America/Denver'; | |
myUser.LocaleSidKey = 'en_US'; | |
myUser.EmailEncodingKey = 'UTF-8'; | |
myUser.LanguageLocaleKey = 'en_US'; | |
myUser.ManagerId = null; | |
insert myUser; | |
Account myAcc = new Account(); | |
myAcc.Name = 'Windsor Hotels'; | |
myAcc.OwnerId = myUser.Id; | |
insert myAcc; | |
Case myCase = new Case(); | |
myCase.Subject = 'Repair TV units'; | |
myCase.AccountId = myAcc.Id; | |
insert myCase; //Case created Id will be me | |
Account updatedAcc = [SELECT OwnerId | |
FROM Account | |
WHERE Id = :myCase.AccountId]; | |
System.assertEquals(UserInfo.getUserId(), updatedAcc.OwnerId); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment