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
BroncoBoyBroncoBoy 

Task Trigger: Reset Checkboxes to Unchecked, Retain Checkbox Values

I have a Task trigger on which I'm tyring to also reset the checkbox values to be unchecked in the before context but still use the initial values (prior to resetting) in the after context.  The idea is to reset them without having to execute a DML update later on in the trigger.  The problem is that the values are not being captured in the before context - they are all remaining false, so I can't use them for logic in the after context.  Thank you in advance for any help you can provide.

Code
 trigger EmailTaskDescription on Task(before insert, after insert, before update, after update)
 {
     Boolean nofityRIW = false;
     Boolean nofityREW = false;
     Boolean nofityBDC = false;
     Boolean nofityGIW = false;
     Boolean nofityGEW = false;

     if (trigger.isBefore)
     {
         Task tt = Trigger.new[0];
         //capture checkbox selections
         nofityRIW = tt.Notify_R_IW__c;
         nofityREW = tt.Notify_R_EW__c;
         nofityBDC = tt.Notify_BDC__c;
         nofityGIW = tt.Notify_G_IW__c;
         nofityGEW = tt.Notify_G_EW__c;
         //reset checkbox selections to unchecked
         tt.Notify_R_IW__c = false;
         tt.Notify_R_EW__c = false;
         tt.Notify_BDC__c = false;
         tt.Notify_G_EW__c = false;
         tt.Notify_G_IW__c = false;
     }
     if (trigger.isAfter)
     {
        //use initial values to execute logic 
        if (nofityRIW)
         {

         }
         else if (nofityREW)
         {

         }
     }
 }
Best Answer chosen by BroncoBoy
Abhishek BansalAbhishek Bansal
Hi,

In order to achieve this you have to create a class with static varibales.
So please create a new Class with Name = "SaveResults" and paste below code in it :
public class SaveResults{
	public static Boolean nofityRIW = false;
	public static Boolean nofityREW = false;
	public static Boolean nofityBDC = false;
	public static Boolean nofityGIW = false;
	public static Boolean nofityGEW = false;
}

Now change your trigger code as follows :
trigger EmailTaskDescription on Task(before insert, after insert, before update, after update)
 {
     if (trigger.isBefore)
     {
         Task tt = Trigger.new[0];
         //capture checkbox selections
         SaveResults.nofityRIW = tt.Notify_R_IW__c;
         SaveResults.nofityREW = tt.Notify_R_EW__c;
         SaveResults.nofityBDC = tt.Notify_BDC__c;
         SaveResults.nofityGIW = tt.Notify_G_IW__c;
         SaveResults.nofityGEW = tt.Notify_G_EW__c;
         //reset checkbox selections to unchecked
         tt.Notify_R_IW__c = false;
         tt.Notify_R_EW__c = false;
         tt.Notify_BDC__c = false;
         tt.Notify_G_EW__c = false;
         tt.Notify_G_IW__c = false;
     }
     if (trigger.isAfter)
     {
        //use initial values to execute logic 
        if (SaveResults.nofityRIW)
         {

         }
         else if (SaveResults.nofityREW)
         {

         }
     }
 }

Please modify your code as suggested above.
Let me know if you have any issue in it.

Thanks,
Abhishek

All Answers

Abhishek BansalAbhishek Bansal
Hi,

In order to achieve this you have to create a class with static varibales.
So please create a new Class with Name = "SaveResults" and paste below code in it :
public class SaveResults{
	public static Boolean nofityRIW = false;
	public static Boolean nofityREW = false;
	public static Boolean nofityBDC = false;
	public static Boolean nofityGIW = false;
	public static Boolean nofityGEW = false;
}

Now change your trigger code as follows :
trigger EmailTaskDescription on Task(before insert, after insert, before update, after update)
 {
     if (trigger.isBefore)
     {
         Task tt = Trigger.new[0];
         //capture checkbox selections
         SaveResults.nofityRIW = tt.Notify_R_IW__c;
         SaveResults.nofityREW = tt.Notify_R_EW__c;
         SaveResults.nofityBDC = tt.Notify_BDC__c;
         SaveResults.nofityGIW = tt.Notify_G_IW__c;
         SaveResults.nofityGEW = tt.Notify_G_EW__c;
         //reset checkbox selections to unchecked
         tt.Notify_R_IW__c = false;
         tt.Notify_R_EW__c = false;
         tt.Notify_BDC__c = false;
         tt.Notify_G_EW__c = false;
         tt.Notify_G_IW__c = false;
     }
     if (trigger.isAfter)
     {
        //use initial values to execute logic 
        if (SaveResults.nofityRIW)
         {

         }
         else if (SaveResults.nofityREW)
         {

         }
     }
 }

Please modify your code as suggested above.
Let me know if you have any issue in it.

Thanks,
Abhishek
This was selected as the best answer
Sanpreet SainiSanpreet Saini
Hello, 
Try something like this. 
 
trigger EmailTaskDescription on Task(before insert, after insert, before update, after update)
 {
     Boolean nofityRIW = false;
     Boolean nofityREW = false;
     Boolean nofityBDC = false;
     Boolean nofityGIW = false;
     Boolean nofityGEW = false;
	 
	static Boolean nofityRIWOLD;
    static Boolean nofityREWOLD;
    static Boolean nofityBDCOLD;
    static Boolean nofityGIWOLD ;
    static Boolean nofityGEWOLD;

	for(task t :trigger.new){
	try{
	 task oldmp = Trigger.oldMap.get(t.Id);
	 nofityRIWOLD = oldmp.Notify_R_IW__c;
	 nofityREWOL  = oldmp.Notify_R_EW__c;
	 //similarly for other values
	}
	catch(exception e){}
	}
	
	
     if (trigger.isBefore)
     {
         Task tt = Trigger.new[0];
         //capture checkbox selections
         nofityRIW = tt.Notify_R_IW__c;
         nofityREW = tt.Notify_R_EW__c;
         nofityBDC = tt.Notify_BDC__c;
         nofityGIW = tt.Notify_G_IW__c;
         nofityGEW = tt.Notify_G_EW__c;
         //reset checkbox selections to unchecked
         tt.Notify_R_IW__c = false;
         tt.Notify_R_EW__c = false;
         tt.Notify_BDC__c = false;
         tt.Notify_G_EW__c = false;
         tt.Notify_G_IW__c = false;
     }
     if (trigger.isAfter)
     {
        //use initial values to execute logic 
		
		//now use something like this
        if (nofityRIWOLD)
         {

         }
         else if (nofityREW)
         {

         }
     }
 }
Regards, 
Sanpreet
 
BroncoBoyBroncoBoy
Sorry forgot to mark these as solved.  Thank you both!  I ended up using Abhishek's solution but I think both could work...

Thanks again!
Bronco