0

This code is used to prevent a case from being closed when it still has open tasks. When the user tries to close a case with an open task an error message pops up.

  1. Instead of using a Status from the "Status" picklist, I opted to use the API IsClosed, but it doesn't allow me to write "TRUE" or "FALSE" so I don't know how to reproduce it in the test class;
  2. This prevention is being used in one specific record type "Retenção e Cancelamento" from the object Case;
  3. The trigger is activated after update a Case record;
  4. In the test class I don't know how to get the change of isClosed to "TRUE" on after update;
  5. There is a process builder that creates a task whenever a case is created;
  6. I'm trying to get 100% code coverage of this code, but I got just 77% of code coverage. The last two lines are not being covered, like the error message code line;

Handler Class

public class Handler_Case_Tasks { 

public static void openTasks(Map<Id, Case> mapCase) {

    Set<Id> caseIds = new Set<Id>(); 

    Id caseRecordTypeId = SObjectType.Case.getRecordTypeInfosByDeveloperName().get('Retencao_cancelamento').getRecordTypeId(); 

    for (Case c : mapCase.values()) {
        if (c.RecordTypeId == caseRecordTypeId && c.isClosed) caseIds.add(c.Id);            
    }



    List<Task> lTask = [SELECT Id, WhatId FROM Task WHERE WhatId IN:caseIds AND isClosed = False];

    for (Task t : lTask) {
        if (mapCase.containsKey(t.WhatId)) {
            mapCase.get(t.WhatId).addError(Label.Open_tasks);
        }
    }
}
}

Test Class

@isTest
public class Teste_Trigger_Case_Tasks {

@TestSetup
static void setup(){
    Id[] groupIds = new Id[]{
        Schema.SObjectType.Account.getRecordTypeInfosByName().get('Pessoa Física Brasil').getRecordTypeId(),
            Schema.SObjectType.Segmento__c.getRecordTypeInfosByName().get('Cargo Tracck').getRecordTypeId(),
            Schema.SObjectType.Case.getRecordTypeInfosByName().get('Retenção e Cancelamento').getRecordTypeId()
            };

                Account account = new Account(
                    RecordTypeId = groupIds[0],
                    Name = 'accountTrigger',
                    Documento__c = '57018098033'
                );

    insert account;            

    Segmento__c segmento = new Segmento__c(
        RecordTypeId = groupIds[1],
        Conta__c = account.Id,
        Name = 'SegmentoTrigger',
        Cadencia__c = '2',
        Circuito__c = '2'
    );

    insert segmento;            

    Case aCase = new Case(
        AccountId = account.Id,
        Segmento_caso__c = segmento.id,      
        Tempo_excedido__c = false,              
        RecordTypeId = groupIds[2],
        Motivos_cancelamento__c = 'Ouvidoria',
        Observacoes__c = 'TESTE'
    );        
    insert aCase;       
}

@isTest static void createCaseTest(){

    try{                
       // I queried the inserted case in the "Setup" and then I tried to manipulate it to reproduce the expected message error, but even so I didn't get this. 

    } catch(Exception e){
        Test.startTest();            
        Boolean expectedExceptionThrown = e.getMessage().contains(Label.Open_Tasks) ? true : false;  
        System.AssertEquals(true,expectedExceptionThrown);
        Test.stopTest();  
    }       
} 
}

Trigger

trigger Trigger_Case on Case (before insert, before update, after update) {    

switch on trigger.operationType {
    when BEFORE_INSERT {}
    when BEFORE_UPDATE {}
    when AFTER_UPDATE {Handler_Case_Tasks.openTasks(Trigger.newMap);}
}
}
  • 1
    in your trigger it looks like you are also checking for task and and then getting attached cases but in your test class it doesn't look look like you are adding any task – User6670 Apr 5 at 17:28
  • I forgot to say. There's a process builder that creates a task when a case is created, that's why I didn't create a task in the test class. – Matheus Antonio Apr 5 at 19:23
  • You can select which statuses represent "Closed" for cases. When you set the case to one of those Status values, Salesforce will set the IsClosed field to TRUE automatically. IsClosed is a calculated field and is read-only. Are you setting the status to a value that represents "closed" in your test class? – Kris Goncalves Apr 5 at 22:30
  • shouldn't you check for one then create if not. I don't find it good to rely on process builder and NOT check that it worked before attempting to test. very much prefer to know my test data is ready. – Ronnie Apr 5 at 22:31
  • @KrisGoncalves yes, you're right. I did it before, but I wanted to avoid using values from the Status picklist, and it is because different org languages would affect the coverage. I changed my test class to a value from Status already, and now my trigger is "before update". To avoid my code not being covered when org language is changed I set a custom label, and then I let some possible translations. I don't think this is the best way of getting this done, but this is a way to go. – Matheus Antonio Apr 6 at 2:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.