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
Anuj Joshi 42Anuj Joshi 42 

2 triggers on same object

Hi All,

I have 2 triggers written on contact object. I need to make it to one trigger and retain the functionalities of both triggers. 
 
trigger DupePreventer on contact
                               (before insert, before update) {

    Map<String, contact> contactMap = new Map<String, contact>();
    for (contact contact : System.Trigger.new) {
        
        
        if ((contact.Email != null) &&
                (System.Trigger.isInsert ||
                (contact.Email != 
                    System.Trigger.oldMap.get(contact.Id).Email))) {
        
           
    
            if (contactMap.containsKey(contact.Email)) {
                contact.Email.addError('Another new contact has the '
                                    + 'same email address.');
            } else {
                contactMap.put(contact.Email, contact);
            }
       }
    }
    
 
    for (contact contact : [SELECT Email FROM contact
                      WHERE Email IN :contactMap.KeySet()]) {
        contact newcontact = contactMap.get(contact.Email);
        newcontact.Email.addError('A contact with this email '
                               + 'address already exists.');
    }
}
 
trigger DeactivateUser on Contact (before update) 
{
	
	Map<Id, Boolean> contactToActivateDeactivate = new Map<Id, Boolean>(); 
	
	
	for(Contact contact : trigger.new)
	{
		
		
		if (contact.Blacklisted__c != trigger.oldMap.get(contact.Id).Blacklisted__c)
		{
			
			contactToActivateDeactivate.put(contact.Id, !contact.Blacklisted__c);
		}
	}

	
	
	if (!contactToActivateDeactivate.IsEmpty())
	{
		activateDeactivateUser activateDeactivateUser = new activateDeactivateUser(); //referring to class
		activateDeactivateUser.updateUser(contactToActivateDeactivate);
	} 
}

Kindly provide me solution

Thanks,
Anuj​
SKSANJAYSKSANJAY
Hi Anuj,

FYI

trigger ContactTrigger on contact(before insert, before update) {

    Map<String, contact> contactMap = new Map<String, contact>();
    Map<Id, Boolean> contactToActivateDeactivate = new Map<Id, Boolean>(); 
    
    //Look for records being inserted
    for (contact contact : System.Trigger.new) {      
        if ((contact.Email != null) && (System.Trigger.isInsert || (contact.Email != System.Trigger.oldMap.get(contact.Id).Email))) {
            if (contactMap.containsKey(contact.Email)) {
                contact.Email.addError('Another new contact has the '
                                    + 'same email address.');
            } else {
                contactMap.put(contact.Email, contact);
            }
       }
       
       if(System.Trigger.isUpdate){
            if (contact.Blacklisted__c != trigger.oldMap.get(contact.Id).Blacklisted__c){            
                contactToActivateDeactivate.put(contact.Id, !contact.Blacklisted__c);
            }
       }
    }
    
    //Look for duplicate email records in existing Contact records
    for (contact contact : [SELECT Email FROM contact
                      WHERE Email IN :contactMap.KeySet()]) {
        contact newcontact = contactMap.get(contact.Email);
        newcontact.Email.addError('A contact with this email '
                               + 'address already exists.');
    }
    
    if (!contactToActivateDeactivate.IsEmpty()){
        activateDeactivateUser activateDeactivateUser = new activateDeactivateUser(); //referring to class
        activateDeactivateUser.updateUser(contactToActivateDeactivate);
    } 
}

Hope this will help you

Thanks,
Sanjay
Manish  ChoudhariManish Choudhari
Hi Anuj,

You need to create a class to handle the trigger logic and same needs to be called from your trigger. Use below code:

Trigger:
trigger ContactInsertAndUpdate on contact
                               (before insert, before update) {

    if(Trigger.isInsert){//check if it is insert trigger
    	ContactTriggerHandler.DupePreventer(); //call DupePreventer method which needs to run on befoe insert
    } else if(Trigger.isUpdate){//checks if it is update trigger
    	ContactTriggerHandler.DupePreventer();// Call both method as both needs to 
    	ContactTriggerHandler.DeactivateUser();// be called from update trigger
    }
}

ContactTriggerHandler:
public class ContactTriggerHandler{

    public static void DupePreventer(){
    	Map<String, contact> contactMap = new Map<String, contact>();
		for (contact contact : System.Trigger.new) {
			
			
			if ((contact.Email != null) &&
					(System.Trigger.isInsert ||
					(contact.Email != 
						System.Trigger.oldMap.get(contact.Id).Email))) {
			
			   
		
				if (contactMap.containsKey(contact.Email)) {
					contact.Email.addError('Another new contact has the '
										+ 'same email address.');
				} else {
					contactMap.put(contact.Email, contact);
				}
		   }
		}
		
	 
		for (contact contact : [SELECT Email FROM contact
						  WHERE Email IN :contactMap.KeySet()]) {
			contact newcontact = contactMap.get(contact.Email);
			newcontact.Email.addError('A contact with this email '
								   + 'address already exists.');
		}
    
    }
    
    public static void DeactivateUser(){
    	Map<Id, Boolean> contactToActivateDeactivate = new Map<Id, Boolean>(); 
	
	
		for(Contact contact : trigger.new)
		{
			
			
			if (contact.Blacklisted__c != trigger.oldMap.get(contact.Id).Blacklisted__c)
			{
				
				contactToActivateDeactivate.put(contact.Id, !contact.Blacklisted__c);
			}
		}
	
		
		
		if (!contactToActivateDeactivate.IsEmpty())
		{
			activateDeactivateUser activateDeactivateUser = new activateDeactivateUser(); //referring to class
			activateDeactivateUser.updateUser(contactToActivateDeactivate);
		} 

	}
}

Let me know if you need further help on this.

Thanks,
Manish
ShikibuShikibu
Consider this Lightweight Apex Trigger Framework (http://chrisaldridge.com/triggers/lightweight-apex-trigger-framework/).
ShikibuShikibu
https://github.com/ChrisAldridge/Lightweight-Trigger-Framework
Deepali KulshresthaDeepali Kulshrestha
Hi Anuj ,


trigger ContactInsertAndUpdate on contact
                               (before insert, before update) {

    if(Trigger.isInsert){                //check if it is insert trigge

        ConTriggerHelper.beforeInsert();             //call DupePreventer method which needs to run on befoe insert

    } else if(Trigger.isUpdate){            //checks if it is update trigger
        ConTriggerHelper.beforeInsertUpdate();            // Call both method as both needs to 
        ConTriggerHelper.beforeUpdateOnly();            // be called from update trigger
    }
}



public class ContactTriggerHandler{

    public static void beforeInsertUpdate(){
        // your code which is run in both condition.  
    
    }
    
    public static void beforeUpdateOnly(){
        //your code which is run in only in update condition only.
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha