function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
srinivasasarmahere1.390217033575374E12srinivasasarmahere1.390217033575374E12 

Help with Trigger

The trigger looks at the Ticket__c(Custom Object) record whose status (GLOBAL_Ticket_Status__c == 'closed')  and updates case($Object) Status(Field) to closed
 
Ticket__c has a lookup to Case, so case can have multiple tickets associated to it but a ticket can only be associated to a case. 

Trigger Ticket on Ticket__c (after insert, after update) {
    Set<Id> closedTicketIds = new Set<Id>();
            for (Ticket__c ticket: Trigger.new) {
            if (ticket.GLOBAL_Ticket_Status__c == 'closed' && ) {
            closedTicketIds.add(ticket.id);
        } 
    }
    List<Ticket__c> ticketForClosing= [SELECT Id, GLOBAL_Goal__c FROM Ticket__c
                                         WHERE Id IN: closedTicketIds];

    set<Id> caseIDs = new set<Id>();
        for(Ticket__c t : ticketForClosing){
        caseIDs.add(t.GLOBAL_Goal__c);
    }
    List<Case> casesToUpdate = new List<Case>();
        for (Case c : [Select Id, Status From Case Where Id In : caseIDs]) {
        c.Status = 'closed';
        casesToUpdate.add(c);
    }
   
    update casesToUpdate;
}

1. The  issue is trigger does not look at all the records of ticket (Requirement is to look at all the records of ticket and only if the status(GLOBAL_Ticket_Status__c == 'closed') then go and close the Case) ,Currently trigger closes a case if either of the tickets sociated to the case are closed but not all.

2. Change the status on case 7 days after all the tickets associated to that case are closed(this allows the business to add tickets to the same case)


Can anyone please help me out on this

Thanks

Best Answer chosen by srinivasasarmahere1.390217033575374E12
Anurag JainAnurag Jain
here some code logic sample which may helps you

  Set<ID> caseIDs = new Set<ID>();
  Map<ID,List<Ticket__c>> CaseID_TicketList = new Map<ID,List<Ticket__c>>();

        for (Ticket__c ticket: Trigger.new) {
            if (ticket.GLOBAL_Ticket_Status__c == 'closed' && ) {
                   caseIDs.add(ticket.GLOBAL_Goal__c);   // case ID of current Records.
            }
    
       for(Ticket__c ticket : [Select ID, GLOBAL_Ticket_Status__c,GLOBAL_Goal__c from Ticket__c where GLOBAL_Goal__c in :caseIDs]){
             if(CaseID_TicketList.containsKey(ticket.GLOBAL_Goal__c)){
                        List<Ticket__c> tkList = CaseID_TicketList.get(ticket.GLOBAL_Goal__c);
                        tkList.add(ticket);   
             }
            else{
                        List<Ticket__c> tkList = new List<Ticket__c>();
                        tkList.add(ticket);
                        CaseID_TicketList.put(ticket.GLOBAL_Goal__c,tkList);
             }
       }
   
  //  Now you have all tickets for 1 case in one Map record.

Set<ID> updateCaseID = new Set<ID>();

          for(Integer i = 0; i <= caseIDs.size() ; i++){
                   ID caseID = caseIDs[0];
                   Set<String> allClose = new Set<String>();
                   Integer TotalTicketforOneCase = CaseID_TicketList.get(caseID).size();
                   Integer ticketCounter = 0;
                   for(Ticket__c tickt : CaseID_TicketList.get(caseID)){ 
                              if(tickt.GLOBAL_Ticket_Status__c == 'Closed'){
                                       allClose.add('true');
                              }
                             else{
                                       allClose.add('true');
                               }
                     }
             // if allClose contains only one elt that means all Tickets status is closed.
                   if(allClose.size() == 1){  
                              updateCaseID.add(caseID);  // if all ticket is closed then add Case ID.
                    }  
}

now SOQL case and update its value.

hope now it helps you... :)

All Answers

Anurag JainAnurag Jain
You need to close Case only if its all tickets are closed???
Anurag JainAnurag Jain
for your 2nd requirment you can you the Apex Scheduler class which runs daily and check if all Tickets of case is Closed and last modified date is greater then 7 days then close that Case.
srinivasasarmahere1.390217033575374E12srinivasasarmahere1.390217033575374E12
Hi Anurag Jai,

Thank you fornthe reply. Yes the Case should only be closed if all the Tickets associated to that case are closed
Anurag JainAnurag Jain
Ok, this issue you can solved by using "MAPs".
  1. Create a Map as CaseID_TicketList like Map<ID,List<Ticket__c>> CaseID_TicketList,
  2. now put Case ID as Key and all associated Tickets as ListValue,
  3. take one record of Map and check if its all tickets Status it closed then update Case.
