+ Start a Discussion
Gar SpencerGar Spencer 

Can someone look at my code and tell me where I went wrong?

I have written a trigger that when an account record is inserted or updated and the Active_buyers__c field equals True, it will search the system for all accounts that have the field Active_seller__c = true and the the List price from the account that has the active seller eqquals true is less than or equal to the price range of the account with the active buyers equal true.

If a match is found it would then send an email notification to the lastmodifiedBy user of the active seller equals true record to notify them of a potential buyer.

I am getting errors with this code and it will not compile..... 

Can someone look at my code and tell me where I went wrong?


trigger BuyerSellerMatchTrigger on Account (after insert, after update) {
        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.Active_buyers__c='True'){
                buyerList.add(a);
                nameSet.add(a.name);
           
            }
        }
        if(buyerList.size()>0 && nameSet.size()>0){
            List<Account> listOfAccounts=[select id,name,BillingCity,owner.email,Price_range__c from account where (name in :nameSet) and Active_seller__c='True'];
            Map<Id,Set<String>> mapOfAccounts=new Map<Id,Set<String>>();
            for(Account a:listOfAccounts){
                for(account trgrAccount:buyerList){
                    if(trgrAccount.Active_Seller__c='True' && trgraccount.list_price__c=a.Price_range__c){
                        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.LastModifiedByid,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});                               
            }       
        }   
    }
KevinPKevinP
Gar,

Thanks for posting code!

What errors are you recieving? line numbers ? errors ?
Gar SpencerGar Spencer
I get an illegal boolean after string declaration on line 7 if(Active_Buyers__c = 'True)
SS KarthickSS Karthick
Hi Gar Spencer,
        Just remove the single quotes and do like
public boolean Active_Buyers{get;set;}
if(Active_Buyers__==true)

I hope this will helps you

Thanks
Karthick
Gar SpencerGar Spencer
thanks but I have made changes and it is still not compiling or saving to SFDC.....  here is my modified code after debugging.  Can anyone see what I am missing.

First thing that should happen is when a new buyer record is saved or updated (identified by the Active_Buyers__c) field is search through system for records that have (Active_Seller__c) field checked.

Then it should query the account records and pull the ID, name, List Price, Price Range, Owner Email, and Last Modified Email. from the account object where the List Price <= to the Price range  and store the results temporarily.

After matches are returned there should be an email notification sent to the LastModifiedByID.email of the record that has the Active Seller field checked. with the

Subject being New Buyer Found, 
Body should contain the account name (hyperlink) owner name and owner email of the account with the Active buyers field checked




trigger BuyerSellerMatchTrigger on Account (after insert,after update){
// Capture all records that have Active_Buyers__c field equals true
      List <Account> BuyerList = new List<Account>();
      Set<String> nameSet= new Set<String>();
      Set<String> emailSet;
      Boolean Active_Buyers = true;
         for(Account a:trigger.new){      
           if(Active_Buyers__c) {
            buyerList.add(a.);
            nameSet.add(a.name);
         }
        }
//check to determine size and the query fields of accounts for the buyer list
        if(buyerList.size()>0 && nameSet.size()>0){
       List<Account>listOfAccounts=[ select id, name, owner.email, Maximun_Price_Range__c, Price_range__c 
           from Account where (name in: nameSet) and Active_Seller__c = True and Listed_Price__c <= Price_Range__c];
//Map the result set of active buyers to active sellers
         Map<Id, Set<String>>mapOfAccounts=new Map<Id, Set<String>>();
         for(Account a:listOfAccounts){
            for(account trgrAccountbuyerList){
               if(trgrAccount.Active_Seller__c = True){
                 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.LastModifiedByid,emailSet);
                        }
                    }
                }
            }
// Email notification trigger
            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});                               
            }       
        }   
}
}
Gar SpencerGar Spencer
I have corrected my code but still am getting errors on the email notification part of the trigger.... error line 49 variable mapOfAccounts doesn't exist

Heres my code;

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

// Capture all records that have Active_Buyers__c field equals true
      List <Account> BuyerList = new List<Account>();
      Set<String> nameSet= new Set<String>();
      Set<String> emailSet;
      Boolean Active_Buyers = true;
         for(Account a : System.trigger.new){
           
           if(a.Active_Buyers__c != False) {
            BuyerList.add(a);
            nameSet.add(a.name);

         }
        }
 
//check to determine size and the query fields of accounts for the buyer list
        if(buyerList.size()>0 && nameSet.size()>0){
         List<Account>listOfAccounts=[ select id, name, owner.email, LastModifiedBy.email, Active_Seller__c, Max_Price_Range__c, Listed_Price__c from Account Where Active_Seller__c = True ];
           
//Map the result set of active buyers to active sellers
         Map<Id, Set<String>>mapOfAccounts=new Map<Id, Set<String>>();
         for(Account a:listOfAccounts){
             for(account trgrAccount:buyerList){
                 if(trgrAccount.Active_Seller__c = True){
                     if(mapOfAccounts.containsKey(trgrAccount.id) &&
                        !mapOfAccounts.get(trgrAccount.id).contains(a.LastModifiedBy.email)){
                            emailSet=mapOfAccounts.remove(trgrAccount.LastModifiedBy.email);
                            emailSet.add(a.LastModifiedBy.email);
                            mapOfAccounts.put(trgrAccount.id,emailSet);
                        }
                 }
                 

                       else {
                            emailSet=new Set<String>();
                            emailSet.add(a.owner.email);
                            mapOfAccounts.put(trgrAccount.LastModifiedByid,emailSet);
                        }
                    }
                }
            }
           
// Email notification trigger
            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});                               
            }       
        }   
kiranmutturukiranmutturu
you initilized the map inside an if which is local to that block and trying to use out side

 so if you want to refer that outside of the definition you need to get the declaration out .. Take the line 22 and put that before line 18
Gar SpencerGar Spencer
thanks that took care of it