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
Sainath VenkatSainath Venkat 

trigger to check check box on account

Hello guys

I am having one scenario, while creating contact, I will be checking the email id of contact and if duplicate email id found for specific record then I have a Checkbox__c field on account and it should be unchecked.
by default Vheckbox__c will be checked.
While creating contact for a particular account,if email is already found on that particular account then checkbox should be unchecked
Newbie__SalesforceNewbie__Salesforce
Hello Sainath,


You can refer the following trigger helper for your requirement.
Let me know if u face any issue for this trigger helper.
 
/**
 * @Description: Check for existing email values on insert call and uncheck parent account
 * @Author: 
 * @Revision history:
 *  Sr. No.     Date        Author              Remarks
 * ------------------------------------------------------------
 *  
 **/
public with sharing class DuplicateContactValidationsHandler {

    /**
     * @Description: validating email fields values.
     * @Date:
     * @Param: 
                @param 1 - List of contact records inserted.
     **/
    public static void validateEmailPhoneFields (List<Contact> lstContact) {
        Set<String> setEmailValues = new Set<String>();
        List<String> lstEmailValues = new List<String>();
		List<Id> lstContactIds = new List<Id>();
		List<Account> lstAccountsToUpdate = new List<Account>();

        for(Contact contactObj : lstContact) {
            lstEmailValues.add(contactObj.Email);
        }

        for(Contact contactObj : [SELECT Email
                                    FROM Contact
                                   WHERE Email IN :lstEmailValues]) {
            setEmailValues.add(contactObj.Email);
        }

        for(Contact contactObj : lstContact ) {
            if(contactObj.Email != null ) {
                if(setEmailValues.contains(contactObj.Email) ) {
                   lstContactIds.add(contactObj.AccountId);
                }
            }
        }
		
		for(Account accountInstance : [SELECT Checkbox__c FROM ACCOUNT WHERE Id IN : lstContactIds]) {
			accountInstance.Checkbox__c = False;
			lstAccountsToUpdate.add(accountInstance);
		}
	
		if(lstAccountsToUpdate.size() > 0) {
			update lstAccountsToUpdate;
		}
    }
}

 
Sainath VenkatSainath Venkat
Hello Newbie,

Thanks for the quick update, can you please help me how to call this handler into trigger if possible.
Newbie__SalesforceNewbie__Salesforce
Contact Trigger 
 
trigger ContactTrigger on Contact (before insert, before update) {
    if(Trigger.isBefore) {
        if(Trigger.isUpdate || Trigger.isInsert) {
            DuplicateContactValidationsHandler.validateEmailPhoneFields(Trigger.new);
        }
    }
}

 
Sainath VenkatSainath Venkat
I have changed my trigger according the code you have given but I am getting some error at Line 11 that "Missing at 'else'",Can you please review my code.
trigger PreventDuplicateContactAndCount on Contact (before insert, before update, after insert, after update, after delete) {


    if(trigger.isBefore)
    {
       if(Trigger.isUpdate || Trigger.isInsert) {
            DuplicateContactValidationsHandler.validateEmailPhoneFields(Trigger.new);
        }
    }  
    }
    else if (trigger.isAfter) {
Set <Id> accountIds = new Set <Id>();
List <Account> lstAccountsToUpdate = new List <Account>();
 if(Trigger.isInsert){
    for(Contact con:trigger.new){
        accountIds.add(con.accountID);
    }
}
if(Trigger.isUpdate|| Trigger.isDelete){
    for(Contact con:trigger.old){
        accountIds.add(con.accountID);
    }
}

for(Account acc:[SELECT Id,Name,Number_Of_Contacts__c,(Select Id from Contacts) from Account where Id IN: accountIds AND Account.Recordtype.DeveloperName='Business_Organization_Mastered']){
    Account accObj = new Account ();
    accObj.Id = acc.Id;
    accObj.Number_Of_Contacts__c= acc.Contacts.size();
    lstAccountsToUpdate.add(accObj);
}

UPDATE lstAccountsToUpdate;
}

 
Newbie__SalesforceNewbie__Salesforce
trigger PreventDuplicateContactAndCount on Contact (before insert, before update, after insert, after update, after delete) {


    if(trigger.isBefore) {
       if(Trigger.isUpdate || Trigger.isInsert) {
            DuplicateContactValidationsHandler.validateEmailPhoneFields(Trigger.new);
        }
    } 
	
	if (trigger.isAfter) {
		Set <Id> accountIds = new Set <Id>();
		List <Account> lstAccountsToUpdate = new List <Account>();
		if(Trigger.isInsert){
			for(Contact con:trigger.new) {
				accountIds.add(con.accountID);
			}
		}
		if(Trigger.isUpdate|| Trigger.isDelete){
			for(Contact con:trigger.old){
				accountIds.add(con.accountID);
			}
		}

		for(Account acc:[SELECT Id,Name,Number_Of_Contacts__c,(Select Id from Contacts) from Account where Id IN: accountIds AND Account.Recordtype.DeveloperName='Business_Organization_Mastered']){
			Account accObj = new Account ();
			accObj.Id = acc.Id;
			accObj.Number_Of_Contacts__c= acc.Contacts.size();
			lstAccountsToUpdate.add(accObj);
		}

		update lstAccountsToUpdate;
	}
}

 
Sainath VenkatSainath Venkat
Thanks a lot for the help and it's work perfectly now
Sainath VenkatSainath Venkat
Hello Newbie,
the handler works perfectly but it's not working on updation, means lets say I have two contacts with unique email id's then Checkbox__c on account will be checked and lets say if I edit one of the contact and give email id as same as other contact then Checkbox__c should get unchecked but its not working.
on update its not working.can you help me for the same please if possible
Ajay K DubediAjay K Dubedi
Hi Sainath,
Try this code.

trigger Check_Account on Contact (before insert,before update) {
    list<contact> conlist=new list<contact>();
    conlist=[select id,email,accountid from contact where email!=null];
    set<id> Accid=new set<id>();
    for(contact con:conlist){
        for(contact c:trigger.new){
            if(con.AccountId==c.Accountid){
                if(con.email==c.email){
                   Accid.add(c.Accountid);
                }
            }
        }
    }
    list<Account> acclist=new list<Account>();
    acclist=[select id,checked__c from account where id in:Accid];
    for(Account a:acclist){
        a.checked__c=false;
    }
    update acclist;
    
}


Hope It will help you.

Thanks
Ajay Dubedi