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
Aakash Saluja 7Aakash Saluja 7 

trigger to create multiple records based on the single field update

Hi,

i am new to salesforce and i have a scenario where i have an object Search_Filter__c where i want to insert records based on the account.
if any account has 10 address it should create multiple records with the unique city value populated in that in the Search_Filter__c. like if there are two cties with the same name, it should create 1 record else two

i have written the code as:

trigger RcordInsert on Account (after insert, after update) {

map<id,account> mapAcc= new map<id,account>();
set<string> cities = new set<string>();

for(account acc: trigger.new){
    if(acc.KOL_Set_Search_flag__c != null){
        mapAcc.put(acc.id,acc);
        system.debug('id is'+mapAcc);
    }
}

list<account> listSearch = new list<account>([ SELECT name,
                                                      KOL_Set_Search_flag__c,
                                                      Middle_vod__c,
                                                      LastName,
                                                      (select City_vod__c,State_vod__c,Country_olr__c,Zip_vod__c from Address_vod__r )
                                               FROM account 
                                               WHERE id in : mapAcc.keyset()
                                            ]);
                                               
        list<Search_Filter__c> searchUpdateList  = new list<GBI_Search_Filter__c>();                                                                      
     for(account c : listSearch ){
      
  if(c.KOL_Set_Search_flag__c == true){
            KOL_GBI_Search_Filter__c searchFilter = new KOL_GBI_Search_Filter__c();
                searchFilter.KOL_FIRSTNAME__c   =  c.name;
                searchFilter.KOL_ID__c = c.id;
                searchFilter.KOL_MIDDLENAME__c  =  c.Middle_vod__c;
                searchFilter.KOL_LASTNAME__c    =  c.LastName;
               // For(Integer I = 0; I < c.Address_vod__r.size(); I++){
                for(Address_vod__c addr : c.Address_vod__r ){
                   
                searchFilter.KOL_CITY__c   =  addr.City_vod__c;
                searchFilter.KOL_STATE__c   =  addr.State_vod__c;
                searchFilter.KOL_POSTAL_ZIP_CODE__c   =  addr.Zip_vod__c;
                searchFilter.KOL_COUNTRY__c   =  addr.Country_olr__c;
                
               }
               
                searchUpdateList.add(searchFilter);
        }
        
     }     
     
     insert searchUpdateList;                                                                    
}

Thanks in advance
Best Answer chosen by Aakash Saluja 7
<Saket><Saket>
Hi Please find your Solution Below and let me know if u want any changes.
trigger RcordInsert on Account (after insert, after update) {
    
    list<Search_Filter__c> searchUpdateList  = new list<GBI_Search_Filter__c>();
    for(Account acc : [ SELECT name, KOL_Set_Search_flag__c, Middle_vod__c,LastName,(select City_vod__c,State_vod__c,Country_olr__c,Zip_vod__c from Address_vod__r ) FROM account WHERE id in : Trigger.newMap.keyset()]){
        
        Set<String> checkUnique = new Set<String>();
        for(Address_vod__r addr : acc.Address_vod__r ){
            
            
            if(!checkUnique.contains(addr.City_vod__c)){
                Search_Filter__c search = new Search_Filter__c();       
                //add field data accordingly.
                searchUpdateList.add(search);
            }else checkUnique.add(addr.City_Vod__c);
            
        }
        
    }
    if(searchUpdateList.size()>0)
        insert searchUpdateList;
}


All Answers

