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
fiona gentryfiona gentry 

Trigger Apex Not working for First Task Action directly calls 2nd Task Action

Here there is a Trigger which calls Trigger handler and in turn Trigger handler calls the apex class,this  apex class is to create tasks on Case record 
and the goal here is to prevent a Task from being created for 2 scenarios
  • First  Task with the Action__c field being ‘airtel account contact Research’ and once user creates a Task with Action__c as "‘airtel account contact Research’"
  • then they should only  able to create second  Task with Action__c as '1st airtel contact'',

Now after these 2 tasks creation is done then user can create any task with any picklist value in Action__c field

Here is Trigger which calls TaskTriggerHandler
TaskTrigger
trigger TaskTrigger on Task (before insert) {
    if(Trigger.isBefore&&Trigger.isInsert){
        TaskTriggerHandler.beforeInsert(Trigger.New);
    }    
}

Here is TaskTriggerHandler 
public class TaskTriggerHandler {
    
    public static void beforeInsert(List<Task> tasks){
        final string ERTTRECORDTYPENAME = 'ERTT_Task_Record_Type'; // Filter String to get the ERTT Task REcordType Id
        Id recordTypeId_ERT = [select id, DeveloperName from RecordType where sobjecttype = 'Task' AND developername =: ERTRECORDTYPENAME].Id;
        
        List<Task> ERTTasks = new List<Task>();
        Set<Id> caseIdset = new Set<Id>();
        
        for(Task taskProcess : tasks){
            
           
            // Functionality Added to Validate only the ERTT_Task_Record_Type Record type. 
            if(taskProcess.RecordTypeId == recordTypeId_ERT && taskProcess.Action__c != 'airtel account contact Research' )
            {
                ERTTasks.add(taskProcess);
                if(taskProcess.WhatId != null)
                {
                   caseIdset.add(taskProcess.WhatId);
                }
            }
        }
     
        // Making sure there is data in the ERTT Tasks and What Id of the Tasks 
        if(!ERTTasks.isEmpty() && !CaseIdset.isEmpty())
        {
           ERTT_TaskCreationValidation.isairtelAccountResearchcompleted_New(ERTTasks,CaseIdset);
           
            
           
        }
    }
    
}

Here is Apex class with Logic written
 
public class ERTT_TaskCreationValidation {
    
   
     public static void isairtelAccountResearchcompleted_New(List<Task> ERTTTasks,Set<Id> caseIdset)    {
        list<Task> lstTAsk=new list<Task>();
		map<Id, Case> mapCaseWithTaskList = new  map<Id, Case>([select id, (Select id, Action__c from Tasks )  from Case where Id in: caseIdset]); 
        for(Task t : ERTTTasks)
        {
            Boolean validationFlag = true;
            Boolean validationFlag2 = true;
            System.debug('mapCaseWithTaskList.gett.WhatId.tasks'+ mapCaseWithTaskList.get(t.WhatId).tasks);
            if(mapCaseWithTaskList.get(t.WhatId).tasks.size()>0)
            	lstTAsk.add(mapCaseWithTaskList.get(t.WhatId).tasks);
            for(Task t1:  mapCaseWithTaskList.get(t.WhatId).tasks){
                if(t1.Action__c == 'airtel account contact Research')
                {
                    validationFlag = false;
                }
            }
            if(validationFlag){
                t.addError('Please Create a task for airtel account contact Research before creating any other Task');
            }
System.debug(' lstTAsk' +lstTAsk);
            if(lstTAsk.size()==2 && t.Action__c == '2nd field'){
                     validationFlag2=false;
                 }
                 if(validationFlag2){
                    t.addError('Please Create a task for 2nd field before creating any other Task');
                 }
            
        }
    }
    

}

Now the problem is when Task is getting created at Case ,the second Error message is thrown which is "Please Create a task for 2nd field before creating any other Task" whereas requirment is to first show the first error message 'Please Create a task for airtel account contact Research before creating any other Task' and then the second error message
User-added image


'Please Create a task for 2nd field before creating any other Task'

Any idea what is going wrong in the apex class ?

Your reply with  resolution is appreciated

Regards

Fiona

 
Best Answer chosen by fiona gentry
Lukesh KarmoreLukesh Karmore
Hi fiona gentry, 
I got your point.
you used   below two boolean variables,
Boolean validationFlag = true;
Boolean validationFlag2 = true;

when you create first record  validationFlag the\is variable become false but  validationFlag2 this remail true  thats the reason.

for(Task t1: mapCaseWithTaskList.get(t.WhatId).tasks){
if(t1.Action__c == 'airtel account contact Research') {
validationFlag = false;
validationFlag2 = false;
}
}

replace  above code  with your  code in ERTT_TaskCreationValidation class.
and let me know .
If it helps please mark the best answer it will helps other.
Thanks ,
Lukesh