Using Case History Created Date When The Case Status Became New For Existing Cases

by ADMIN 83 views

As a Salesforce administrator or developer, you're likely no stranger to the complexities of managing cases and their associated history. One common challenge many users face is retrieving the created date when a case status becomes "New." In this article, we'll delve into the world of Apex, Case, and Field History to provide a comprehensive solution for this problem.

Understanding Case History

Case History is a powerful feature in Salesforce that allows you to track changes made to a case over time. This includes updates to fields, changes to the case status, and even user assignments. By leveraging Case History, you can gain valuable insights into the evolution of a case and make more informed decisions.

The Challenge: Retrieving Created Date for New Case Status

When a case is created, its status is typically set to "New." However, as the case progresses through its lifecycle, the status may change multiple times. The challenge lies in retrieving the created date when the case status first becomes "New." This information can be crucial in understanding the initial creation of a case and its subsequent history.

Using Apex to Retrieve Case History

To solve this problem, we'll use Apex, Salesforce's proprietary programming language. Apex allows you to write custom code that interacts with the Salesforce platform, making it an ideal solution for this challenge.

Step 1: Create a Custom Field

Before we begin, we need to create a custom field to store the created date when the case status becomes "New." This field will be used to populate the data retrieved from Case History.

  • Log in to your Salesforce org and navigate to the Setup menu.
  • Click on Customize > Cases > Fields.
  • Click the New Field button and select Date as the field type.
  • Name the field (e.g., "New_Status_Created_Date") and click Save.

Step 2: Write an Apex Trigger

Next, we'll create an Apex trigger that will retrieve the created date when the case status becomes "New." This trigger will be fired whenever a case is updated.

  • Navigate to the Setup menu and click on Develop > Apex Triggers.
  • Click the New button and select After Update as the trigger type.
  • Name the trigger (e.g., "New_Status_Created_Date_Trigger") and click Save.

Step 3: Write the Apex Code

Now, we'll write the Apex code that will retrieve the created date when the case status becomes "New." This code will use the CaseHistory object to query the Case History records.

trigger New_Status_Created_Date_Trigger on Case (after update) {
    // Check if the case status has changed to "New"
    if (Trigger.oldMap.get('Status') != 'New' && Trigger.newMap.get('Status') == 'New') {
        // Retrieve the created date from Case History
        List<CaseHistory> caseHistories = [SELECT CreatedDate FROM CaseHistory WHERE CaseId = :Trigger.newId AND Field = 'Status' AND OldValue = 'New'];
    // Check if any Case History records were
    if (caseHistories.size() &gt; 0) {
        // Update the custom field with the created date
        Trigger.newMap.put(&#39;New_Status_Created_Date&#39;, caseHistories[0].CreatedDate);
    }
}

}

Step 4: Deploy the Apex Trigger

Finally, we'll deploy the Apex trigger to our Salesforce org.

  • Navigate to the Setup menu and click on Develop > Apex Triggers.
  • Click the Deploy button next to the trigger and follow the prompts to deploy it to your org.

Conclusion

In this article, we've explored the world of Case History and Apex to provide a comprehensive solution for retrieving the created date when a case status becomes "New." By following these steps, you can unlock the power of Case History and gain valuable insights into the evolution of your cases.

Best Practices

When working with Case History and Apex, keep the following best practices in mind:

  • Always test your code thoroughly before deploying it to production.
  • Use meaningful variable names and comments to make your code easier to understand.
  • Consider using a testing framework to ensure your code is robust and reliable.

Additional Resources

For more information on Case History and Apex, check out the following resources:

In our previous article, we explored the world of Case History and Apex to provide a comprehensive solution for retrieving the created date when a case status becomes "New." However, we know that there are many more questions and challenges that arise when working with Case History and Apex. In this article, we'll address some of the most frequently asked questions and provide additional insights to help you master the art of Case History and Apex.

Q: What is Case History, and why is it important?

A: Case History is a powerful feature in Salesforce that allows you to track changes made to a case over time. This includes updates to fields, changes to the case status, and even user assignments. By leveraging Case History, you can gain valuable insights into the evolution of a case and make more informed decisions.

Q: How do I create a custom field to store the created date when the case status becomes "New"?

A: To create a custom field, follow these steps:

  1. Log in to your Salesforce org and navigate to the Setup menu.
  2. Click on Customize > Cases > Fields.
  3. Click the New Field button and select Date as the field type.
  4. Name the field (e.g., "New_Status_Created_Date") and click Save.

Q: What is an Apex trigger, and how do I create one?

A: An Apex trigger is a custom code that is executed automatically when a specific event occurs, such as a case update. To create an Apex trigger, follow these steps:

  1. Navigate to the Setup menu and click on Develop > Apex Triggers.
  2. Click the New button and select After Update as the trigger type.
  3. Name the trigger (e.g., "New_Status_Created_Date_Trigger") and click Save.

Q: How do I write the Apex code to retrieve the created date when the case status becomes "New"?

A: To write the Apex code, follow these steps:

  1. Use the CaseHistory object to query the Case History records.
  2. Check if any Case History records were found.
  3. Update the custom field with the created date.

Here's an example of the Apex code:

trigger New_Status_Created_Date_Trigger on Case (after update) {
    // Check if the case status has changed to "New"
    if (Trigger.oldMap.get('Status') != 'New' && Trigger.newMap.get('Status') == 'New') {
        // Retrieve the created date from Case History
        List<CaseHistory> caseHistories = [SELECT CreatedDate FROM CaseHistory WHERE CaseId = :Trigger.newId AND Field = 'Status' AND OldValue = 'New'];
    // Check if any Case History records were found
    if (caseHistories.size() &gt; 0) {
        // Update the custom field with the created date
        Trigger.newMap.put(&#39;New_Status_Created_Date&#39;, caseHistories[0].CreatedDate);
    }
}

}

Q: How do I deploy the Apex trigger to my Salesforce org?

A: To deploy the Apex trigger, follow these steps:

  1. Navigate to the Setup menu and click on Develop > Apex Triggers.
  2. Click the Deploy button next to the trigger and follow the prompts to deploy it to your org.

Q: What are some best practices to keep in mind when working with Case History and Apex?

A: Here are some best practices to keep in mind:

  • Always test your code thoroughly before deploying it to production.
  • Use meaningful variable names and comments to make your code easier to understand.
  • Consider using a testing framework to ensure your code is robust and reliable.

Q: Where can I find additional resources to learn more about Case History and Apex?

A: Here are some additional resources to learn more about Case History and Apex:

By following these best practices and leveraging the resources provided, you'll be well on your way to mastering the art of Case History and Apex in Salesforce.