-1

can you please help me to write this code in very standard way.

scenarios:

I have object Inquiry. It contains 3 accounts. they are primary Inquiry account,secondary Inquiry Account and third inquiry Account

In Account level,I have global key account (checkbox) and Global Key Account Responsibility(lookup to user).

Problem:

Email needs to be sent to Global Key Account Responsibility user if Global Key Account is checked for Account

If it is checked on all 3 inquiry Accounts, email should be sent to all 3 Global Key Account Responsibility users

if same User is specified on Primary, Secondary and Third Inquiry company, only one email should be sent

We also need to ensure there is no duplicate email sent

So I had written trigger for that :

 public class s360_InquiryUpdateDetails {
  public static void inquiryInformation(List<Opportunity> opList){
    set<id> primaryAccount=new set<id>();
    set<id> secondaryAccount=new set<id>();
    set<id> thirdAccount=new set<id>();
    for(Opportunity op:opList){
        //Primary Account
        if(op.AccountId!=null){
            primaryAccount.add(op.AccountId);
        }
        //secondary
        if(op.Secondary_Inquiring_Company__c!=null){
            secondaryAccount.add(op.Secondary_Inquiring_Company__c);
        }
        //Third
        if(op.Third_Inquiring_Company__c!=null){
            thirdAccount.add(op.Third_Inquiring_Company__c);
        }
    }// for loop end
    map<id,id> primaryAccountMap=new map<id,id>();
    map<id,id> secondaryAccountMap=new map<id,id>();
    map<id,id>thirdAccountMap=new map<id,id>();

    for(Account ac:[select id,Key_Account__c,Key_Account_Responsibility__c from Account where id in:primaryAccount]){
        if(ac.Key_Account__c==true){
            primaryAccountMap.put(ac.id,ac.Key_Account_Responsibility__c);
        }

    }

    for(Account ac:[select id,Key_Account__c,Key_Account_Responsibility__c from Account where id in:secondaryAccount]){
        if(ac.Key_Account__c==true){
        secondaryAccountMap.put(ac.id,ac.Key_Account_Responsibility__c);
        }
    }
    for(Account ac:[select id,Key_Account__c,Key_Account_Responsibility__c from Account where id in:thirdAccount]){
         if(ac.Key_Account__c==true){
        thirdAccountMap.put(ac.id,ac.Key_Account_Responsibility__c);
         }
    }//map concept ends here

    map<id,string> primaryUserMap=new map<id,string>();
    map<id,string> secondUserMap=new map<id,string>();
    map<id,string> thirdUserMap=new map<id,string>();

    List<User>primaryUserList=[select id,name,email from user where id in:primaryAccountMap.values()];

    for(User u:primaryUserList){
        primaryUserMap.put(u.id,u.Email);
    }

    List<User>secondaryUserList=[select id,name,email from user where id in:secondaryAccountMap.values()];
    for(User sec:primaryUserList){
        secondUserMap.put(sec.id,sec.Email);
    }

    List<User>thirdUserList=[select id,name,email from user where id in:thirdAccountMap.values()];
    for(User third:primaryUserList){
        thirdUserMap.put(third.id,third.email);
    }

    List<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();
    for(Opportunity op:opList){

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        List<String> sendTo = new List<String>();
        //sendTo.add(.Email);
        mail.setToAddresses(sendTo);
        mail.setReplyTo('[email protected]');
        mail.setSenderDisplayName('Test Mail');
        mail.setSubject('shawocr');
        String body = 'Test Mail';
        mail.setHtmlBody(body);
        mails.add(mail);
  }
    Messaging.sendEmail(mails);
    }

}

Problem Facing: Most of the time in this code,I have to go for if-else condition. Anyone please help give me idea to achieve this code in very simple way.Whats the best way to achieve this requirement.

Iam thinking about following scenarios:

1.If 3 users are same,I will send one email

2.If 2 users are same,one mail for those two users and different mail for remaining users

3.If 3 users are different,so I have to send 3 mails for users

4
  • quick win would be to add Key_Account__c=true in your SOQL rather than in your Apex code. That being said.. You don't need 3 querries... to get same Account data. Jun 25, 2018 at 10:43
  • hi..itsnot same account data.it may different account Jun 25, 2018 at 10:57
  • I meant its same object Account... no need for 3 querries Jun 25, 2018 at 10:59
  • which one you are talking about.how can I make it in one query, Ya its same object but account ids are different Jun 25, 2018 at 11:00

