Just wanted to know if the below Trigger can be further optimized. Requirement is to look for specific case record types with Approval and delete related case task that has got 'Credit Claim' in subject.
trigger DeleteRelatedTasks on Case (before insert, before update) {
list<case> relatedCase = new list<case>();
// Get the approved case record
for (case myCase : Trigger.new) {
if (myCase.VXRecordTypeName__c == 'Credit') {
if (myCase.VXTL_Approval_Status__c == 'Approved' || myCase.VXTL_Approval_Status__c == 'Decline') {
relatedCase.add(myCase);
}
}
// Delete related tasks
string caseId = myCase.Id;
list<Task> deleteTasks = [select whatid,Subject from Task where WhatId = :caseId];
for (Task removeTasks : deleteTasks){
if (removeTasks.Subject.contains('Credit Claim')) {
delete deleteTasks;
}
}
}
}