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 

addError "FIELD_CUSTOM_VALIDATION_EXCEPTION"

I'm trying to deploy this trigger:
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);
        }
    }  
for (Case c: trigger.new) {
      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 = 'Concluído';
            update t;
      }
          }
}
}
}

I don't want to close my cases with tasks opened.
It works fine, but when I will deploy I get this error message "Failure Message: "System.DmlException: Update failed. First exception on row 0 with id 500U0000009hsErIAI; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Você não pode fechar um chamado com tarefas abertas. "Abertura de vaga - Aprovar vaga" está aberta. Verificar com CP7 Diretoria: []", Failure Stack Trace: "Class.Email_abertur..."

@isTest (seeAllData=true)
public class Email_abertura_de_vaga_test {

static testMethod void myUnitTest001() {
          
Case ca = new Case ();
    ca.Func_abertura_chamado__c = 'EVERTON GOMES';
    ca.motivo__c='Abertura de vaga';
    ca.subject='test';
    ca.Description='test test';
    ca.resposta__c=null;
    ca.Nome_atendimento__c='Asassa';
    ca.N_veis__c='Auxiliar';
    ca.Meios_de_divulga_o__c='Asdfgh';
    ca.Candidatos_agendados__c=15;
    ca.Candidatos_avaliados__c=9;
    ca.Candidatos_pr_selecionados__c=1;
insert ca;

ca.resposta__c='teste'; ca.Func_abertura_chamado__c = 'ALAN BRONER';
    ca.motivo__c='Abertura de vaga'; ca.Nome_atendimento__c='Teste';
    ca.N_veis__c='Recepcionista';
    ca.Meios_de_divulga_o__c='TEste';
    ca.Candidatos_agendados__c=12;
    ca.Candidatos_avaliados__c=10;
    ca.Candidatos_pr_selecionados__c=2;
    ca.In_cio__c= Date.today();
update ca;
 }           
}

I thought is because I have this another trigger:

trigger CriarTarefaCase_Abertura_de_vaga on Case (after insert) {

List<Task> tasks = new List<Task>();
    List<Case> cse = Trigger.new;
        
    for (Case c : cse){
            if (c.motivo__c == 'Abertura de vaga' && c.status != 'Fechado'){
          Task tsk = new Task
              (whatID = c.ID,
               Subject = 'Abertura de vaga - Aprovar vaga',
               ActivityDate = Date.today(),
               Ownerid = '005U0000000FDE9');
      tasks.add(tsk);
            }
}
insert tasks;
}

I found the problem, but I don't have any idea what I have to do to deploy my trigger "NaoFechaChamado".
Abhinav GuptaAbhinav Gupta
First of all a general warning, your triggers are not bulkified, you shouldnt do any DML or SOQL in loops. Please google and read about lots of good articles about bulkification practicies. If you dont take care of it now, it will bite you soon. 

It seems their is a validation rule in target org where you are deploying, and your record data is not complying with that. I would suggest examine the target org for validation rules, and fix your test data accordingly. Read more here on official salesforce help : http://help.salesforce.com/apex/HTViewSolution?id=000002540&language=en_US (http://help.salesforce.com/apex/HTViewSolution?id=000002540&language=en_US)


EvertonSzekeresEvertonSzekeres
It can't be a validation rule.
Because I deactivated all of them to see if this is the cause, but it doesn't.
EvertonSzekeresEvertonSzekeres
The error that appears it is from my own trigger "NaoFechaChamado". Because SF understood in the test class "Email_abertura_de_vaga_test" that I'm inserting a case "Abertura de vaga" and the trigger "CriarTarefaCase_Abertura_de_vaga" is understanding when I create the case "Abertura de vaga" it will insert a task, when I try to update the case SF read the case "Abertura de vaga" with a task "Abertura de vaga - Aprovar vaga" and then appears my error.

I don't know what to do to made this test class understood that all the tasks are already closed.