Guest User

Untitled

a guest
Apr 19th, 2018
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.82 KB | None | 0 0
  1. @isTest
  2. private class TicketAutoMatchTest
  3. {
  4. static testMethod void FirstAndLastMatch()
  5. {
  6. Campaign campaign = CreateCampaign();
  7. Opportunity opportunity = CreateOpportunity();
  8. Contact contact = CreateContact();
  9.  
  10. //do test
  11. {
  12. //this will be loaded from the database after storing a test ticket
  13. sbxe1__Ticket__c actualTicket = null;
  14. {
  15. //pass 1 - first and last and email match
  16. sbxe1__Ticket__c storedTicket = new sbxe1__Ticket__c();
  17. {
  18. //populate ticket to be stored
  19. {
  20. storedTicket.sbxe1__sbx_Opportunity__c = opportunity.Id;
  21. storedTicket.sbxe1__sbx_Campaign__c = campaign.Id;
  22. storedTicket.sbxe1__sbx_FirstName__c = contact.FirstName;
  23. storedTicket.sbxe1__sbx_LastName__c = contact.LastName;
  24. storedTicket.sbxe1__sbx_Email__c = contact.Email;
  25. storedTicket.sbxe1__sbx_TicketType__c = 'Student';
  26. }
  27. insert storedTicket;
  28.  
  29. List<sbxe1__Ticket__c> ticketList =
  30. [
  31. select
  32. t.Id, t.Name, t.sbxe1__sbx_TicketHolderContact__c
  33. from
  34. sbxe1__Ticket__c t
  35. where
  36. t.Id = :storedTicket.Id
  37. ];
  38.  
  39. system.Assert(
  40. ticketList.size() == 1,
  41. 'test method found more than one ticket ' + ticketList.size() + ' when testing pass 1');
  42.  
  43. actualTicket = ticketList[0];
  44. }
  45. }
  46. system.Assert(
  47. actualTicket.sbxe1__sbx_TicketHolderContact__c == contact.Id,
  48. 'Ticket Auto Match matched a contact record but sbxe1__sbx_TicketHolderContact__c does not equal contact Id');
  49. }
  50. }
  51.  
  52.  
  53. static testMethod void FirstAndLastAndMailingMatch()
  54. {
  55. Campaign campaign = CreateCampaign();
  56. Opportunity opportunity = CreateOpportunity();
  57. Contact contact = CreateContact();
  58.  
  59. //test stuff
  60. {
  61. //pass 2 - first and last name and mailing address
  62. sbxe1__Ticket__c actualTicket = null;
  63. {
  64. sbxe1__Ticket__c testTicket = new sbxe1__Ticket__c();
  65. //populate ticket
  66. {
  67. testTicket.sbxe1__sbx_Opportunity__c = opportunity.Id;
  68. testTicket.sbxe1__sbx_Campaign__c = campaign.Id;
  69. testTicket.sbxe1__sbx_FirstName__c = contact.FirstName;
  70. testTicket.sbxe1__sbx_LastName__c = contact.LastName;
  71. //not sure what behavior we should have if a different email was given
  72. // to the ticket than what is stored with the Contact in the database
  73. //Currently, it is as if the email was not given at all, a different
  74. // match will be tried
  75. testTicket.sbxe1__sbx_Email__c = contact.Email + 'someothertext';
  76. testTicket.sbxe1__sbx_MailingStreet__c = contact.MailingStreet;
  77. testTicket.sbxe1__sbx_MailingCity__c = contact.MailingCity;
  78. testTicket.sbxe1__sbx_MailingState__c = contact.MailingState;
  79. testTicket.sbxe1__sbx_MailingPostalCode__c = contact.MailingPostalCode;
  80. testTicket.sbxe1__sbx_TicketType__c = 'Student';
  81. }
  82. insert testTicket;
  83.  
  84.  
  85. List<sbxe1__Ticket__c> ticketList =
  86. [
  87. select
  88. t.Id, t.Name, t.sbxe1__sbx_TicketHolderContact__c
  89. from
  90. sbxe1__Ticket__c t
  91. where
  92. t.Id = :testTicket.Id
  93. ];
  94.  
  95. system.Assert(ticketList.size() == 1, 'test method found more than one ticket ' + ticketList.size() + ' when testing pass 2');
  96. actualTicket = ticketList[0];
  97. }
  98.  
  99. system.Assert(
  100. actualTicket.sbxe1__sbx_TicketHolderContact__c == contact.Id,
  101. 'Ticket Auto Match matched a contact record but sbxe1__sbx_TicketHolderContact__c does not equal contact Id');
  102. }
  103. }
  104.  
  105.  
  106. static testMethod void FirstOrLastAndEmailMatch()
  107. {
  108. Campaign campaign = CreateCampaign();
  109. Opportunity opportunity = CreateOpportunity();
  110. Contact contact = CreateContact();
  111.  
  112. //test stuff
  113. {
  114. //pass 3 - try for a (first name OR last name) AND email address match
  115. sbxe1__Ticket__c testTicket = new sbxe1__Ticket__c();
  116. {
  117. //populate ticket
  118. {
  119. testTicket.sbxe1__sbx_Opportunity__c = opportunity.Id;
  120. testTicket.sbxe1__sbx_Campaign__c = campaign.Id;
  121. testTicket.sbxe1__sbx_FirstName__c = 'First';
  122. testTicket.sbxe1__sbx_LastName__c = 'Butnotlast';
  123. testTicket.sbxe1__sbx_Email__c = 'email@email.com';
  124. testTicket.sbxe1__sbx_TicketType__c = 'Student';
  125. }
  126. insert testTicket;
  127.  
  128. List<sbxe1__Ticket__c> ticketList =
  129. [
  130. select
  131. t.Id, t.Name, t.sbxe1__sbx_TicketHolderContact__c
  132. from
  133. sbxe1__Ticket__c t
  134. where
  135. t.Id = :testTicket.Id
  136. ];
  137.  
  138. system.Assert(ticketList.size() == 1, 'test method found more than one ticket ' + ticketList.size() + ' when testing pass 3');
  139. }
  140. }
  141. }
  142.  
  143.  
  144. static testMethod void DuplicateContact()
  145. {
  146. //EmailUtil.DoSend = false;
  147.  
  148. Campaign campaign = CreateCampaign();
  149. Opportunity opportunity = CreateOpportunity();
  150. Contact contact = CreateContact();
  151. Contact duplicateContact = CreateContact();
  152.  
  153. //test stuff
  154. {
  155. sbxe1__Ticket__c testTicket = new sbxe1__Ticket__c();
  156. {
  157. testTicket.sbxe1__sbx_Opportunity__c = opportunity.Id;
  158. testTicket.sbxe1__sbx_Campaign__c = campaign.Id;
  159. testTicket.sbxe1__sbx_FirstName__c = contact.FirstName;
  160. testTicket.sbxe1__sbx_LastName__c = contact.LastName;
  161. testTicket.sbxe1__sbx_Email__c = contact.Email;
  162. testTicket.sbxe1__sbx_TicketType__c = 'Student';
  163. }
  164.  
  165. //and insert one of the tickets we are using here again
  166. //pass 1 - first and last and email match
  167. insert testTicket;
  168.  
  169. system.assertEquals(1, Limits.getEmailInvocations(), 'Expected an email to be sent');
  170. system.assert(EmailUtil.SentEmails.size() > 0, 'An email detailing an error should have been constructed');
  171. //check some of the body to make sure the correct email message is created
  172. system.assert(((Messaging.SingleEmailMessage)EmailUtil.SentEmails[0][0]).getPlainTextBody().contains('<p>More than one Contact found for'), ((Messaging.SingleEmailMessage)EmailUtil.SentEmails[0][0]).getPlainTextBody());
  173. }
  174. }
  175.  
  176.  
  177. private static Contact CreateContact()
  178. {
  179. Contact result = new Contact();
  180. {
  181. result.FirstName = 'First';
  182. result.LastName = 'Last';
  183. result.Email = 'email@email.com';
  184. result.MailingStreet = '1400 Townsend Dr.';
  185. result.MailingCity = 'Houghton';
  186. result.MailingState = 'MI';
  187. result.MailingPostalCode = '49931';
  188. }
  189. upsert result;
  190.  
  191. return result;
  192. }
  193.  
  194.  
  195. private static Campaign CreateCampaign()
  196. {
  197. Campaign result = new Campaign(Name='TicketAutoMatchTests Campaign', Training_Hours__c = 12);
  198. insert result;
  199.  
  200. return result;
  201. }
  202.  
  203.  
  204. private static Opportunity CreateOpportunity()
  205. {
  206. Opportunity result = new Opportunity();
  207. {
  208. result.Name = 'TicketAutoMatchTests Opportunity';
  209. result.StageName = 'Closed Won';
  210. result.CloseDate = system.today();
  211. }
  212. insert result;
  213.  
  214. return result;
  215. }
  216. }
  217.  
  218. public with sharing class SoapboxTicketAutoMatch
  219. {
  220. public class TicketMatcher
  221. {
  222. public TicketMatcher(sbxe1__Ticket__c ticket)
  223. {
  224. this.ticket = ticket;
  225. this.contacts = SoapboxHelper.getContacts(ticket);
  226. }
  227.  
  228.  
  229. public sbxe1__Ticket__c ticket { get; private set; }
  230. public List<Contact> contacts { get; private set; }
  231.  
  232.  
  233. public boolean hasErrors { get { return contacts.size() != 1; } }
  234. }
  235.  
  236.  
  237. public static void sendEmailOnErrors(List<sbxe1__Ticket__c> tickets)
  238. {
  239. try { SoapBoxTicketAutoMatch.throwOnErrors(trigger.new); }
  240. catch (SoapboxTicketMatchException xception)
  241. {
  242. List<String> errorMessages_noContact = new List<String>();
  243. List<String> errorMessages_multipleContacts = new List<String>();
  244. List<String> errorMessages_campaignsWithMultipleContacts = new List<String>();
  245. {
  246. //construct error messages
  247. {
  248. //match errors
  249. {
  250. for (SoapboxTicketAutoMatch.TicketMatcher matcher : xception.matchers)
  251. {
  252. if (matcher.contacts.size() == 0)
  253. {
  254. errorMessages_noContact.add(
  255. '<p>No Contact information found for' +
  256. SoapBoxTicketAutoMatch.createErrorMessageHelper(matcher.ticket) + '</br>' +
  257. 'Please review the tickets on this <a href=https://ctt.my.salesforce.com/' + matcher.ticket.sbxe1__sbx_Opportunity__c + '>Opportunity.</a>' +
  258. '</p>');
  259. }
  260. else if (matcher.contacts.size() > 1)
  261. {
  262. errorMessages_multipleContacts.add(
  263. '<p>More than one Contact found for' +
  264. SoapBoxTicketAutoMatch.createErrorMessageHelper(matcher.ticket) +
  265. '<br>The number of canidate Contacts is ' + matcher.contacts.size() +
  266. '</p>');
  267. }
  268. }
  269. }
  270.  
  271. //campaigns with duplicate contacts
  272. {
  273. if (xception.campaignIdsWithDuplicateContacts.size() != 0)
  274. {
  275. String errorMessage = '' + system.now() + '<p>Duplicate Registrants detected by Ticket Auto Match. Please review the tickets on the following links';
  276. for (ID campaignId : xception.campaignIdsWithDuplicateContacts)
  277. {
  278. errorMessage += '<br><a href=https://ctt.my.salesforce.com/' + campaignId + '>Campaign</a> and address any duplicates.';
  279. }
  280. errorMessage += '</p>';
  281.  
  282. errorMessages_campaignsWithMultipleContacts.add(errorMessage);
  283. }
  284. }
  285. }
  286. }
  287. sendEmail('No Salesforce Contact information found for ticket', errorMessages_noContact);
  288. sendEmail('Multiple Salesforce Contacts found for ticket', errorMessages_multipleContacts);
  289. sendEmail('Duplicate Registrants detected for ticket', errorMessages_campaignsWithMultipleContacts);
  290. }
  291. }
  292.  
  293.  
  294. public static void throwOnErrors(List<sbxe1__Ticket__c> tickets)
  295. {
  296. system.debug('TicketAutoMatch: Number of tickets to automatch = ' + tickets.size());
  297.  
  298. Set<Id> campaignIds = new Set<Id>();
  299. List<Contact> newTicketContacts = new List<Contact>();
  300. List<TicketMatcher> matchErrors = new List<TicketMatcher>();
  301. {
  302. //check each ticket to ensure there is one contact
  303. //set the sbxe1__sbx_TicketHolderContact__c field for valid tickets
  304. for (sbxe1__Ticket__c ticket : tickets)
  305. {
  306. Id opportunityId = ticket.sbxe1__sbx_Opportunity__c;
  307. Id campaignId = ticket.sbxe1__sbx_Campaign__c;
  308. campaignIds.add(campaignId);
  309.  
  310. TicketMatcher matcher = new TicketMatcher(ticket);
  311. if (matcher.hasErrors) { matchErrors.add(matcher); }
  312. else
  313. {
  314. Contact contact = matcher.contacts[0];
  315. newTicketContacts.add(contact);
  316. ticket.sbxe1__sbx_TicketHolderContact__c = contact.ID;
  317. }
  318. }
  319. }
  320.  
  321.  
  322. //doing this check after each ticket has been examined because a field this check
  323. // uses is set when the tickets are examined
  324. List<ID> campaignIdsWithDuplicateContacts = new List<ID>();
  325. {
  326. //check if any of the registered contacts are in more than one ticket
  327. for (ID campaignId : campaignIds)
  328. {
  329. List<sbxe1__Ticket__c> duplicateContacts =
  330. [
  331. select id, sbxe1__sbx_TicketHolderContact__c
  332. from sbxe1__Ticket__c
  333. where
  334. sbxe1__sbx_Campaign__c = :campaignId
  335. AND sbxe1__sbx_TicketHolderContact__c IN :newTicketContacts
  336. ];
  337.  
  338. if (duplicateContacts.size() != 0) { campaignIdsWithDuplicateContacts.add(campaignId); }
  339. }
  340. }
  341.  
  342.  
  343. //now throw an exception if any errors were found
  344. {
  345. if ((matchErrors.size() > 0) || (campaignIdsWithDuplicateContacts.size() > 0))
  346. {
  347. SoapboxTicketMatchException xception = new SoapboxTicketMatchException();
  348. {
  349. //no need to check which of these lists have values, adding no values doesn't hurt
  350. xception.matchers.addAll(matchErrors);
  351. xception.campaignIdsWithDuplicateContacts.addAll(campaignIdsWithDuplicateContacts);
  352. }
  353. throw xception;
  354. }
  355. }
  356. }
  357.  
  358.  
  359. /**
  360. * Helper method to construct an error message with data from the ticket
  361. */
  362. public static String createErrorMessageHelper(sbxe1__Ticket__c ticket)
  363. {
  364. String result = '';
  365. {
  366. String firstNameString = ticket.sbxe1__sbx_FirstName__c == null ? '' : ticket.sbxe1__sbx_FirstName__c;
  367. String lastNameString = ticket.sbxe1__sbx_LastName__c == null ? '' : ticket.sbxe1__sbx_LastName__c;
  368. String emailString = ticket.sbxe1__sbx_Email__c == null ? '' : ticket.sbxe1__sbx_Email__c;
  369. String streetString = ticket.sbxe1__sbx_MailingStreet__c == null ? '' : ticket.sbxe1__sbx_MailingStreet__c;
  370. String cityString = ticket.sbxe1__sbx_MailingCity__c == null ? '' : ticket.sbxe1__sbx_MailingCity__c;
  371. String stateString = ticket.sbxe1__sbx_MailingState__c == null ? '' : ticket.sbxe1__sbx_MailingState__c;
  372. String postalCodeString = ticket.sbxe1__sbx_MailingPostalCode__c == null ? '' : ticket.sbxe1__sbx_MailingPostalCode__c;
  373.  
  374. result =
  375. '<br>First Name: ' + firstNameString +
  376. '<br>Last Name: ' + lastNameString +
  377. ' <a href=https://ctt.my.salesforce.com/_ui/search/ui/UnifiedSearchResults?searchType=2&str='+firstNameString + '+' + lastNameString+'>Search</a> the org for this first and last name.' +
  378. '<br>Email Address: ' + emailString +
  379. ' <a href=https://ctt.my.salesforce.com/_ui/search/ui/UnifiedSearchResults?searchType=2&str='+emailString+'>Search</a> the org for this email address.' +
  380. '<br>Mailing Address: ' + streetString + ' ' + cityString + ', ' + stateString + ' ' + postalCodeString;
  381. }
  382. return result;
  383. }
  384.  
  385.  
  386. private static void sendEmail(String subject, List<String> messages)
  387. {
  388. if (!(messages.size() > 0)) { return; }
  389.  
  390.  
  391. String emailBody = '<p>An error occurred when matching the soapbox ticket with a contact';
  392. {
  393. for (string message : messages)
  394. {
  395. emailBody += message + '</br>';
  396. }
  397. }
  398. emailBody += '</p>';
  399. System.debug('TicketAutoMatch: ' + emailBody);
  400.  
  401.  
  402. //now send an email with the error message
  403. EmailUtil emails = new EmailUtil();
  404. {
  405. emails.createCttEmail(subject, emailBody);
  406. }
  407. emails.send();
  408. }
  409. }
Add Comment
Please, Sign In to add comment