+ Start a Discussion
Lokesh PatilLokesh Patil 

Please Tell me where i am wrong

Error on this Trigger:This Error
where i am wrong
Requirement Details:
- Account should have two fields of type text area Current Issues(Products), Past Issues(Products)
- Keep count of total issues/Cases at account level, whenever the count reaches 5, send an email to the Account owner to get in touch with the service team.
- When a case is created the total issue count should increase and the Product for which issue is raised should be shown in comma separated format in field Current Issues (Products).
- When a case is closed the product should be removed from current Issues(Products) and should be added to Past Issues(Products) in comma separated format.


Trigger UpdateCaseCount on Case(After Insert, After Update) {
    Set<id> AccIdUpd = new Set<Id>();
  Map<Id, Id> AccPrd = new Map<Id, Id>();
    set<id> ContactId = new set<id>();
 
  //Create a master list to hold the emails we'll send
  List<Messaging.SingleEmailMessage> mails =
  new List<Messaging.SingleEmailMessage>();
 
   
  for(Case cs:Trigger.New) {       
    if(Trigger.IsInsert || (cs.IsClosed && Trigger.OldMap.get(cs.Id).IsClosed != cs.IsClosed)) {
      AccIdUpd.add(cs.AccountId);
      AccPrd.put(cs.AccountId, cs.Product__c);
    }
 
    }     

  List<AggregateResult> ARListCurrIssue = [select AccountId, Product__c, Count(Id) CaseCount from Case where
                                             AccountId In:AccIdUpd and IsClosed != True group By AccountId, Product__c];
 
  // Logic to update curernt Issues
  List<Account> UpdAccList = new List<Account>();
  for(AggregateResult AR : ARListCurrIssue) {
  String CurrCount = AR.get('CaseCount') + ', ' +  AccPrd.get((ID)AR.get('Product__c'));
        UpdAccList.add(new Account(Id = (ID)AR.get('AccountId'), CurrentCount__c = CurrCount));
        Integer iCount = Integer.valueOf(AR.get('CaseCount'));
        If(iCount == 5) {
            //write logic to sendMail     
 
         Contact con = [Select firstname,lastname,email,id,name,MobilePhone from Contact where id in :ContactId];
      // Step 1: Create a new Email
      Messaging.SingleEmailMessage mail =  new Messaging.SingleEmailMessage();
  
      // Step 2: Set list of people who should get the email
      List<String> sendTo = new List<String>();
      sendTo.add(con.email);
      mail.setToAddresses(sendTo);
  
      // Step 3: Set who the email is sent from
      mail.setReplyTo('lokeshpatil830@gmail.com');
      mail.setSenderDisplayName('lokesh patil');
  
      // (Optional) Set list of people who should be CC'ed
      List<String> ccTo = new List<String>();
      ccTo.add('puja.patil@aress.com');
      mail.setCcAddresses(ccTo);
 
      // Step 4. Set email contents - you can use variables
      mail.setSubject('Get in touch with the service team');
      String body = 'Dear ' + con.FirstName;
    
      mail.setHtmlBody(body);
  
      // Step 5. Add your email to the master list
      mails.add(mail);
        Messaging.sendEmail(mails);             
   
  }


    if(UpdAccList.size()>0){
    update UpdAccList;

}
  // Logic to update Past Issues
      ARListCurrIssue = [select AccountId, Count(Id) CaseCount  from Case where AccountId In:
                                             AccIdUpd and IsClosed = True group By AccountId];
 
  UpdAccList = new List<Account>();
  for(AggregateResult AR : ARListCurrIssue) {
    String PastCount = AR.get('CaseCount') + ', ' +  AccPrd.get((ID)AR.get('AccountId'));
    UpdAccList.add(new Account(Id = (ID)AR.get('AccountId'), PastCount__c = PastCount));
   
  }
   
  if(UpdAccList.size()>0) {
    update UpdAccList;
  }

}
Tintu_BabuTintu_Babu
Hi,
In this Line ,
String CurrCount = AR.get('CaseCount') + ', ' +  AccPrd.get((ID)AR.get('Product__c'));
You are trying to access coount as String actually its a number . So get it like (Integer)AR.get('CaseCount')
By just appending that with 
(Integer)AR.get('CaseCount') +''; will make it String so just Access its as Integer and make it string 
Lokesh PatilLokesh Patil
Not Working
Expression cannot be a statement.
Variable does not exist: CurrCount