function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
EvertonSzekeresEvertonSzekeres 

Test class tasks closed

I'm trying to make my test class understand that all the tasks are closed.
The problem is it in "LIMIT 1" but if I remove "LIMIT 1" I get this:

System.QueryException: List has more than 1 row for assignment to SObject
Stack Trace: Class.Email_abertura_de_vaga_test.myUnitTest001: line 23, column 1

This is my class:

@isTest (seeAllData=true)
public class Email_abertura_de_vaga_test {

static testMethod void myUnitTest001() {

User u = [select id, Name from User where Name='CP7 Marketing'];

Case ct = new Case();
    ct.Func_abertura_chamado__c = 'EVERTON GOMES';
    ct.motivo__c='Abertura de vaga';
    ct.subject='test';
    ct.Description='test test';
    ct.resposta__c=null;
    ct.Nome_atendimento__c=null;
    ct.N_veis__c=null;
    ct.Meios_de_divulga_o__c=null;
    ct.Candidatos_agendados__c=null;
    ct.Candidatos_avaliados__c=null;
    ct.Candidatos_pr_selecionados__c=null;
    ct.status = 'Novo';
insert ct;
     
      Task t = [select id, whatid from Task where isClosed=true LIMIT 1];

ct.status = 'Fechado';
ct.status_do_processo__c = null;
ct.id = t.whatid;     
ct.Func_abertura_chamado__c = 'THIAGO MARTINS';
ct.Nome_atendimento__c='Teste';
    ct.N_veis__c='Recepcionista';
    ct.Meios_de_divulga_o__c='TEste';
    ct.Candidatos_agendados__c=12;
    ct.Candidatos_avaliados__c=10;
    ct.Candidatos_pr_selecionados__c=2;
    ct.In_cio__c= Date.today();
    ct.resposta__c='teste';
update ct;
}


shiv@SFDCshiv@SFDC
Because query [select id, whatid from Task where isClosed=true LIMIT 1]; is returning multiple records(List of records) but you trying to assing this list to a single record reference..
Task T (single record reference) So either you havt to put limit to or you need to declare T as  List<task>T
ct.id = t.whatid; this line will be changed to ct.id = t[0].whatid;
EvertonSzekeresEvertonSzekeres
I have this trigger:

And when I try to deploy my test class, I get my respective error above here.
Because I have in my production different cases with more than one task that is not completed in the same case.

I tried change my test class with you have suggested to me.

So, trying to deploy this test class I get the error:

"Você não pode fechar um chamado com tarefas abertas. "'+t.subject+'" está aberta. Verificar com '+t.owner.name+'" = "You can't close a case with tasks still opened. "'+t.subject+'" is opened. Check with '+t.owner.name+"'
trigger NaoFechaChamado on Case (after update) {
    
    Set <id> ids = new Set <id> ();
    for (Case c: trigger.new){
        if (c.status=='Fechado'){
            ids.add(c.id);
        }

      if(ids.size()>0){
          for(Task t : [SELECT Id, WhatId, owner.name, subject FROM Task WHERE isclosed=false AND (Not subject like '%Fech%') AND whatid IN: ids]){
            
            if(c.status_do_processo__c != 'Extinto'){
            c.addError('Você não pode fechar um chamado com tarefas abertas. "'+t.subject+'" está aberta. Verificar com '+t.owner.name+'');
      }
          }
}
    if(ids.size()>0){
          for(Task t : [SELECT Id, WhatId, owner.name, subject FROM Task WHERE isclosed=false AND (subject like '%Fech%') AND whatid IN: ids]){
            
            if(c.status_do_processo__c != 'Extinto'){
            t.status = 'Completed';
            update t;
      }
          }
}
}
}
Fechado = Closed