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