• Lokesh Patil
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 5
    Questions
  • 1
    Replies
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;
  }

}
Is code right

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;
    }

}
 
Can someone help with following case of Trigger:
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.
- Code should be able to handle bulk data; meaning multiple accounts can be updated using Import Wizard or Data loader
Create a custom number field on Account and name it ‘Total Campaign Cost’
Create a lookup relationship on Campaign as Account.
Use Campaign ‘Actual Cost’ field for operations.
Each time a campaign status is marked as ‘Completed’ add the amount into the ‘Total Campaign Cost’ field of account.
Update Account Status:
If Total Campaign Cost on Account is greater than $10,000 : Platinum
If Total Campaign Cost on Account is > $8,000 & <$10,000 : Gold
If Total Campaign Cost on Account is <$8,000: Silver



Also, create an apex test class to ensure developed code can be deployed; adhere to best practices of the testing framework.
Trigger to
Create a custom number field on Account and name it ‘Total Campaign Cost’
Create a lookup relationship on Campaign as Account.
Use Campaign ‘Actual Cost’ field for operations.
Each time a campaign status is marked as ‘Completed’ add the amount into the ‘Total Campaign Cost’ field of account.
Update Account Status:
If Total Campaign Cost on Account is greater than $10,000 : Platinum
If Total Campaign Cost on Account is > $8,000 & <$10,000 : Gold
If Total Campaign Cost on Account is <$8,000: Silver
- Code should be able to handle bulk data; meaning multiple campaigns can be added/updated with status as ‘Completed’ using Import Wizard or Data loader
- Code should adhere to programming best practices like
- Standardized Naming conventions
- Apt utilization of collection elements
- Follow of Trigger pattern
- Avoiding Trigger recurrence on same object
- Proper Code comments
- On keeping Salesforce Governor limits into consideration
- Also, create an apex test class to ensure developed code can be deployed; adhere to best practices of the testing framework.
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;
  }

}