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
Andrew Hoban 6Andrew Hoban 6 

Multiple IF statements in a trigger

Hi all,

I was wondering if its possible for multiple if statements to fire independantly inside one trigger. I wish to see if a checkbok is either true or false, the date and time field gets populated with the current time and date. This works if im doing one if statement, the trouble begins when i create a similar if statement below. The trigger will populate both the date and time fields instead of just one. I am trying to avoid creating multiple triggers and instead having one. The code  is as follows:
trigger checkedTimeTriggers on Match_Day_Check_List__c (before insert, before update) {

for(match_day_check_list__c t: trigger.new){

if(t.Handshake_Event_Setup__c == True || t.Handshake_Event_Setup__c == False)
    {
    t.Handshake_Event_Setup_Checked_At__c = DateTime.Now();
    }
  
Else{
 if(t.Access_Manager_Checks__c == True || t.Access_Manager_Checks__c == False)
    {
    t.Access_Manager_Checks_Checked_At__c = DateTime.Now();
    }
  }
 }
}

I have tried doing this without the else however the same problem occurs. Thank you.
Best Answer chosen by Andrew Hoban 6
Vatsal KothariVatsal Kothari
Hi,

Please refer below updated code:

trigger checkedTimeTriggers on Match_Day_Check_List__c (before insert, before update) {
	if(Trigger.isInsert){
		for(match_day_check_list__c t: trigger.new){
			t.Handshake_Event_Setup_Checked_At__c = DateTime.Now();
			t.Access_Manager_Checks_Checked_At__c = DateTime.Now();
		}
	}
	
	if(Trigger.isUpdate){
		for(match_day_check_list__c t: trigger.new){
			match_day_check_list__c oldt = Trigger.oldMap.get(t.ID);
			
			if(oldt.Handshake_Event_Setup__c != t.Handshake_Event_Setup__c){
				t.Handshake_Event_Setup_Checked_At__c = DateTime.Now();
			}
			
			if(oldt.Access_Manager_Checks__c != t.Access_Manager_Checks__c){
				t.Access_Manager_Checks_Checked_At__c = DateTime.Now();
			}
			
		}
	}
}
If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal

All Answers

Vatsal KothariVatsal Kothari

Hi,

For both the condition your are populating field with current dat and time.
Even if it is true or false you want to populate field with current date and time than you simply update the field.
For this refer below code:
 

trigger checkedTimeTriggers on Match_Day_Check_List__c (before insert, before update) {
	for(match_day_check_list__c t: trigger.new){
		t.Handshake_Event_Setup_Checked_At__c = DateTime.Now();
		t.Access_Manager_Checks_Checked_At__c = DateTime.Now();
	}
}

Andrew Hoban 6Andrew Hoban 6
Hi,

This works perfectly, however I should have mentioned that I only want the date/time field to update if that particular checkbox has been modified. With this code if anything is checked all of the times change. I am trying to simulate a field last modifed feature. I hope this makes sence.
Vatsal KothariVatsal Kothari
Hi,

Please refer below updated code:

trigger checkedTimeTriggers on Match_Day_Check_List__c (before insert, before update) {
	if(Trigger.isInsert){
		for(match_day_check_list__c t: trigger.new){
			t.Handshake_Event_Setup_Checked_At__c = DateTime.Now();
			t.Access_Manager_Checks_Checked_At__c = DateTime.Now();
		}
	}
	
	if(Trigger.isUpdate){
		for(match_day_check_list__c t: trigger.new){
			match_day_check_list__c oldt = Trigger.oldMap.get(t.ID);
			
			if(oldt.Handshake_Event_Setup__c != t.Handshake_Event_Setup__c){
				t.Handshake_Event_Setup_Checked_At__c = DateTime.Now();
			}
			
			if(oldt.Access_Manager_Checks__c != t.Access_Manager_Checks__c){
				t.Access_Manager_Checks_Checked_At__c = DateTime.Now();
			}
			
		}
	}
}
If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal
This was selected as the best answer
Andrew Hoban 6Andrew Hoban 6
Brilliant mate, thank you.