Scenario: 9
Whenever a Case is created in Salesforce with a specific priority level, automatically create a Task for the Support Team to follow up within a specified timeframe.
code:
// Apex trigger to create a Task for Support Team on high priority Cases
trigger CreateTaskOnHighPriorityCase on Case (after insert) {
// Priority level for creating a Task
String highPriority = 'High'; // Change to the desired priority level
// Follow-up timeframe in days
Integer followUpDays = 3; // For example, follow up within 3 days
// List to hold Tasks to be created
List<Task> tasksToCreate = new List<Task>();
// Iterate through the newly inserted Cases
for (Case newCase : Trigger.new) {
// Check if the Case's Priority is high
if (newCase.Priority != null && newCase.Priority == highPriority) {
// Calculate the follow-up date
Datetime followUpDate = System.now().addDays(followUpDays);
// Create a new Task for the Support Team
Task task = new Task(
Subject = 'Follow-up on Case: ' + newCase.CaseNumber,
WhatId = newCase.Id,
ActivityDate = followUpDate,
OwnerId = '005XXXXXXXXXXXXXXX' // Replace with the Support Team's User Id
);
// Add the Task to the list for creation
tasksToCreate.add(task);
}
}
// Insert Tasks for high priority Cases
insert tasksToCreate;
}
Explanation:
- This Apex trigger fires after new Case records are inserted (
after insert
). - It checks each inserted Case's Priority against a specified high priority level (
highPriority
). - If a Case's Priority is high, it calculates the follow-up date based on the specified timeframe (
followUpDays
). - It then creates a new Task for the Support Team, assigning it to the specified User Id (
OwnerId
). - Finally, it inserts the Tasks for high priority Cases to prompt the Support Team for follow-up.
1 Comments
Awesome u
ReplyDelete