0

enter image description hereenter image description hereI'm trying to exclude certain accounts in order to exclude them in sending survey emails. COuld see that I have included account id, which is very hard coding. I know. But there are some more accounts which I want to exclude but not this way. I want to exclude them with their names. Coz they have pretty close names. So I felt it's better to exclude them by names. I would really appreciate your help. New bee in coding.

> public  static void Sendnotification(List<case> CaseLst,Map<id,case>
> OldMap){
>         set<id> contactidset=new set<id>();
>         set<id> RecordOwnerIDSet=new Set<id>();


    for(Case cs:CaseLst){
        if(cs.Status == 'Closed' ){
           if(**cs.AccountId != '00130000007Z0nL'** && cs.Age_days__c <= 30 && cs.Survey_Type__c == 'Agent' && cs.Problem_Type__c != 'Sales/Trial Request' && cs.Parentid == null ) {                
              contactidset.add(cs.contactid);
           }                              
         } 

       RecordOwnerIDSet.add(cs.ownerid);  
    }

     Map<id,User> UsrMap=new Map<id,User>();
    if(RecordOwnerIDSet!=null && RecordOwnerIDSet.size()>0){
        for(user usr:[select id,name, profileid,profile.name from user where id in:RecordOwnerIDSet]){
            UsrMap.put(usr.id,usr);
        }
    }


    List<Licensee__c> LicsnList=new List<Licensee__c>();
    if(contactidset!=null && contactidset.size()>0){
        LicsnList =[select id,User__c,User__r.email,   Support_Status__c, SN__r.Banned__c, SN__r.Block_Support__c, SN__r.Bad_Debt__c from Licensee__c where User__c in:contactidset];
    }
   // Set<string> ContactEmailIDSet= new Set<string>();
   Map<string,string> ContactLicsnMap=new Map<string,string>();
    if(LicsnList!=null && LicsnList.size()>0){
        for(Licensee__c LIC:LicsnList){
            //if(LIC.AMSEnd__c>system.today())
              if(LIC.No_Support__c == false && (LIC.User__r.Nb_Days_since_last_survey__c > 30 || LIC.User__r.Nb_Days_since_last_survey__c == 0) ) {
                //ContactEmailIDSet.add(User__r.email);
                ContactLicsnMap.put(LIC.User__c,LIC.User__r.email);

            }

    }
      List < Messaging.SingleEmailMessage > maillist = new List < Messaging.SingleEmailMessage > ();
     EmailTemplate et=[Select Id, Subject, HtmlValue, Body from EmailTemplate where DeveloperName=:'Customer_Satisfaction_05_29_14_43'];
    for(Case cs:CaseLst){
    if(cs.Status == 'Closed' && OldMap.get(cs.id).Status!='Closed' && UsrMap.containskey(cs.ownerid) && UsrMap.get(cs.ownerid).profile.name!=null &&  (UsrMap.get(cs.ownerid).profile.name =='Customer Care Manager' || UsrMap.get(cs.ownerid).profile.name =='Customer Care' || UsrMap.get(cs.ownerid).profile.name =='Customer Care Knowledge') && ContactLicsnMap.containskey(cs.contactid) && ContactLicsnMap.get(cs.contactid)!=null){            


        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
       mail.setTargetObjectId(cs.contactid);
        mail.setTemplateId(et.id);
       /*  String subject = et.Subject;
        String htmlBody = et.HtmlValue;
        htmlBody = htmlBody.replace('{!Contact.FirstName}', cs.contactid);
        htmlBody = htmlBody.replace('{!Case.Agent__c}', cs.agent__c);
        htmlBody = htmlBody.replace('{!Case.CaseNumber}', cs.CaseNumber);*/
        mail.setSenderDisplayName('iBwave Support');
       // mail.setSubject(subject);
        //mail.setHtmlBody(htmlBody);
        mail.setWhatId(cs.id);
        String[] toAddress = new String[] {ContactLicsnMap.get(cs.contactid)};
        mail.setToAddresses(toAddress);
        mail.setSaveAsActivity(false);
        maillist.add(mail);

     } 
   }
   if(maillist!=null && maillist.size()>0){
     Messaging.sendEmail(maillist);
   }

   }

}
}

1 Answer 1

2

Simply add a Checkbox field named something like Exclude_From_Surveys__c. Then you can just update your code to act based on the value:

if (!cs.Account.Exclude_From_Surveys__c && otherCriteria)
{
    // add to set, send email, etc.
}

In this way, you can make wether the email is sent to a particular Account or not a simple data loading exercise.


If you find the checkbox approach unpalatable, you could add a List Custom Setting to enumerate the Account Names you want to exclude. Then, you don't need to modify the Account data at all, but can still exclude them in a configurable way. You wouldn't even need to add any custom fields, simply use the Name on the Custom Setting to determine the Name to exclude.

Set<Id> excludedAccountIds = new Map<Id, Account>([
    SELECT Id FROM Account WHERE Name IN :AccountSurveyBlacklist__c.getAll().keySet()
]).keySet();

for (Case record : trigger.new)
{
    if (record.Status == 'Closed' && !excludedAccountIds.contains(record.AccountId) && ...)
    {
        // logic here
    }
}

Using this approach, if you wanted to blacklist all Account records whose Name is Ericsson or Ericsson US, you would create two AccountSurveyBlacklist__c records, one for each Name.

13
  • There are several accounts to exclude. It would be hard to have checkbox and checking them by going into each account record. There are some accounts which does have pretty close name. For example: Ericsson, Ericsson US. It would be easier to mention name of the account in the code (!cs.Account.Name.contains('Ericsson')
    – Gidy
    Nov 8, 2016 at 17:08
  • 1
    It might seem easier now, but then when you have to tweak the logic in a few months, not so much. It is very easy to set a checkbox on the records you want to exclude.
    – Adrian Larson
    Nov 8, 2016 at 17:12
  • I really appreciate your idea. But this idea, removing certain accounts, is not permanent. It's very temporary. We would like to include them in the code.
    – Gidy
    Nov 8, 2016 at 17:16
  • 1
    Using code is much more permanent than creating a data dependency. When you want to include them again, simply uncheck the box...it's just a data loading exercise at that point.
    – Adrian Larson
    Nov 8, 2016 at 17:16
  • 3
    @Gidy - Bad decision that will come back to bite you later. Good luck
    – Eric
    Nov 8, 2016 at 17:35

You must log in to answer this question.

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