Advertisement
matthewpoer

SugarCRM: When an Account was Last Contacted

Dec 3rd, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 4.33 KB | None | 0 0
  1. -- update the last_touched_datetime_c based on directly related calls
  2. update calls,accounts,accounts_cstm
  3. set accounts_cstm.last_touched_datetime_c = calls.date_end
  4. where calls.parent_id = accounts.id and accounts.deleted = 0
  5.     and accounts.id = accounts_cstm.id_c
  6. and calls.status = 'Held' and calls.parent_type = 'Accounts' and calls.deleted=0
  7. and (accounts_cstm.last_touched_datetime_c is null OR calls.date_end > accounts_cstm.last_touched_datetime_c);
  8.  
  9. -- update the last_touched_datetime_c based on directly related meetings
  10. update meetings,accounts,accounts_cstm
  11. set accounts_cstm.last_touched_datetime_c = meetings.date_end
  12. where meetings.parent_id = accounts.id and accounts.deleted = 0
  13.     and accounts.id = accounts_cstm.id_c
  14. and meetings.status = 'Held' and meetings.parent_type = 'Accounts' and meetings.deleted=0
  15. and (accounts_cstm.last_touched_datetime_c is null OR meetings.date_end > accounts_cstm.last_touched_datetime_c);
  16.  
  17. -- update the last_touched_datetime_c based on directly related notes
  18. update notes,accounts,accounts_cstm
  19. set accounts_cstm.last_touched_datetime_c = notes.date_entered
  20. where notes.parent_id = accounts.id and accounts.deleted = 0
  21.     and accounts.id = accounts_cstm.id_c
  22. and notes.parent_type = 'Accounts' and notes.deleted=0
  23. and (accounts_cstm.last_touched_datetime_c is null OR notes.date_entered > accounts_cstm.last_touched_datetime_c);
  24.  
  25. -- update the last_touched_datetime_c based on directly related emails
  26. update emails,accounts,accounts_cstm
  27. set accounts_cstm.last_touched_datetime_c = emails.date_sent
  28. where emails.parent_id = accounts.id and accounts.deleted = 0
  29.     and accounts.id = accounts_cstm.id_c
  30. and emails.parent_type = 'Accounts' and emails.deleted=0
  31. and (accounts_cstm.last_touched_datetime_c is null OR emails.date_sent > accounts_cstm.last_touched_datetime_c);
  32.  
  33. -- update the last_touched_datetime_c based on indirectly related calls
  34. update calls,contacts,accounts_contacts,accounts,accounts_cstm
  35. set accounts_cstm.last_touched_datetime_c = calls.date_end
  36. where
  37. calls.parent_id = contacts.id and contacts.deleted = 0 and calls.deleted=0
  38. and accounts_contacts.contact_id = contacts.id and accounts_contacts.deleted =0
  39. and accounts.id = accounts_contacts.account_id and accounts.deleted=0
  40. and accounts.id = accounts_cstm.id_c
  41. and calls.parent_type = 'Contacts' and calls.status = 'Held'
  42. and (accounts_cstm.last_touched_datetime_c is null OR calls.date_end > accounts_cstm.last_touched_datetime_c);
  43.  
  44. -- update the last_touched_datetime_c based on indirectly related meetings
  45. update meetings,contacts,accounts_contacts,accounts,accounts_cstm
  46. set accounts_cstm.last_touched_datetime_c = meetings.date_end
  47. where
  48. meetings.parent_id = contacts.id and contacts.deleted = 0 and meetings.deleted=0
  49. and accounts_contacts.contact_id = contacts.id and accounts_contacts.deleted =0
  50. and accounts.id = accounts_contacts.account_id and accounts.deleted=0
  51. and accounts.id = accounts_cstm.id_c
  52. and meetings.parent_type = 'Contacts' and meetings.status = 'Held'
  53. and (accounts_cstm.last_touched_datetime_c is null OR meetings.date_end > accounts_cstm.last_touched_datetime_c);
  54.  
  55. -- update the last_touched_datetime_c based on indirectly related notes
  56. update notes,contacts,accounts_contacts,accounts,accounts_cstm
  57. set accounts_cstm.last_touched_datetime_c = notes.date_entered
  58. where
  59. notes.parent_id = contacts.id and contacts.deleted = 0 and notes.deleted=0
  60. and accounts_contacts.contact_id = contacts.id and accounts_contacts.deleted =0
  61. and accounts.id = accounts_contacts.account_id and accounts.deleted=0
  62. and accounts.id = accounts_cstm.id_c
  63. and notes.parent_type = 'Contacts'
  64. and (accounts_cstm.last_touched_datetime_c is null OR notes.date_entered > accounts_cstm.last_touched_datetime_c);
  65.  
  66. -- update the last_touched_datetime_c based on indirectly related emails
  67. update emails,contacts,accounts_contacts,accounts,accounts_cstm
  68. set accounts_cstm.last_touched_datetime_c = emails.date_sent
  69. where
  70. emails.parent_id = contacts.id and contacts.deleted = 0 and emails.deleted=0
  71. and accounts_contacts.contact_id = contacts.id and accounts_contacts.deleted =0
  72. and accounts.id = accounts_contacts.account_id and accounts.deleted=0
  73. and accounts.id = accounts_cstm.id_c
  74. and emails.parent_type = 'Contacts'
  75. and (accounts_cstm.last_touched_datetime_c is null OR emails.date_sent > accounts_cstm.last_touched_datetime_c);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement