Advertisement
martyychang

trigger LaatsteCallMobileMiles on Task

Feb 22nd, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. /*
  2.  * @see https://success.salesforce.com/answers?id=90630000000hdCVAAY
  3.  */
  4. trigger LaatsteCallMobileMiles on Task (after insert) {
  5.  
  6.     // Retrieve the email addresses of CreatedBy users for tasks currently
  7.     // being processed by the trigger
  8.  
  9.     List<Task> newTasks = [
  10.         SELECT Id, WhatId, Subject, CreatedDate, CreatedBy.Email
  11.         FROM Task
  12.         WHERE Id IN :Trigger.new
  13.     ];
  14.  
  15.     // Initialize a map of accounts to update, based on the Related To
  16.     // (WhatId) field on the task. The map will be keyed on the Account ID,
  17.     // to make sure the same account is not included twice in a single DML
  18.     // operation.
  19.  
  20.     Map<Id, Account> affectedAccountsById = new Map<Id, Account>();
  21.  
  22.     // Go through the new tasks and check to see whether the account shoudl
  23.     // be updated, based on the email address and subject criteria
  24.  
  25.     for (Task eachTask : newTasks) {
  26.         if (eachTask.WhatId != null) {
  27.             if (eachTask.CreatedBy.Email.contains('@mobilemiles.nl')
  28.                 && eachTask.Subject.contains('Made call to')) {
  29.  
  30.                 // Construct the Account record to update
  31.  
  32.                 Account affectedAccount = new Account(
  33.                     Id = eachTask.WhatId,
  34.                     Laatste_activiteit_Mobile_Miles__c = eachTask.CreatedDate);
  35.  
  36.                 // Put the affected account in the map
  37.  
  38.                 affectedAccountsById.put(affectedAccount.Id, affectedAccount);
  39.             }
  40.         }   // if (eachTask.WhatId != null)
  41.     }   // for each Task in newTasks
  42.  
  43.     // Update any affected accounts
  44.  
  45.     if (!affectedAccountsById.isEmpty())
  46.         update affectedAccountsById.values();
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement