Salesforce Real Time Scenario: 8

 

Scenario: 8

As a Salesforce developer for a retail company, you're tasked with implementing a trigger to automate the creation of a Task whenever a new Case is created. The company wants to ensure that all incoming Cases are promptly assigned to the appropriate support agents for resolution. Additionally, the Task should include details about the Case and be assigned to the appropriate support queue for triage. How would you approach this scenario using Apex triggers? 

Code: 

// Trigger: CreateTaskForCaseTrigger // Object: Case // Purpose: Automatically create a Task when a new Case is created trigger CreateTaskForCaseTrigger on Case (after insert) { // Method to create Task for new Cases public static void createTaskForCase(List<Case> newCases) { // List to store Tasks to be inserted List<Task> tasksToInsert = new List<Task>(); // Iterate through new Cases to create Tasks for (Case c : newCases) { Task newTask = new Task(); newTask.Subject = 'Case Assignment: ' + c.Subject; newTask.Description = 'Please review and resolve the following Case: ' + c.CaseNumber; newTask.WhatId = c.Id; // Related Case newTask.OwnerId = 'Specify Support Queue Id'; // Assign to support queue for triage newTask.Priority = 'Medium'; // Set default priority newTask.Status = 'Open'; // Set default status newTask.ActivityDate = Date.today(); // Set due date tasksToInsert.add(newTask); } // Insert Tasks if (!tasksToInsert.isEmpty()) { insert tasksToInsert; } } // Trigger handler method public static void afterInsert(List<Case> newCases) { createTaskForCase(newCases); } }

Evaluation:

  • The candidate provides an Apex trigger named CreateTaskForCaseTrigger that executes after the insertion of Case records.
  • They effectively use SOQL queries within the trigger to query related records and populate a list of Tasks to be inserted.
  • The code iterates through new Case records and creates Tasks with appropriate details such as Subject, Description, OwnerId (assigned to support queue), Priority, Status, and ActivityDate (due date).
  • The trigger handler method afterInsert calls the createTaskForCase method, maintaining separation of concerns and promoting code maintainability.
  •  Stay tuned for more tutorials and join our community SFDC Telugu! 

    Apex Programs PDF Free Downloads For Channel Members Join Channel Membership 

    For more detailed explanations, practical examples, and resources, refer your friends to SFDC Telugu:











  • www.sfdctelugu.in

  • Post a Comment

    0 Comments