|
| 1 | +// EXAMPLE |
| 2 | +/* |
| 3 | + |
| 4 | +ActivityLogEntry a = new ActivityLogEntry (); |
| 5 | +a.contact = '003L000000FsQSz'; |
| 6 | +a.opportunity = '006L0000003fNkz'; |
| 7 | +a.entry_type = 'TEAM-START'; |
| 8 | +a.action = 'Start Team'; |
| 9 | +a.outcome = 'Complete'; |
| 10 | +a.chatter_post = true; |
| 11 | +a.create (); |
| 12 | +
|
| 13 | +
|
| 14 | +*/ |
| 15 | +// END EXAMPLE |
| 16 | + |
| 17 | +// Activity Log must be consistent - enforced via this class |
| 18 | + |
| 19 | +public with sharing class ActivityLogEntry { |
| 20 | + |
| 21 | + // TODO - Add Custom Setting to supress all Chatter posts - this will be used for conversion |
| 22 | + public ID account_id; |
| 23 | + public ID campaign_id; |
| 24 | + public ID contact_id; |
| 25 | + public ID related_contact_1; |
| 26 | + public ID related_contact_2; |
| 27 | + |
| 28 | + // --- new to be implemented |
| 29 | + public ID organizer_id; |
| 30 | + public ID leader_id; |
| 31 | + public ID team_id; |
| 32 | + // -- end new |
| 33 | + |
| 34 | + public ID opportunity_id; |
| 35 | + public string entry_type; |
| 36 | + public string action; |
| 37 | + public string outcome; |
| 38 | + public boolean chatter_postable; |
| 39 | + public datetime entry_date_time; |
| 40 | + |
| 41 | + private List < String > valid_log_entry_types; |
| 42 | + private ID activity_log_entry_id; |
| 43 | + |
| 44 | + // init routine - required by constructors |
| 45 | + private void init () { |
| 46 | + valid_log_entry_types = new List <string> ( ); |
| 47 | + |
| 48 | + // ORGANIZING MODEL ACTIVITIES |
| 49 | + // TODO Make this custom setting with Type Key and Chatter Postability |
| 50 | + |
| 51 | + valid_log_entry_types.add ( 'MEMBER-JOIN' ); // New Membership ( Team, Leader, Organizer, Contact (member), Membership Gift ) |
| 52 | + valid_log_entry_types.add ( 'MEMBER-RENEW' ); // Membership Renewal ( Team, Leader, Organizer, Contact (member), Membership Gift ) |
| 53 | + valid_log_entry_types.add ( 'MEMBER-LAPSE' ); // Membership Lapsed ( Team, Leader, Organizer, Contact (member) ) |
| 54 | + valid_log_entry_types.add ( 'MEMBER-DROP' ); // Membership Dropped ( Team, Leader, Organizer, Contact (member) ) |
| 55 | + |
| 56 | + //building what is essentially a data warehouse file here is starting to feel super heavy and I'm not convinced that it's necessary. Are we really going to go to this as historical data |
| 57 | + //when we acquire a BI/Warehouse tool? will it be useable?? |
| 58 | + |
| 59 | + valid_log_entry_types.add ( 'ORG-AFF-START' ); // Affiliation Started ( Team, Leader, Organizer, Contact (member) ) |
| 60 | + valid_log_entry_types.add ( 'ORG-AFF-END' ); // Affiliation Started ( Team, Leader, Organizer, Contact (member) ) |
| 61 | + |
| 62 | + // default fields |
| 63 | + |
| 64 | + if ( chatter_postable == null ) { |
| 65 | + chatter_postable = false; |
| 66 | + } |
| 67 | + |
| 68 | + activity_log_entry_id = null; |
| 69 | + } |
| 70 | + |
| 71 | + // basic constructor |
| 72 | + public ActivityLogEntry () { |
| 73 | + init(); |
| 74 | + } |
| 75 | + |
| 76 | + // overloaded constructor with quick setup |
| 77 | + public ActivityLogEntry ( ID account, ID campaign, ID contact, ID related_contact_1, ID related_contact_2, ID organizer, ID leader, ID team, ID opportunity, String entry_type, String action, String outcome, Boolean post_chatter ) { |
| 78 | + this.account_id = account; |
| 79 | + this.campaign_id = campaign; |
| 80 | + this.contact_id = contact; |
| 81 | + this.related_contact_1 = related_contact_1; |
| 82 | + this.related_contact_2 = related_contact_2; |
| 83 | + this.organizer_id= organizer; |
| 84 | + this.leader_id= leader; |
| 85 | + this.team_id= team; |
| 86 | + this.opportunity_id = opportunity; |
| 87 | + this.entry_type = entry_type; |
| 88 | + this.action = action; |
| 89 | + this.outcome = outcome; |
| 90 | + this.chatter_postable = post_chatter; |
| 91 | + init(); |
| 92 | + } |
| 93 | + |
| 94 | + //overloaded constructor for Membership Activities |
| 95 | + public ActivityLogEntry (ID contact, ID organizer, ID leader, ID team, ID opportunity, String entry_type, String action, Boolean post_chatter) { |
| 96 | + this.contact_id = contact; |
| 97 | + this.organizer_id= organizer; |
| 98 | + this.leader_id= leader; |
| 99 | + this.team_id= team; |
| 100 | + this.opportunity_id = opportunity; |
| 101 | + this.entry_type = entry_type; |
| 102 | + this.action = action; |
| 103 | + this.chatter_postable = post_chatter; |
| 104 | + init(); |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | + // TODO create a bulk version of this (see Bulk cases) |
| 109 | + // Note: Chatter component relies on the fact that the actual User is initiating this transaction |
| 110 | + |
| 111 | + public boolean create ( ) { |
| 112 | + |
| 113 | + // check for type exceptions ??what does that mean |
| 114 | + system.debug('***************************************At least were in the Create Method'); |
| 115 | + |
| 116 | + // create the activity log entry |
| 117 | + Activity_Log__c al = new Activity_Log__c (); |
| 118 | + al.Account__c = account_id; |
| 119 | + al.Campaign__c = campaign_id; |
| 120 | + al.Contact__c = contact_id; |
| 121 | + al.Leader__c = (leader_id!='' ? leader_id : null); |
| 122 | + al.Organizer__c = (organizer_id!='' ? organizer_id : null); |
| 123 | + al.Team__c = (team_id!='' ? team_id : null); |
| 124 | + al.Related_Contact_1__c = related_contact_1; |
| 125 | + al.Related_Contact_2__c = related_contact_2; |
| 126 | + |
| 127 | + al.Opportunity__c = (opportunity_id!='' ? opportunity_id : null); |
| 128 | + al.Type__c = entry_type; |
| 129 | + al.Action__c = action; |
| 130 | + al.Outcome__c = outcome; |
| 131 | + al.User__c = UserInfo.getUserId(); |
| 132 | + |
| 133 | + // insert the new record |
| 134 | + try { |
| 135 | + insert al; |
| 136 | + } catch (exception e) { |
| 137 | + system.debug('***************************************Entered the catch'); |
| 138 | + throw new StandException(119); |
| 139 | + } |
| 140 | + |
| 141 | + activity_log_entry_id = al.Id; |
| 142 | + |
| 143 | + // whoa - chatter post |
| 144 | + // note: no account, campaign supported |
| 145 | + if ( chatter_postable ) { |
| 146 | + chatterHandler ( ); |
| 147 | + |
| 148 | + } |
| 149 | + |
| 150 | + return true; |
| 151 | + } |
| 152 | + |
| 153 | + private boolean chatterHandler ( ) { |
| 154 | + |
| 155 | + string chatter_body; |
| 156 | + string chatter_taglist; |
| 157 | + string baseUrl = string.ValueOf(URL.getSalesforceBaseURL().toExternalForm()); |
| 158 | + Account team = ([SELECT Name FROM Account WHERE Id =:this.team_id]); |
| 159 | + string team_name = team.name; |
| 160 | + |
| 161 | + if ( this.entry_type == 'MEMBER-JOIN' ) { |
| 162 | + chatter_body = 'New Member on the ' + team_name +'!'; |
| 163 | + chatter_taglist = '#' + team_name.replaceAll('\\s+', '') + ' #' + this.action.replaceAll('\\s+', ''); |
| 164 | + return createChatterPost ( this.contact_id , chatter_body, chatter_taglist, baseUrl +'/' + this.activity_log_entry_id , 'view log entry' ); |
| 165 | + } else if (this.entry_type == 'MEMBER-RENEW') { |
| 166 | + chatter_body = 'Renewed Member on the ' + team_name +'!'; |
| 167 | + chatter_taglist = '#' + team_name.replaceAll('\\s+', '') + ' #' + this.action.replaceAll('\\s+', ''); |
| 168 | + return createChatterPost ( this.contact_id , chatter_body, chatter_taglist, baseUrl +'/' + this.activity_log_entry_id , 'view log entry' ); |
| 169 | + } |
| 170 | + |
| 171 | + return false; |
| 172 | + } |
| 173 | + |
| 174 | + private boolean createChatterPost ( ID obj_id, string body, string tag_list, string link_url, string link_title ) { |
| 175 | + |
| 176 | + FeedItem post = new FeedItem(); |
| 177 | + post.ParentId = obj_id; |
| 178 | + post.Body = body + '\r\n\r\n' + '{ ' + tag_list + ' }'; |
| 179 | + post.LinkUrl = link_url; |
| 180 | + post.Title = link_title; |
| 181 | + |
| 182 | + insert post; |
| 183 | + |
| 184 | + return true; |
| 185 | + |
| 186 | + } |
| 187 | +} |
0 commit comments