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
Srujana ReddySrujana Reddy 

How to Update the Case status using trigger

How to Update the Case status to Closed using trigger When ClosedDate is Today.
can any one help me.
Thanks in advance
 
SFDC Ravi KumarSFDC Ravi Kumar
Hi Reddy,
                     The way you'd do that is by creating a time-dependent workflow action. Essentially, you place the case in the workflow queue as soon as it is Active and has an Expiry Date, and then Salesforce will wait until the Expiry Date arrives and update the record to Expired.

Here are some guidelines to setting your workflow:

1) Go to Setup > Create > Workflow & Approvals > Workflow Rules
2) Click New Rule, then select Case
3) Give your rule a name, leave the evaluation criteria on 'created, and any time it’s edited to subsequently meet criteria'
4) Specify your Rule Criteria, which I think should be Case Status equals Active and Case Expiry Date not equal to (leave it blank).
5) Click Save and Next
6) Add a Time Trigger of 0 Days After Expiry Date - then click Save
7) In your Time-Dependent Workflow Action, click Add Workflow Action > New Field Update
8) Give your Field Update a name, select Expiry Date and then select a specific value of Expired
9) Click Save, click Done, click Activate
10) Test your new rule using the Time-Based Workflow option in Setup
Srujana ReddySrujana Reddy
Hi,
SFDC Ravi Kumar  thank you for Your Response
Actually I want to do with the help of Trigger.
Hemant_JainHemant_Jain
Refer below code:
 
trigger caseStatusUpdate on Case(after update) {

    Set<Id> caseId = new Set<Id>();
    for (Integer i = 0; i < Trigger.new.size(); i++)
    {
        if(Trigger.new[i].CloseDate!=Trigger.old[i].CloseDate && Trigger.new[i].CloseDate==date.today())
        caseId.add(Trigger.new[i].Id);
    }
    
    List<Case> caseListToUpdate = new List<Case>();

    for(Case caseObj: [Select Name, Id,Status FROM Case Where Id IN :caseId])
    {
       caseObj.Status = 'Closed';
       caseListToUpdate.add(caseObj);
  
    }
  
    if(caseListToUpdate.size()>0)
    update caseListToUpdate;
   
}

If this resolves, kindly mark it as best answer
Amit Chaudhary 8Amit Chaudhary 8
Hi Srujana Reddy ,

NOTE:- Trigger will only execute when you will update any record it will not pick any record automaticly.

For your requirement you need batch job. Please check below post for Batch Job
1) http://amitsalesforce.blogspot.com/2016/02/batch-apex-in-salesforce-test-class-for.html

Try below sample Batch job
global class AccountUpdateBatchJob implements Database.Batchable<sObject> 
{
    global Database.QueryLocator start(Database.BatchableContext BC) 
    {
		
		return Database.getQueryLocator([SELECT Id,Status FROM Case where closedDate = today ]);
		
    }
    global void execute(Database.BatchableContext BC, List<Case> scope) 
    {
        for(Case c : scope)
        {
            c.Status  = 'Closed';
        }
        update scope;
    }
    global void finish(Database.BatchableContext BC) {
    }
}

Let us know if this will help you