hope it helps..
srinivasasarmahere1.390217033575374E12srinivasasarmahere1.390217033575374E12
Can you please help me in writing the code?

Thanks
Anurag JainAnurag Jain
here some code logic sample which may helps you

  Set<ID> caseIDs = new Set<ID>();
  Map<ID,List<Ticket__c>> CaseID_TicketList = new Map<ID,List<Ticket__c>>();

        for (Ticket__c ticket: Trigger.new) {
            if (ticket.GLOBAL_Ticket_Status__c == 'closed' && ) {
                   caseIDs.add(ticket.GLOBAL_Goal__c);   // case ID of current Records.
            }
    
       for(Ticket__c ticket : [Select ID, GLOBAL_Ticket_Status__c,GLOBAL_Goal__c from Ticket__c where GLOBAL_Goal__c in :caseIDs]){
             if(CaseID_TicketList.containsKey(ticket.GLOBAL_Goal__c)){
                        List<Ticket__c> tkList = CaseID_TicketList.get(ticket.GLOBAL_Goal__c);
                        tkList.add(ticket);   
             }
            else{
                        List<Ticket__c> tkList = new List<Ticket__c>();
                        tkList.add(ticket);
                        CaseID_TicketList.put(ticket.GLOBAL_Goal__c,tkList);
             }
       }
   
  //  Now you have all tickets for 1 case in one Map record.

Set<ID> updateCaseID = new Set<ID>();

          for(Integer i = 0; i <= caseIDs.size() ; i++){
                   ID caseID = caseIDs[0];
                   Set<String> allClose = new Set<String>();
                   Integer TotalTicketforOneCase = CaseID_TicketList.get(caseID).size();
                   Integer ticketCounter = 0;
                   for(Ticket__c tickt : CaseID_TicketList.get(caseID)){ 
                              if(tickt.GLOBAL_Ticket_Status__c == 'Closed'){
                                       allClose.add('true');
                              }
                             else{
                                       allClose.add('true');
                               }
                     }
             // if allClose contains only one elt that means all Tickets status is closed.
                   if(allClose.size() == 1){  
                              updateCaseID.add(caseID);  // if all ticket is closed then add Case ID.
                    }  
}

now SOQL case and update its value.

hope now it helps you... :)
This was selected as the best answer
srinivasasarmahere1.390217033575374E12srinivasasarmahere1.390217033575374E12
Trigger Ticket on Ticket__c (after insert, after update) {

Set<ID> caseIDs = new Set<ID>();
  Map<ID,List<Ticket__c>> CaseID_TicketList = new Map<ID,List<Ticket__c>>();

        for (Ticket__c ticket: Trigger.new) {
            if (ticket.GLOBAL_Ticket_Status__c == 'closed') {
                   caseIDs.add(ticket.GLOBAL_Goal__c);   // case ID of current Records.
            }
   
       for(Ticket__c tckt : [Select ID, GLOBAL_Ticket_Status__c,GLOBAL_Goal__c from Ticket__c where GLOBAL_Goal__c in :caseIDs]){
             if(CaseID_TicketList.containsKey(ticket.GLOBAL_Goal__c)){
                        List<Ticket__c> tkList = CaseID_TicketList.get(ticket.GLOBAL_Goal__c);
                        tkList.add(tckt);  
             }
            else{
                        List<Ticket__c> tkList = new List<Ticket__c>();
                        tkList.add(tckt);
                        CaseID_TicketList.put(ticket.GLOBAL_Goal__c,tkList);
             }
       }
  


Set<ID> updateCaseID = new Set<ID>();

          for(Integer i = 0; i <= caseIDs.size() ; i++){
                   ID caseID = caseIDs[0];   Line 28
                   Set<String> allClose = new Set<String>();
                   Integer TotalTicketforOneCase = CaseID_TicketList.get(caseID).size();
                   Integer ticketCounter = 0;
                   for(Ticket__c tickt : CaseID_TicketList.get(caseID)){
                              if(tickt.GLOBAL_Ticket_Status__c == 'Closed'){
                                       allClose.add('true');
                              }
                             else{
                                       allClose.add('true');
                               }
                     }
            
                   if(allClose.size() == 1){ 
                              updateCaseID.add(caseID); 
                    } 
}

    List<Case> casesToUpdate = new List<Case>();
        for (Case c : [Select Id, Status From Case Where Id In : caseIDs]) {
        c.Status = 'closed';
        casesToUpdate.add(c);
        }
   
    update casesToUpdate;
    }
}

this is what it looks like but when i save it  

                     id:  01q110000004GxR               
                     problem:  Expression must be a list type: SET<Id>
                     extent:  ApexTrigger                   
                     line:  28                            
                     name:  Ticket


is the error