1 Answer 1

0

There are few things which are bit not right in your code

  1. You are doing if check, if(ac.key_Account__c==true) , This can easily be moved in SOQL. SOQL filter are faster, and doesnt not contribute to CPU execution time
  2. As you are querrying only Account, you dont need 3 querries. You can combine them into one. Imaging having 100 list, will you do 100 querries?
  3. You are sending emails to the user. You dont need to know their email id. Just set setTargetId as the userId to whom you want to send it. It wont be counted in daily 5000 email limits

            public class s360_InquiryUpdateDetails {
      public static void inquiryInformation(List<Opportunity> opList){
        set<id> primaryAccount=new set<id>();
        set<id> secondaryAccount=new set<id>();
        set<id> thirdAccount=new set<id>();
        for(Opportunity op:opList){
            //Primary Account
            if(op.AccountId!=null){
                primaryAccount.add(op.AccountId);
            }
            //secondary
            if(op.Secondary_Inquiring_Company__c!=null){
                secondaryAccount.add(op.Secondary_Inquiring_Company__c);
            }
            //Third
            if(op.Third_Inquiring_Company__c!=null){
                thirdAccount.add(op.Third_Inquiring_Company__c);
            }
        }// for loop end
        map<id,id> primaryAccountMap=new map<id,id>();
        map<id,id> secondaryAccountMap=new map<id,id>();
        map<id,id>thirdAccountMap=new map<id,id>();
    
        for(Account ac:[SELECT id,Key_Account__c,Key_Account_Responsibility__c from Account where (id in:primaryAccount OR
                        id in:secondaryAccount OR
                        id in:thirdAccount)
                        and Key_Account__c=true]){
    
            if(primaryAccount.contains(ac.Id)){
                primaryAccountMap.put(ac.id,ac.Key_Account_Responsibility__c);
            }else if(secondaryAccount.contains(ac.Id){
                secondaryAccountMap.put(ac.id,ac.Key_Account_Responsibility__c);
            }else if(thirdAccount.contains(ac.Id){
                 thirdAccountMap.put(ac.id,ac.Key_Account_Responsibility__c);
            }
    
    
    
        }
    
    
        List<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();
        for(Opportunity op:opList){
    
    
        if(primaryAccountMap.containsKey(op.AccountId)){
            mails.add(createEmailMessage(primaryAccountMap.get(op.AccountId)));
    
        }
        if(secondaryAccountMap.containsKey(op.AccountId) && !primaryAccountMap.containsKey(op.AccountId)){
            mails.add(createEmailMessage(secondaryAccountMap.get(op.Secondary_Inquiring_Company__c)));
    
        }
        if(thirdAccountMap.containsKey(op.AccountId) && !primaryAccountMap.containsKey(op.AccountId) && !secondaryAccountMap.containsKey(op.AccountId)){
            mails.add(createEmailMessage(thirdAccountMap.get(op.Third_Inquiring_Company__c)));
    
        }
    
        mails.add(mail);
      }
        Messaging.sendEmail(mails);
    }
    
        private static createEmailMessage(String userId){
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setTargetId(userId);
            mail.setReplyTo('[email protected]');
            mail.setSenderDisplayName('Test Mail');
            mail.setSubject('shawocr');
            String body = 'Test Mail';
            mail.setHtmlBody(body);
            return mail;
        }
    
    }
    
4
  • Thanks.How its works in case of If 3 users are same,I have to send only one email Jun 25, 2018 at 11:20
  • Updated code, for the same. You can always add extra if else clause to accomplish it Jun 25, 2018 at 11:25
  • Hi You did wrong in these line for example.i have primary account and secondary account. Primary account has its key right so it will not enter here:secondaryAccountMap.containsKey(op.Secondary_Inquiring_Company__c) && !primaryAccountMap.containsKey(op.AccountId) Jun 26, 2018 at 14:56
  • The example I posted was for representation purpose of logic, I didnt write this in an editor and can have copypaste /typo errors. Jun 26, 2018 at 16:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .