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
badri nath 9badri nath 9 

trigger to chack duplicates i am using query not soql&List?

code below working not fine I am querying the records don't use soql&collections.please find that ,

trigger Trgr_duplicate_check on Account (before insert,before update) {

  //List<Account> actList=  [select id,name from Account];
   string str = 'select id ,Name from Account' ;
  
  for(Account actobj :Trigger.New){
    if(Trigger.isinsert || Trigger.isBefore){
     // for(Account actobj1 :actList){
      
       /*if(actobj1.Name == actobj.Name){
          actobj.Name.addError('Name already exits!!!');
        
        }*/
        if(str.contains(actobj.Name)){  // here require proper method to search strings for duplicates
          
                  actobj.Name.addError('Name already exits!!!');
   }
          // }
      }
   }
}
Thanks):-
HARSHIL U PARIKHHARSHIL U PARIKH
Hello Badri,

We need to take an account of three difference conditions such as Before Insert, Before Update, After UnDelete.

Below, trigger handles them.

Trigger Code:
 
Trigger DupeAccountCheck On Account(Before Insert, Before Update, After UnDelete){
    
    List<String> comingAccountsName          = New List<String>();
    List<Account> comingAccounts             = New List<Account>();
    List<Id>      comingAccountIds           = New List<Id>();
    
    If(Trigger.IsInsert || Trigger.IsUpdate || Trigger.IsUnDelete)
    {
        For(Account act : Trigger.New)
        {
            If(act.Name != Null)
            {
                comingAccountsName.add(act.Name); 
                comingAccounts.add(act);
                comingAccountIds.add(act.Id);
            }
        }
       
        
       List<Account> SimilarAccounts = [Select Id, Name FROM Account WHERE Name =: comingAccountsName
                                                                       AND Id  !=: comingAccountIds];
       
       If(SimilarAccounts.size() > 0)
       {
            For(Account EveryAccount : comingAccounts)
            {
                EveryAccount.addError('This Account Name Already Exists!');
            }
       }
       
       
    }
}

Hope it helps and if it solves the query then please mark it as Best Answer!
badri nath 9badri nath 9
Hi HARSHIL fine but not enough, please understand my requirement, i am  writing query for records in datsbase ,
i wanted to work with nearly 5MLN Records ., and names are stored in string variable and i wanted to check those,
#Please go through question once,