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
Peter Martensen 8Peter Martensen 8 

Can I prevent a "before insert, before update" Apex Trigger from firing if a checkbox = true?

I need to prevent an Apex Trigger from firing during the cloning of a record.  Can I use the value of a checkbox to prevent the trigger from firing?
Best Answer chosen by Peter Martensen 8
Raj VakatiRaj Vakati
Ty like this
 
trigger triggerCase on Case (after delete, after insert, after undelete, after update, before delete, before insert, before update) {
    if(Trigger.isBefore){
		if(Trigger.new[0].checkbox__c){
		// Do do any tiink	
			// You can move all before logic in the apex class and call the class from the trigger
		}else{
			call logic here 
			
		}
		
	}
	
	
}

 

All Answers

Raj VakatiRaj Vakati
yes .. try like below 
 
trigger triggerCase on Case (after delete, after insert, after undelete, after update, before delete, before insert, before update) {
    if(Trigger.isBefore){
		if(checkbox__c){
		// Do do any tiink	
			// You can move all before logic in the apex class and call the class from the trigger
		}else{
			call logic here 
			
		}
		
	}
	
	
}


 
Peter Martensen 8Peter Martensen 8
Rajamohan, Thanks for the response. I put my field name “IsCloned__c” into the code you wrote, and I get an error that the variable does not exist. Do you know what I’m doing wrong? Peter
Raj VakatiRaj Vakati
Ty like this
 
trigger triggerCase on Case (after delete, after insert, after undelete, after update, before delete, before insert, before update) {
    if(Trigger.isBefore){
		if(Trigger.new[0].checkbox__c){
		// Do do any tiink	
			// You can move all before logic in the apex class and call the class from the trigger
		}else{
			call logic here 
			
		}
		
	}
	
	
}

 
This was selected as the best answer
Rahul Chaudhary OfficialRahul Chaudhary Official
Hey Peter,

try

if(Trigger.Before) {
    for(YourObjectName obj : Trigger.new/old(depending your scenario)) {
        if(obj.IsCloned__c) {
           //Do your logic
        }
    }
}
Cromer JonesCromer Jones
To solve the issue in Salesforce, one of the solutions is to create a Wrapper class with the hashcode and equals method in the class. The hashcode method creates a unique id for the individual wrapper class instance. This unique id is in turn used by Salesforce to ensure uniqueness of the records mypremiercreditcard (https://mypremiercreditcard.me).
ray carryray carry
try this
if(Trigger.Before) {
    for(YourObjectName obj : Trigger.new/old(depending your scenario)) {
        if(obj.IsCloned__c) }
else{
(Trigger.new[0].checkbox__c)
}

        }
    }
}
I hope you get best answer then contact me at Mypremiercreditcard (https://mypremiercreditcard.website)