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
Everton CP7Everton CP7 

Case doesn't close if have tasks opened

I'm trying to do a trigger that doesn't let anyone close the case if I have tasks opened in the case.

 

If you try to close the case an error will appear.

 

Code:

 

trigger ChamadoAbertoTarefaAberta on Case (after update) {

    {
        List <Task> statuslist = [SELECT id, Status FROM Task WHERE WhatId IN :trigger.new AND Status statuslist.add(t.WhatId, true)];
        MAP<id,string> statuslist= new LIST<Id,String>(); 
   		for(Task t : trigger.new) 
        }
            
            for(Case c : trigger.new()){
            if(map.get(c.Id) == true){
            c.addError(My error here); 
            }
            else{
            break; 
}
            }
}

 I don't know where I'm going wrong.

 

Thanks for help !

Best Answer chosen by Admin (Salesforce Developers) 
jbroquistjbroquist

Since you wanto to prevent the user from updating the Case to closed, you would want this trigger to fire "before update". I've commented up the following code for you to help ya out.

 

trigger ChamadoAbertoTarefaAberta on Case (before update) 
{
    //create a map of open tasks related to the cases
    Map<Id, Task> taskMap = new Map<Id, Task>();

    //query open tasks related to cases and populate map
    for(Task t : [SELECT Id, WhatId FROM Task WHERE IsClosed=false AND WhatId IN :trigger.newMap.keySet()])
    {
        taskMap.put(WhatId, t);
    }

    //iterate through updated cases and add errors if open tasks exist
    for(Case c : Trigger.new)
    {
        //check if the case has any open tasks and has just been changed to closed
        if(taskMap.containsKey(c.Id) && c.IsClosed && c.IsClosed != Trigger.oldMap.get(c.Id).IsClosed)
            c.addError('YOUR ERROR HERE');
    }
}

 Let me know if you have any questions.

All Answers

jbroquistjbroquist

Since you wanto to prevent the user from updating the Case to closed, you would want this trigger to fire "before update". I've commented up the following code for you to help ya out.

 

trigger ChamadoAbertoTarefaAberta on Case (before update) 
{
    //create a map of open tasks related to the cases
    Map<Id, Task> taskMap = new Map<Id, Task>();

    //query open tasks related to cases and populate map
    for(Task t : [SELECT Id, WhatId FROM Task WHERE IsClosed=false AND WhatId IN :trigger.newMap.keySet()])
    {
        taskMap.put(WhatId, t);
    }

    //iterate through updated cases and add errors if open tasks exist
    for(Case c : Trigger.new)
    {
        //check if the case has any open tasks and has just been changed to closed
        if(taskMap.containsKey(c.Id) && c.IsClosed && c.IsClosed != Trigger.oldMap.get(c.Id).IsClosed)
            c.addError('YOUR ERROR HERE');
    }
}

 Let me know if you have any questions.

This was selected as the best answer
Everton CP7Everton CP7

Thanks 

I tried somethings and get errors with a couple of things.
I wrote some comments here and finally I decide to change "before update" to "after update" and it works.

@IsTest(SeeAllData=true)
public class NaoFechaChamado_test{
 
     static testmethod void MyUnitTest(){
 
     Case c = [select id from Case where Status='Fechado'];
      
     Task tsk  = new Task(Status='Não iniciado', Priority='Normal', whatId=c.id);
     insert tsk;
 
    c.Status = 'Novo';
    update c;
}
}

 Can you help me in this code too?

 

Thanks again !!