+ Start a Discussion
Nishant SunadhamNishant Sunadham 

Test class needed for this Trigger

Hi!
I'm trying to figure out how to write a test class for this trigger. Any help/ideas would be appreciated :)

trigger TR_UpdateCaseOwners on Case (before insert) {
    
    //Variable declarations
    List <Id> AccountIds = new List <Id>();
    List <Programs__C> ProgramOwner = new List<Programs__C>();
    List <Contact> contacts = new List<Contact>();
    List <User> OwnerID = new list<User>();
    
     for (Case c :Trigger.new) {
        if (c.type == 'Program Inquiry') {
           
      Set<Contact> con = new set<Contact>([Select Intent__C, CSU_Primary_Degree_Interest__c, CSU_Primary_Certificate_Interest__c FROM CONTACT WHERE ID = : c.ContactId]);
        
      for (Contact B : con){
      
      if (B.Intent__C == 'Degree') {
      
    //Update case owner
    ProgramOwner = ([SELECT ProgramOwner__c from Programs__C WHERE NAME =: B.CSU_Primary_Degree_Interest__c]);         
    
    //ProgramOwner SObject
    for (Programs__C P : ProgramOwner){
    
    //Query user table for program owner GUID    
    OwnerID = ([Select ID FROM User WHERE Name =: P.ProgramOwner__c]);    
    
    for (User U : OwnerID){
    
    //Assign new ID    
    C.OwnerID = U.ID; 
    
    }     
   }      
 }
    
 else  {
    
    if (B.Intent__C == 'Certificate')
    {
    
   //Update case owner
    ProgramOwner = ([SELECT ProgramOwner__c from Programs__C WHERE NAME =: B.CSU_Primary_Degree_Interest__c]);         
    
    //ProgramOwner SObject
    for (Programs__C P : ProgramOwner){
    
    //Query user table for program owner GUID    
    OwnerID = ([Select ID FROM User WHERE Name =: P.ProgramOwner__c]);    
    
    for (User U : OwnerID){
    
    //Assign new ID    
    C.OwnerID = U.ID; 
    
}     
}      
}
}
}
}
}
}
Deepak_KumarDeepak_Kumar

Hi Nishant,

Here is the test class.

 

@IsTest
private class TR_UpdateCaseOwnersTest {
    static testMethod void testBehavior() {
        UserRole role = new UserRole(DeveloperName = 'MyCustomRole', Name = 'My Role');
        insert role;
        User u = new User(
                ProfileId = [SELECT Id FROM Profile WHERE Name = 'YOUR PROFILE'].Id,
                LastName = 'Test User',
                Email = 'puser000@amamama.com',
                Username = 'puser000@amamama.com' + System.currentTimeMillis(),
                CompanyName = 'TEST',
                Title = 'title',
                Alias = 'alias',
                TimeZoneSidKey = 'America/Los_Angeles',
                EmailEncodingKey = 'UTF-8',
                LanguageLocaleKey = 'en_US',
                LocaleSidKey = 'en_US',
                UserRoleId = role.Id
        );

        Programs__C programs = new Programs__C();
        programs.Name = 'TestDegree';
        programs.ProgramOwner__c = 'Test User';
        insert programs;
        Contact contact = new Contact();
        contact.LastName = 'Test';
        contact.Intent__C = 'Degree';
        contact.CSU_Primary_Degree_Interest__c = 'TestDegree';
        insert contact;
        Contact contact1 = new Contact();
        contact1.LastName = 'Test';
        contact1.Intent__C = 'Certificate';
        contact1.CSU_Primary_Degree_Interest__c = 'TestDegree';
        insert contact1;

        List<Case> cases = new List<Case>();
        for (Integer i = 0; i < 2; i++) {
            Case caseObj = new Case();
            caseObj.type = 'Program Inquiry';
            caseObj.Status = 'New';
            caseObj.Origin = 'Email';
            caseObj.ContactId = contact.Id;
            if(i== 0){
                caseObj.ContactId = contact1.Id;
            }
            cases.add(caseObj);
        }

        insert cases;
    }

I hope this solves your problem.
Please mark as best answer if this helps you.

Thanks 
Deepak​​​​​​​