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
Abby StockerAbby Stocker 

Trigger Error1

What would I do without the community!? 
Please assist with the error for the trigger. Thank you! 

CLASS------->
public class RelatedTasks { 
    public static boolean taskmethod(){
        List<Task> tasks = [SELECT id, status from Task WHERE WhatId =:ApexPages.CurrentPage().getparameters().get('id') and status = 'Open'];
        boolean isOpen = false;
        for(Task t : tasks){        
            if(t.Status =='Open'){
                isOpen = true;           
            }
        } 
        return isOpen;
    }
}


TRIGGER------------>
trigger getOpenTasks on Case (before update) {
    for(Case c : Trigger.New){        
        If(RelatedTasks.taskmethod().isOpen==true){
            c.adderror('You cannot close a case with an open task');
        }
    }
}
ERROR---------> Variable does not exist: isOpen

THANK YOU!!!
Best Answer chosen by Abby Stocker
Ajeet28augAjeet28aug
Use the below. I forgot to typecast
trigger getOpenTasks on Case (before update) {
    Map<Id,Case> mapCaseById = new Map<Id,Case>([Select id, (Select id from tasks WHERE status = 'Open') from Case where Id in: Trigger.NewMap.keySet() ]);
	for(Case c: Trigger.new){
		if(mapCaseById.get(c.Id).tasks.size() > 0){
			c.adderror('You cannot close a case with an open task');
		}
	}
}

All Answers

Ajeet28augAjeet28aug
Hi Abby,

Can you update the trigger as follows
trigger getOpenTasks on Case (before update) {
    for(Case c : Trigger.New){        
        If(RelatedTasks.taskmethod()){
            c.adderror('You cannot close a case with an open task');
        }
    }
}

Just wondering, what's the use case for this: From a trigger, you are calling an apex class and in that, you are using ApexPages.CurrentPage().getparameters().get('id').

Let's say when you'll update the case from data loader or apex, then how will you get the value for 
ApexPages.CurrentPage().getparameters().get('id')


Regards
-Ajeet JainSalesforcegig (https://salesforcegig.com)
 
Abby StockerAbby Stocker
Ajeet, I also have this class involved. The use case is if a user is trying to close a case that has open tasks associate with it, the trigger should fire the error message. So I have a class that gets the current case record Id and a class that finds all associated open tasks and now I need to figure out how to make it fire once a user marks the case as 'closed'.


public class currentCaseRecord {
        Case currentRecord;
    public currentCaseRecord (ApexPages.StandardController controller){
        currentRecord = [Select Id FROM Case Where Id =:ApexPages.CurrentPage().getparameters().get('id')];
        }
    public case getcurrentRecord(){
        return currentRecord;
    }
}
Nashle PakNashle Pak
Hello Abby,

Test this code please :
trigger getOpenTasks on Case (before update) { 
boolean isOpen = false; 
for(Case c : Trigger.New){ 
isOpen = RelatedTasks.taskmethod(); 
If(isOpen){ 
c.adderror('You cannot close a case with an open task'); 
} 
} 
}


 
 
Abby StockerAbby Stocker
Thank you both so much! I wish I could best answer both! lol 
Ajeet28augAjeet28aug
Hi Abby,

I believe, following trigger will be sufficient to achieve what you are looking for
trigger getOpenTasks on Case (before update) {
    Map<Id,Case> mapCaseById = [Select id, (Select id from tasks WHERE status = 'Open') from Case where Id in: Trigger.NewMap.keySet() ];
	for(Case c: Trigger.new){
		if(mapCaseById.get(c.Id).tasks.size() > 0){
			c.adderror('You cannot close a case with an open task');
		}
	}
}

Regards
-Ajeet Jain​​​​​​​
Ajeet28augAjeet28aug
Using the 'RelatedTasks' class, will make the query in the loop.
Abby StockerAbby Stocker
These codes seem to work together but I am getting errors when creating cases which obviously should not happen. I am guessing that I am missing a step. 
First class is identifying the current case record ID:

public class currentCaseRecord {
        Case currentRecord;
    public currentCaseRecord (ApexPages.StandardController controller){
        currentRecord = [Select Id,status FROM Case Where Id =:ApexPages.CurrentPage().getparameters().get('id') and status='closed'];
        }
    public case getcurrentRecord(){
        return currentRecord;
    }
}

Second class is returning all associated open tasks with that current case ID:

public class RelatedTasks { 
    public static boolean taskmethod(){
        List<Task> tasks = [SELECT id, status from Task WHERE WhatId =:ApexPages.CurrentPage().getparameters().get('id') and status = 'Open'];
        boolean isOpen = false;
        for(Task t : tasks){        
            if(t.Status =='Open'){
                isOpen = true;           
            }
        } 
        return isOpen;
    }
}

Finally, the trigger is supposed to be performing this : If a user closes a case but the case has associated open tasks, Throw an error "You cannot close a case with an open task". 


trigger getOpenTasks on Case (before update) {
boolean isOpen = false;   
for(Case c : Trigger.New){       
        isOpen = RelatedTasks.taskmethod();
        If(isOpen){
            c.adderror('You cannot close a case with an open task');
        }
    }
}

All of them are saved, No errors but when I try to even create a case I get yelled at with a bunch of errors. I do not want this trigger to fire unless the user attempts to close the case. 

Any thoughts? (This is the very first trigger/class I have ever written so it has been quite a learning experience! lol) Thank you for any help!!!

 
Ajeet28augAjeet28aug
Can you just replace your trigger with the below code and see if your requirement is fulfilled:
trigger getOpenTasks on Case (before update) {
    Map<Id,Case> mapCaseById = [Select id, (Select id from tasks WHERE status = 'Open') from Case where Id in: Trigger.NewMap.keySet() ];
	for(Case c: Trigger.new){
		if(mapCaseById.get(c.Id).tasks.size() > 0){
			c.adderror('You cannot close a case with an open task');
		}
	}
}

 
Nashle PakNashle Pak
User-added image
Abby StockerAbby Stocker
Hi Ajeet! I tried your code but received this error:
Illegal assignment from List<Case> to Map<Id,Case>
Ajeet28augAjeet28aug
Use the below. I forgot to typecast
trigger getOpenTasks on Case (before update) {
    Map<Id,Case> mapCaseById = new Map<Id,Case>([Select id, (Select id from tasks WHERE status = 'Open') from Case where Id in: Trigger.NewMap.keySet() ]);
	for(Case c: Trigger.new){
		if(mapCaseById.get(c.Id).tasks.size() > 0){
			c.adderror('You cannot close a case with an open task');
		}
	}
}
This was selected as the best answer
Abby StockerAbby Stocker
You are awesome thank you so much. I will try that in just a moment :)
Abby StockerAbby Stocker
Hi Ajeet! It seems the code did work and I am not getting those angry error messages I have been getting so used to :) Thank you SOO much for that! My only problem now is that the trigger is firing during any update at all to the case. I only want it to fire when the user attempts to change the status to "Closed" and their are still associated open tasks. 
Ajeet28augAjeet28aug
In that case, use the below code:
trigger getOpenTasks on Case (before update) {
    Map<Id,Case> mapCaseById = new Map<Id,Case>([Select id, (Select id from tasks WHERE status = 'Open') from Case where Id in: Trigger.NewMap.keySet() ]);
	for(Case c: Trigger.new){
		if( c.Status != Trigger.oldMap.get(c.Id).status && c.status == 'Closed' && mapCaseById.get(c.Id).tasks.size() > 0){
			c.adderror('You cannot close a case with an open task');
		}
	}
}

 
Abby StockerAbby Stocker
Ok, you are AWESOME. That works!!!!! Last question and I will leave you alone :) I just now noticed an issue. So, this works if the case is open and someone tries to change it to closed with an open task (Thanks again) but what I have noticed is if the case is closed and someone tries to create an open task on a closed case, it lets them. Im guessing this could be resolved by creating another trigger on the task object for before insert? to check if the associated case is closed before allowing insertion?
Ajeet28augAjeet28aug
Yes. A 'Before insert' trigger on Task will resolve that issue. 
Abby StockerAbby Stocker
perfect. Thanks again for all of your help!!!
Abby StockerAbby Stocker
Ajeet, If you have a moment, We have myself and another person now starting to work on the new trigger but I figured I would reach out to you as well. Using the above trigger for tasks, which aspects of it would you change to create one that doesnt allow a task to be created if the associated case status is closed?