<Saket><Saket>
Hi, Can you please more elaborate your problem? Where are you getting stuck?
Aakash Saluja 7Aakash Saluja 7
Currently its just creating 1 record only, i wanted to create as many records are there in the address relkated list of the account but should be unique.
Example ,if there are three address with the cities as mumbai, Delhi and mumbai, then it should create only two records 1 for each mumbai and delhi
<Saket><Saket>
Hi Please find your Solution Below and let me know if u want any changes.
trigger RcordInsert on Account (after insert, after update) {
    
    list<Search_Filter__c> searchUpdateList  = new list<GBI_Search_Filter__c>();
    for(Account acc : [ SELECT name, KOL_Set_Search_flag__c, Middle_vod__c,LastName,(select City_vod__c,State_vod__c,Country_olr__c,Zip_vod__c from Address_vod__r ) FROM account WHERE id in : Trigger.newMap.keyset()]){
        
        Set<String> checkUnique = new Set<String>();
        for(Address_vod__r addr : acc.Address_vod__r ){
            
            
            if(!checkUnique.contains(addr.City_vod__c)){
                Search_Filter__c search = new Search_Filter__c();       
                //add field data accordingly.
                searchUpdateList.add(search);
            }else checkUnique.add(addr.City_Vod__c);
            
        }
        
    }
    if(searchUpdateList.size()>0)
        insert searchUpdateList;
}


This was selected as the best answer
Aakash Saluja 7Aakash Saluja 7
Hi,

I have done the changes but currently i am getting case senstive values also in the set. is there any way we can remove that.

trigger RcordInsertAakash on Account (after insert, after update) {

map<id,account> mapAcc= new map<id,account>();
set<Address_vod__c> cities = new set<Address_vod__c>([select City_vod__c from Address_vod__c where id in: mapAcc.keyset() ]);
set<String> citiesSet = new set<String>();
List<String> citiesuni = new List<String>();
Boolean addFnd = false; 
for(account acc: trigger.new){
    if(acc.KOL_Set_Search_flag__c != null){
        mapAcc.put(acc.id,acc);
        system.debug('id is'+mapAcc);
    }
}

list<account> listSearch = new list<account>([ SELECT name,
                                                      firstname,
                                                      KOL_Set_Search_flag__c,
                                                      Middle_vod__c,
                                                      LastName,
                                                      (select City_vod__c,State_vod__c,Country_olr__c,Zip_vod__c from Address_vod__r )
                                               FROM account 
                                               WHERE id in : mapAcc.keyset()
                                            ]);
                                               
        list<Search_Filter__c> searchUpdateList  = new list<Search_Filter__c>();                                                                      
     for(account c : listSearch ){
      
  if(c.KOL_Set_Search_flag__c == true){
    
    for(Address_vod__c addr : c.Address_vod__r ){
     citiesSet.add(addr.City_vod__c);
     system.debug('citiesSet'+citiesSet);
    }
    List<String> listStrings = new List<String>(citiesSet);

   for(String st : listStrings){
    addFnd = false;   
   for(Address_vod__c addr : c.Address_vod__r ){
    //for(integer i=0; i < addr.size(); i++){
            
            if(st.Equals(addr.City_vod__c)){
                if(addFnd == false){
            addFnd = True;  
            Search_Filter__c searchFilter = new Search_Filter__c();
                searchFilter.KOL_FIRSTNAME__c   =  c.firstname;
                searchFilter.KOL_ID__c = c.id;
                searchFilter.KOL_MIDDLENAME__c  =  c.Middle_vod__c;
                searchFilter.KOL_LASTNAME__c    =  c.LastName;
               // For(Integer I = 0; I < c.Address_vod__r.size(); I++){
              //  for(Address_vod__c addr : c.Address_vod__r ){
                   
                searchFilter.KOL_CITY__c   =  st;
                searchFilter.KOL_STATE__c   =  addr.State_vod__c;
                searchFilter.KOL_POSTAL_ZIP_CODE__c   =  addr.Zip_vod__c;
                searchFilter.KOL_COUNTRY__c   =  addr.Country_olr__c;
                searchUpdateList.add(searchFilter);
                break;
              }
           }
           
           //}   
           
               
               
            }
        
     }
          }
   }
     
     insert searchUpdateList;                                                                    
}
<Saket><Saket>
Hi,
you can do is, just before adding City in Set, lowercase or Uppercase it so that it can be identified, I hope it will solve your problem.
Thanks 
<Saket><Saket>
Hi Aakash,

Can you please tell us here that how u solved the problem or if the problem is solved by above answers then please mark it as a best answer

Thanks 
Saket Sharma