Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @isTest
- private class TicketAutoMatchTest
- {
- static testMethod void FirstAndLastMatch()
- {
- Campaign campaign = CreateCampaign();
- Opportunity opportunity = CreateOpportunity();
- Contact contact = CreateContact();
- //do test
- {
- //this will be loaded from the database after storing a test ticket
- sbxe1__Ticket__c actualTicket = null;
- {
- //pass 1 - first and last and email match
- sbxe1__Ticket__c storedTicket = new sbxe1__Ticket__c();
- {
- //populate ticket to be stored
- {
- storedTicket.sbxe1__sbx_Opportunity__c = opportunity.Id;
- storedTicket.sbxe1__sbx_Campaign__c = campaign.Id;
- storedTicket.sbxe1__sbx_FirstName__c = contact.FirstName;
- storedTicket.sbxe1__sbx_LastName__c = contact.LastName;
- storedTicket.sbxe1__sbx_Email__c = contact.Email;
- storedTicket.sbxe1__sbx_TicketType__c = 'Student';
- }
- insert storedTicket;
- List<sbxe1__Ticket__c> ticketList =
- [
- select
- t.Id, t.Name, t.sbxe1__sbx_TicketHolderContact__c
- from
- sbxe1__Ticket__c t
- where
- t.Id = :storedTicket.Id
- ];
- system.Assert(
- ticketList.size() == 1,
- 'test method found more than one ticket ' + ticketList.size() + ' when testing pass 1');
- actualTicket = ticketList[0];
- }
- }
- system.Assert(
- actualTicket.sbxe1__sbx_TicketHolderContact__c == contact.Id,
- 'Ticket Auto Match matched a contact record but sbxe1__sbx_TicketHolderContact__c does not equal contact Id');
- }
- }
- static testMethod void FirstAndLastAndMailingMatch()
- {
- Campaign campaign = CreateCampaign();
- Opportunity opportunity = CreateOpportunity();
- Contact contact = CreateContact();
- //test stuff
- {
- //pass 2 - first and last name and mailing address
- sbxe1__Ticket__c actualTicket = null;
- {
- sbxe1__Ticket__c testTicket = new sbxe1__Ticket__c();
- //populate ticket
- {
- testTicket.sbxe1__sbx_Opportunity__c = opportunity.Id;
- testTicket.sbxe1__sbx_Campaign__c = campaign.Id;
- testTicket.sbxe1__sbx_FirstName__c = contact.FirstName;
- testTicket.sbxe1__sbx_LastName__c = contact.LastName;
- //not sure what behavior we should have if a different email was given
- // to the ticket than what is stored with the Contact in the database
- //Currently, it is as if the email was not given at all, a different
- // match will be tried
- testTicket.sbxe1__sbx_Email__c = contact.Email + 'someothertext';
- testTicket.sbxe1__sbx_MailingStreet__c = contact.MailingStreet;
- testTicket.sbxe1__sbx_MailingCity__c = contact.MailingCity;
- testTicket.sbxe1__sbx_MailingState__c = contact.MailingState;
- testTicket.sbxe1__sbx_MailingPostalCode__c = contact.MailingPostalCode;
- testTicket.sbxe1__sbx_TicketType__c = 'Student';
- }
- insert testTicket;
- List<sbxe1__Ticket__c> ticketList =
- [
- select
- t.Id, t.Name, t.sbxe1__sbx_TicketHolderContact__c
- from
- sbxe1__Ticket__c t
- where
- t.Id = :testTicket.Id
- ];
- system.Assert(ticketList.size() == 1, 'test method found more than one ticket ' + ticketList.size() + ' when testing pass 2');
- actualTicket = ticketList[0];
- }
- system.Assert(
- actualTicket.sbxe1__sbx_TicketHolderContact__c == contact.Id,
- 'Ticket Auto Match matched a contact record but sbxe1__sbx_TicketHolderContact__c does not equal contact Id');
- }
- }
- static testMethod void FirstOrLastAndEmailMatch()
- {
- Campaign campaign = CreateCampaign();
- Opportunity opportunity = CreateOpportunity();
- Contact contact = CreateContact();
- //test stuff
- {
- //pass 3 - try for a (first name OR last name) AND email address match
- sbxe1__Ticket__c testTicket = new sbxe1__Ticket__c();
- {
- //populate ticket
- {
- testTicket.sbxe1__sbx_Opportunity__c = opportunity.Id;
- testTicket.sbxe1__sbx_Campaign__c = campaign.Id;
- testTicket.sbxe1__sbx_FirstName__c = 'First';
- testTicket.sbxe1__sbx_LastName__c = 'Butnotlast';
- testTicket.sbxe1__sbx_Email__c = 'email@email.com';
- testTicket.sbxe1__sbx_TicketType__c = 'Student';
- }
- insert testTicket;
- List<sbxe1__Ticket__c> ticketList =
- [
- select
- t.Id, t.Name, t.sbxe1__sbx_TicketHolderContact__c
- from
- sbxe1__Ticket__c t
- where
- t.Id = :testTicket.Id
- ];
- system.Assert(ticketList.size() == 1, 'test method found more than one ticket ' + ticketList.size() + ' when testing pass 3');
- }
- }
- }
- static testMethod void DuplicateContact()
- {
- //EmailUtil.DoSend = false;
- Campaign campaign = CreateCampaign();
- Opportunity opportunity = CreateOpportunity();
- Contact contact = CreateContact();
- Contact duplicateContact = CreateContact();
- //test stuff
- {
- sbxe1__Ticket__c testTicket = new sbxe1__Ticket__c();
- {
- testTicket.sbxe1__sbx_Opportunity__c = opportunity.Id;
- testTicket.sbxe1__sbx_Campaign__c = campaign.Id;
- testTicket.sbxe1__sbx_FirstName__c = contact.FirstName;
- testTicket.sbxe1__sbx_LastName__c = contact.LastName;
- testTicket.sbxe1__sbx_Email__c = contact.Email;
- testTicket.sbxe1__sbx_TicketType__c = 'Student';
- }
- //and insert one of the tickets we are using here again
- //pass 1 - first and last and email match
- insert testTicket;
- system.assertEquals(1, Limits.getEmailInvocations(), 'Expected an email to be sent');
- system.assert(EmailUtil.SentEmails.size() > 0, 'An email detailing an error should have been constructed');
- //check some of the body to make sure the correct email message is created
- system.assert(((Messaging.SingleEmailMessage)EmailUtil.SentEmails[0][0]).getPlainTextBody().contains('<p>More than one Contact found for'), ((Messaging.SingleEmailMessage)EmailUtil.SentEmails[0][0]).getPlainTextBody());
- }
- }
- private static Contact CreateContact()
- {
- Contact result = new Contact();
- {
- result.FirstName = 'First';
- result.LastName = 'Last';
- result.Email = 'email@email.com';
- result.MailingStreet = '1400 Townsend Dr.';
- result.MailingCity = 'Houghton';
- result.MailingState = 'MI';
- result.MailingPostalCode = '49931';
- }
- upsert result;
- return result;
- }
- private static Campaign CreateCampaign()
- {
- Campaign result = new Campaign(Name='TicketAutoMatchTests Campaign', Training_Hours__c = 12);
- insert result;
- return result;
- }
- private static Opportunity CreateOpportunity()
- {
- Opportunity result = new Opportunity();
- {
- result.Name = 'TicketAutoMatchTests Opportunity';
- result.StageName = 'Closed Won';
- result.CloseDate = system.today();
- }
- insert result;
- return result;
- }
- }
- public with sharing class SoapboxTicketAutoMatch
- {
- public class TicketMatcher
- {
- public TicketMatcher(sbxe1__Ticket__c ticket)
- {
- this.ticket = ticket;
- this.contacts = SoapboxHelper.getContacts(ticket);
- }
- public sbxe1__Ticket__c ticket { get; private set; }
- public List<Contact> contacts { get; private set; }
- public boolean hasErrors { get { return contacts.size() != 1; } }
- }
- public static void sendEmailOnErrors(List<sbxe1__Ticket__c> tickets)
- {
- try { SoapBoxTicketAutoMatch.throwOnErrors(trigger.new); }
- catch (SoapboxTicketMatchException xception)
- {
- List<String> errorMessages_noContact = new List<String>();
- List<String> errorMessages_multipleContacts = new List<String>();
- List<String> errorMessages_campaignsWithMultipleContacts = new List<String>();
- {
- //construct error messages
- {
- //match errors
- {
- for (SoapboxTicketAutoMatch.TicketMatcher matcher : xception.matchers)
- {
- if (matcher.contacts.size() == 0)
- {
- errorMessages_noContact.add(
- '<p>No Contact information found for' +
- SoapBoxTicketAutoMatch.createErrorMessageHelper(matcher.ticket) + '</br>' +
- 'Please review the tickets on this <a href=https://ctt.my.salesforce.com/' + matcher.ticket.sbxe1__sbx_Opportunity__c + '>Opportunity.</a>' +
- '</p>');
- }
- else if (matcher.contacts.size() > 1)
- {
- errorMessages_multipleContacts.add(
- '<p>More than one Contact found for' +
- SoapBoxTicketAutoMatch.createErrorMessageHelper(matcher.ticket) +
- '<br>The number of canidate Contacts is ' + matcher.contacts.size() +
- '</p>');
- }
- }
- }
- //campaigns with duplicate contacts
- {
- if (xception.campaignIdsWithDuplicateContacts.size() != 0)
- {
- String errorMessage = '' + system.now() + '<p>Duplicate Registrants detected by Ticket Auto Match. Please review the tickets on the following links';
- for (ID campaignId : xception.campaignIdsWithDuplicateContacts)
- {
- errorMessage += '<br><a href=https://ctt.my.salesforce.com/' + campaignId + '>Campaign</a> and address any duplicates.';
- }
- errorMessage += '</p>';
- errorMessages_campaignsWithMultipleContacts.add(errorMessage);
- }
- }
- }
- }
- sendEmail('No Salesforce Contact information found for ticket', errorMessages_noContact);
- sendEmail('Multiple Salesforce Contacts found for ticket', errorMessages_multipleContacts);
- sendEmail('Duplicate Registrants detected for ticket', errorMessages_campaignsWithMultipleContacts);
- }
- }
- public static void throwOnErrors(List<sbxe1__Ticket__c> tickets)
- {
- system.debug('TicketAutoMatch: Number of tickets to automatch = ' + tickets.size());
- Set<Id> campaignIds = new Set<Id>();
- List<Contact> newTicketContacts = new List<Contact>();
- List<TicketMatcher> matchErrors = new List<TicketMatcher>();
- {
- //check each ticket to ensure there is one contact
- //set the sbxe1__sbx_TicketHolderContact__c field for valid tickets
- for (sbxe1__Ticket__c ticket : tickets)
- {
- Id opportunityId = ticket.sbxe1__sbx_Opportunity__c;
- Id campaignId = ticket.sbxe1__sbx_Campaign__c;
- campaignIds.add(campaignId);
- TicketMatcher matcher = new TicketMatcher(ticket);
- if (matcher.hasErrors) { matchErrors.add(matcher); }
- else
- {
- Contact contact = matcher.contacts[0];
- newTicketContacts.add(contact);
- ticket.sbxe1__sbx_TicketHolderContact__c = contact.ID;
- }
- }
- }
- //doing this check after each ticket has been examined because a field this check
- // uses is set when the tickets are examined
- List<ID> campaignIdsWithDuplicateContacts = new List<ID>();
- {
- //check if any of the registered contacts are in more than one ticket
- for (ID campaignId : campaignIds)
- {
- List<sbxe1__Ticket__c> duplicateContacts =
- [
- select id, sbxe1__sbx_TicketHolderContact__c
- from sbxe1__Ticket__c
- where
- sbxe1__sbx_Campaign__c = :campaignId
- AND sbxe1__sbx_TicketHolderContact__c IN :newTicketContacts
- ];
- if (duplicateContacts.size() != 0) { campaignIdsWithDuplicateContacts.add(campaignId); }
- }
- }
- //now throw an exception if any errors were found
- {
- if ((matchErrors.size() > 0) || (campaignIdsWithDuplicateContacts.size() > 0))
- {
- SoapboxTicketMatchException xception = new SoapboxTicketMatchException();
- {
- //no need to check which of these lists have values, adding no values doesn't hurt
- xception.matchers.addAll(matchErrors);
- xception.campaignIdsWithDuplicateContacts.addAll(campaignIdsWithDuplicateContacts);
- }
- throw xception;
- }
- }
- }
- /**
- * Helper method to construct an error message with data from the ticket
- */
- public static String createErrorMessageHelper(sbxe1__Ticket__c ticket)
- {
- String result = '';
- {
- String firstNameString = ticket.sbxe1__sbx_FirstName__c == null ? '' : ticket.sbxe1__sbx_FirstName__c;
- String lastNameString = ticket.sbxe1__sbx_LastName__c == null ? '' : ticket.sbxe1__sbx_LastName__c;
- String emailString = ticket.sbxe1__sbx_Email__c == null ? '' : ticket.sbxe1__sbx_Email__c;
- String streetString = ticket.sbxe1__sbx_MailingStreet__c == null ? '' : ticket.sbxe1__sbx_MailingStreet__c;
- String cityString = ticket.sbxe1__sbx_MailingCity__c == null ? '' : ticket.sbxe1__sbx_MailingCity__c;
- String stateString = ticket.sbxe1__sbx_MailingState__c == null ? '' : ticket.sbxe1__sbx_MailingState__c;
- String postalCodeString = ticket.sbxe1__sbx_MailingPostalCode__c == null ? '' : ticket.sbxe1__sbx_MailingPostalCode__c;
- result =
- '<br>First Name: ' + firstNameString +
- '<br>Last Name: ' + lastNameString +
- ' <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.' +
- '<br>Email Address: ' + emailString +
- ' <a href=https://ctt.my.salesforce.com/_ui/search/ui/UnifiedSearchResults?searchType=2&str='+emailString+'>Search</a> the org for this email address.' +
- '<br>Mailing Address: ' + streetString + ' ' + cityString + ', ' + stateString + ' ' + postalCodeString;
- }
- return result;
- }
- private static void sendEmail(String subject, List<String> messages)
- {
- if (!(messages.size() > 0)) { return; }
- String emailBody = '<p>An error occurred when matching the soapbox ticket with a contact';
- {
- for (string message : messages)
- {
- emailBody += message + '</br>';
- }
- }
- emailBody += '</p>';
- System.debug('TicketAutoMatch: ' + emailBody);
- //now send an email with the error message
- EmailUtil emails = new EmailUtil();
- {
- emails.createCttEmail(subject, emailBody);
- }
- emails.send();
- }
- }
Add Comment
Please, Sign In to add comment