Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * @see https://success.salesforce.com/answers?id=90630000000hdCVAAY
- */
- trigger LaatsteCallMobileMiles on Task (after insert) {
- // Retrieve the email addresses of CreatedBy users for tasks currently
- // being processed by the trigger
- List<Task> newTasks = [
- SELECT Id, WhatId, Subject, CreatedDate, CreatedBy.Email
- FROM Task
- WHERE Id IN :Trigger.new
- ];
- // Initialize a map of qualyfing tasks, keyed on the Related To (WhatId)
- // field on the task. This will be used later to pull in Task.CreatedDate
- // when updating accounts
- Map<Id, Task> qualifyingTasksByWhatId = new Map<Id, Task>();
- // Go through the new tasks and check to see whether the task qualifies
- // based on the email address and subject criteria
- for (Task eachTask : newTasks) {
- if (eachTask.WhatId != null) {
- if (eachTask.CreatedBy.Email.contains('@mobilemiles.nl')
- && eachTask.Subject.contains('Made call to')) {
- // Put the qualifying task in the map
- qualifyingTasksByWhatId.put(eachTask.WhatId, eachTask);
- }
- } // if (eachTask.WhatId != null)
- } // for each Task in newTasks
- // Look for accounts that were affected, by querying the account object.
- // This avoids situations where the task is not related to an account but
- // to some other type of record in the system.
- List<Account> affectedAccounts = [
- SELECT Id
- FROM Account
- WHERE Id IN :qualifyingTasksByWhatId.keySet()
- ];
- // Update any affected accounts
- if (affectedAccounts.size() > 0) {
- for (Account eachAccount : affectedAccounts) {
- eachAccount.Laatste_activiteit_Mobile_Miles__c =
- qualifyingTasksByWhatId.get(eachAccount.Id).CreatedDate;
- }
- update affectedAccounts;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement