Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

What are test classes for triggers in salesforce?

Asked by Christopher Paterson Mar 2, 2023 711 views 1 answer
Share

About this question

I have an apex trigger which updates the description of cases with the concatenation of its child cases, if it has. If not, it fills the Description field with a default phrase. I'm trying to code the test class for this trigger and I don't know how to continue.


Apex trigger:

trigger CaseTrigger on Case (before update) {
   
    Map> idPadreYSusHijos = new Map>();
    for(Case c :[SELECT Id, ParentId, CaseNumber FROM Case WHERE ParentId IN : Trigger.newMap.keySet()]){
        // Comprobar si los Id ya están en el mapa, si no están se añaden. 
        // De esta forma se evitan duplicados
        if(!idPadreYSusHijos.containsKey(c.ParentId)){
            idPadreYSusHijos.put(c.ParentId, new List());
        }
        // Añadir los casosHijos a sus respectivos padres mediante Id en el mapa
        idPadreYSusHijos.get(c.ParentId).add(c.CaseNumber);
    }
    for(Case caso : Trigger.New){
       List numeroDeCasosHijos = idPadreYSusHijos.get(caso.Id);
        
        if(numeroDeCasosHijos != null){
            caso.Description = String.join(numeroDeCasosHijos, ', ');
        }else{
        // Lista vacía, no hay hijos, asignamos descripción por defecto
        caso.Description = 'Caso sin hijos';
        }
    }
}
Test class:
@isTest
public class CaseTriggerTest {
    
   @testSetup
   static void dataCreation() {
        Case padreConHijos =  new Case(Status = 'New',Origin='Phone');
        Case padreSinHijos =  new Case(Status = 'New',Origin='Phone');
        Case hijo1 = new Case(Status = 'New',Origin='Phone',ParentId=padreConHijos.Id, Description='hijo1');
        Case hijo2 = new Case(Status = 'New',Origin='Phone',ParentId=padreConHijos.Id, Description='hijo2');
        insert padreConHijos;
        insert padreSinHijos;
        insert hijo1;
        insert hijo2;    
    }
    
    @isTest
    static void testCaseTrigger() {
        List casosHijos = new List();
        List casosPadres = new List();
        for(Case c : [SELECT Id,CaseNumber FROM Case WHERE ParentId = null]) {
            casosPadres.add(c);
        }
        //List nombresHijos = new List([SELECT CaseNumber FROM Case WHERE Id IN :casosHijos]);
        update casosPadres;     
        for(Case c : [SELECT Id,CaseNumber FROM Case WHERE ParentId != null]) {
            casosHijos.add(c);
        }
        update casosHijos;
        
        Test.startTest();
        for(Case caso : casosHijos){
        System.assertEquals(caso.Description, 'Caso sin hijos');
            }
        Test.stopTest();
        
        /*Test.startTest();
        for(Case caso : casosHijos){
        System.assertEquals(caso.Description, String.join(casosHijos, ', '));
            }
        Test.stopTest();*/
    }
    
    @isTest
    static void testMapa() {
        Case hijo1;
        Case padreConHijos;
        
        Test.startTest();
        Map> lista = new Map>();
        lista.put(padreConHijos.Id,new List());
        lista.get(padreConHijos.Id).add(hijo1.CaseNumber);
        Test.stopTest();
        
    }
    
}
With the test class I've made I cover 60% of the trigger. I don't know how to test the case where numeroDeCasosHijos !=null and the following lines :
if(!idPadreYSusHijos.containsKey(c.ParentId)){
    idPadreYSusHijos.put(c.ParentId, new List());
}

Your answer

1 Answer

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.