+ Start a Discussion
Abby StockerAbby Stocker 

Help!~

I have tried and tried but I keep getting a ton of errors so I am reaching out to you all for help again! I need to write a trigger that will not allow a user to create a task on a case when the case status is "closed". Thank you!!!!
Best Answer chosen by Abby Stocker
Khan AnasKhan Anas (Salesforce Developers) 
Hi Abby,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
trigger PreventTaskCreation on Task (before insert, before update) {
    
    List<Task> relToCaseTasks = new List<Task>(); // store tasks that are related to cases
    Set<Id> caseIds = new Set<Id>(); // stroe the ids of the cases to which tasks are related to
    
    // appropriate error message
    String errMsg = 'Cannot create/update Task because case is already closed.'; 
    
    for(Task var : Trigger.new){
        if(var.WhatId != null){
            if(String.valueOf(var.whatId).startsWith('500')){ // check if the task is related to an case
                relToCaseTasks.add(var); // if yes then hold that task in a list
                caseIds.add(var.whatId); // also store the id of the case record
            }
        }
    }
    
    if(relToCaseTasks != null && relToCaseTasks.size() > 0){
        // query all the case records that had tasks assigned to them
        Map<Id, Case> idToCaseRecMap = new Map<Id, Case>([SELECT Id, Status FROM Case WHERE Id IN:caseIds]); // map to hold the case with their id as the key
        if(idToCaseRecMap != null && idToCaseRecMap.values().size() > 0){
            for(Task var : relToCaseTasks){ // iterate over the selected task records
                Case c = new Case();
                c = idToCaseRecMap.get(var.WhatId); // get the corresponding task record from the map
                if(c != null){
                    if(c.Status == 'Closed'){ 
                        var.addError(errMsg);
                    }                        
                }
            }
        }
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Abby,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
trigger PreventTaskCreation on Task (before insert, before update) {
    
    List<Task> relToCaseTasks = new List<Task>(); // store tasks that are related to cases
    Set<Id> caseIds = new Set<Id>(); // stroe the ids of the cases to which tasks are related to
    
    // appropriate error message
    String errMsg = 'Cannot create/update Task because case is already closed.'; 
    
    for(Task var : Trigger.new){
        if(var.WhatId != null){
            if(String.valueOf(var.whatId).startsWith('500')){ // check if the task is related to an case
                relToCaseTasks.add(var); // if yes then hold that task in a list
                caseIds.add(var.whatId); // also store the id of the case record
            }
        }
    }
    
    if(relToCaseTasks != null && relToCaseTasks.size() > 0){
        // query all the case records that had tasks assigned to them
        Map<Id, Case> idToCaseRecMap = new Map<Id, Case>([SELECT Id, Status FROM Case WHERE Id IN:caseIds]); // map to hold the case with their id as the key
        if(idToCaseRecMap != null && idToCaseRecMap.values().size() > 0){
            for(Task var : relToCaseTasks){ // iterate over the selected task records
                Case c = new Case();
                c = idToCaseRecMap.get(var.WhatId); // get the corresponding task record from the map
                if(c != null){
                    if(c.Status == 'Closed'){ 
                        var.addError(errMsg);
                    }                        
                }
            }
        }
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Abby StockerAbby Stocker
THANK YOU!! It works exactly how it should!
Abby StockerAbby Stocker
Thanks so much for the notes by the way! It really helps understand whats going on! 
Abby StockerAbby Stocker
Khan Anas, I am attempting to take this trigger a step further and I figured I would reach out to you first. Although the trigger is doing what is intended (Thank you again), is there a way to get it to ignore emails? With it the way it stands, users are not able to email on a closed case (which sometimes is necessary). Thank you!!!