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
Gar SpencerGar Spencer 

Need Help with Apex

Hey can someone help me with a class and trigger?

I need to create a trigger that when record 1 populates fields a, b, c ,d on an account
it will search through all existing accounts and search for fields d,e,f,g that matche record 1's fields a,b,c,d.
then if records are found would populate a list of matching account names (as a hyperlink) to record 1.
and send an email notification to owners of records that were matched to record 1's criteria with the account name of the record 1
Suneel#8Suneel#8
You want to compare values in fields-a,b,c,d with that of d,e,f,g.Why is it so?Are you trying to get the duplicate accounts?
Gar SpencerGar Spencer
No I have accounts for buyers and accounts for sellers..... record 1 is a buyer account.... they have criteria they are looking for fields a,b,c,d and i want to search the org for a seller account that has field d,e,f,g that match the record 1..... and then  I want to create a triger that notifies the owner of the sellers record via email  with the buyer Account link in the email.
 
Suneel#8Suneel#8
Below is the skeleton of the code you can use.Below is the code to identify duplicate accounts with same Name and City.Modify the SOQL and nameSet,citySet collections to compare a,b,c,d fields with f,g,h,i
if((trigger.isAfter && trigger.isInsert)||(trigger.isAfter && trigger.isUpdate)){
        List<Account> buyerList=new List<Account>();
        Set<String> nameSet=new Set<String>();
        Set<String> citySet=new Set<String>();
        Set<String> emailSet;
        for(Account a:trigger.new){
            if(a.recordtypeid=='BUYER_RECORD_TYPE_ID'){
                buyerList.add(a);
                nameSet.add(a.name);
                citySet.add(a.Billingcity);
            }
        }
        if(buyerList.size()>0 && nameSet.size()>0 && citySet.size()>0){
            List<Account> listOfAccounts=[select id,name,BillingCity,owner.email from account where (name in :nameSet or Billingcity in :citySet) and recordtypeid='SELLER_REORD_TYPE_ID'];
            Map<Id,Set<String>> mapOfAccounts=new Map<Id,Set<String>>();
            for(Account a:listOfAccounts){
                for(account trgrAccount:buyerList){
                    if(trgrAccount.name==a.name && trgraccount.Billingcity==a.Billingcity){
                        if(mapOfAccounts.containsKey(trgrAccount.id) && !mapOfAccounts.get(trgrAccount.id).contains(a.owner.email)){
                            emailSet=mapOfAccounts.remove(trgrAccount.id);
                            emailSet.add(a.owner.email);
                            mapOfaccounts.put(trgrAccount.id,emailSet);
                        }else {
                            emailSet=new Set<String>();
                            emailSet.add(a.owner.email);
                            mapOfaccounts.put(trgrAccount.id,emailSet);
                        }
                    }
                }
            }
            for(Id idey:mapOfaccounts.keySet()){                               
                String emailMessage='Buyer Email account-https://na7.salesforce.com/'+ idey;
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                String[] toAddresses=new List<String>();
                toAddresses.addAll(mapOfaccounts.get(idey));
                mail.setToAddresses(toAddresses);
                mail.setReplyTo('noreply@salesforce.com');  
                mail.setSenderDisplayName('Buyer Email');  
                mail.setSubject('Subject');
                mail.setPlainTextBody(emailMessage);
                mail.setHtmlBody(emailMessage);
                Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail});                                
            }        
        }    
    }