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
Jyosi jyosiJyosi jyosi 

Bulkfying the trigger code

Hello Everyone.

I have created a trigger by using context varibale.Need to bulkfying my code.
Can you please help out.

trigger AccountTestTrigeer on Account (before insert, before update) {
    AccountTestTrigeerhandler handler = new AccountTestTrigeerhandler();

     if(Trigger.Isbefore && Trigger.isInsert)
    
     {
      handler.onDuplicateCheck(Trigger.New);
     }else if (Trigger.Isbefore && Trigger.Isupdate)
     {
      handler.onDuplicateCheck(Trigger.New);
     }
   
}

And here is my Trigger Handler class

public without sharing class AccountTestTrigeerhandler
{



public void onDuplicateCheck(List<Account> Acctlist)
{
 
  for(Account acterror:Acctlist)
  {
   List<Account> acc=[Select id from Account where Name=:acterror.Name ]; //Need to query this outside the for lOOP who to Acheive this please
   system.debug('AccountistVaues' +acc);
   system.debug('AccountSize@@@@@@@@@@@'+acc.size());
  if(acc.size()>0)
  {
   acterror.addError('You Cannot Create the Duplicate Account');
  }
 
}
 
 
}

}

Regards,
Jyo
Best Answer chosen by Jyosi jyosi
Sumitkumar_ShingaviSumitkumar_Shingavi
I corrected your code and here you go!
trigger AccountTestTrigeer on Account (before insert, before update) {
    AccountTestTrigeerhandler handler = new AccountTestTrigeerhandler();
    if(Trigger.Isbefore && Trigger.isInsert) {
		handler.onDuplicateCheck(Trigger.New);
    } else if (Trigger.Isbefore && Trigger.Isupdate) {
      handler.onDuplicateCheck(Trigger.New);
    }   
}


public without sharing class AccountTestTrigeerhandler {

	public void onDuplicateCheck(List<Account> Acctlist) {	
		
		Set<String> sAccountNames = new Set<String>();
		
		for(Account acc : Acctlist) sAccountNames.add(acc.Name);
		
		List<Account> lExistingAccounts = [SELECT Id, Name FROM Account WHERE Name in: sAccountNames];
		Map<String, Account> mExistingAccounts = new Map<String, Account>();
		for(Account acc : lExistingAccounts) mExistingAccounts.put(acc.Name, acc);
		
		for(Account acc : Acctlist) {
			if(mExistingAccounts.containsKey(acc.Name)) {
				acc.addError('You Cannot Create the Duplicate Account');
			}
		}
	}
}
PS: if this answers your question then hit Like and mark it as solution!

All Answers

Sumitkumar_ShingaviSumitkumar_Shingavi
I corrected your code and here you go!
trigger AccountTestTrigeer on Account (before insert, before update) {
    AccountTestTrigeerhandler handler = new AccountTestTrigeerhandler();
    if(Trigger.Isbefore && Trigger.isInsert) {
		handler.onDuplicateCheck(Trigger.New);
    } else if (Trigger.Isbefore && Trigger.Isupdate) {
      handler.onDuplicateCheck(Trigger.New);
    }   
}


public without sharing class AccountTestTrigeerhandler {

	public void onDuplicateCheck(List<Account> Acctlist) {	
		
		Set<String> sAccountNames = new Set<String>();
		
		for(Account acc : Acctlist) sAccountNames.add(acc.Name);
		
		List<Account> lExistingAccounts = [SELECT Id, Name FROM Account WHERE Name in: sAccountNames];
		Map<String, Account> mExistingAccounts = new Map<String, Account>();
		for(Account acc : lExistingAccounts) mExistingAccounts.put(acc.Name, acc);
		
		for(Account acc : Acctlist) {
			if(mExistingAccounts.containsKey(acc.Name)) {
				acc.addError('You Cannot Create the Duplicate Account');
			}
		}
	}
}
PS: if this answers your question then hit Like and mark it as solution!
This was selected as the best answer
Jyosi jyosiJyosi jyosi
Hello Sumitkumar,

Thanks a lot,Need some help for bettering undertstanding when to use sets ,List and Maps,
Is there any Link/document with you.
That would be great help .

Regards,
jyo