Skip to content

Instantly share code, notes, and snippets.

@evandonovan
Created December 23, 2011 23:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evandonovan/1515657 to your computer and use it in GitHub Desktop.
Save evandonovan/1515657 to your computer and use it in GitHub Desktop.
Assignment of Contacts Class
/* Class */
public with sharing class CityvisionContactAssigner {
static final Id OWNER_NYOUNG;
static final Id OWNER_AWEBB;
static {
List<User> users = [SELECT Id, Alias FROM User WHERE Alias = 'nyoung' OR Alias = 'awebb' OR Alias = 'MOmori' OR Alias = 'ASears'];
for(User u : users) {
// Switch which user to use based on whether we are in production environment, since partner users don't carry across to sandboxes.
if((CityvisionConstants.onProduction == true && u.Alias == 'nyoung')
|| (CityvisionConstants.onProduction == false && u.Alias == 'ASears')) {
OWNER_NYOUNG = u.Id;
}
else if((CityvisionConstants.onProduction == true && u.Alias == 'awebb')
|| (CityvisionConstants.onProduction == false && u.Alias == 'MOmori')) {
OWNER_AWEBB = u.Id;
}
}
}
// Assign everyone in the first half of the alphabet to Angela, the rest to Nancy.
public static void assignContacts(List<Contact> pContacts) {
for (Contact c : pContacts) {
// Only do the assignment for Contacts that would get a City Vision Status.
if(CityvisionUtils.hasCityvisionStatus(c)) {
String lastName = (c.LastName != null) ? c.LastName : '';
String firstChar = lastName.substring(0, 1).toLowerCase();
String splitPoint = 'm';
Integer alphaPos = firstChar.compareTo(splitPoint);
if(alphaPos <= 0) {
c.OwnerId = OWNER_AWEBB;
}
else {
c.OwnerId = OWNER_NYOUNG;
}
}
}
}
}
/* Test */
@isTest
private class TestCityvisionContactAssigner {
static Account testAcct;
static Contact testContact;
static Contact testContact_Updated;
static final Id OWNER_NYOUNG;
static final Id OWNER_AWEBB;
static final Id OWNER_EDONOVAN;
// initialize to have a date to use for the date fields
static Datetime t = System.now();
static Date d = Date.newInstance(t.year(),t.month(),t.day());
static {
List<User> users = [SELECT Id, Alias FROM User WHERE Alias = 'nyoung' OR Alias = 'awebb' OR Alias = 'MOmori' OR Alias = 'ASears' OR Alias = 'EDonovan'];
for(User u : users) {
// Switch which user to use based on whether we are in production environment, since partner users don't carry across to sandboxes.
if((CityvisionConstants.onProduction == true && u.Alias == 'nyoung')
|| (CityvisionConstants.onProduction == false && u.Alias == 'ASears')) {
OWNER_NYOUNG = u.Id;
}
else if((CityvisionConstants.onProduction == true && u.Alias == 'awebb')
|| (CityvisionConstants.onProduction == false && u.Alias == 'MOmori')) {
OWNER_AWEBB = u.Id;
}
else if(u.Alias == 'EDonovan') {
OWNER_EDONOVAN = u.Id;
}
}
testAcct = new Account(name='Test Org');
insert testAcct;
}
static void initContact_pos(String pName) {
testContact = new Contact(FirstName = 'example', LastName = pName,
AccountId = testAcct.Id, City_Vision_Terms_and_Conditions_Date__c = d);
insert testContact;
}
static void initContact_neg(String pName) {
testContact = new Contact(FirstName = 'example', LastName = pName,
AccountId = testAcct.Id, OwnerId = OWNER_EDONOVAN);
insert testContact;
}
/* Positive path. */
// Case 1: Name before M - Angela.
static testMethod void assignContact_case1() {
initContact_pos('Alpha');
Test.startTest();
Test.stopTest();
testContact_updated = [SELECT Id, OwnerId FROM Contact WHERE Id = :testContact.Id];
System.assertEquals(OWNER_AWEBB, testContact_Updated.OwnerId, 'Before M - assigned to Angela');
}
// Case 2: Name beginning with M - Angela.
static testMethod void assignContact_case2() {
initContact_pos('Maxine');
Test.startTest();
Test.stopTest();
testContact_updated = [SELECT Id, OwnerId FROM Contact WHERE Id = :testContact.Id];
System.assertEquals(OWNER_AWEBB, testContact_Updated.OwnerId, 'M - assigned to Angela');
}
// Case 3: Name beginning with N - Nancy.
static testMethod void assignContact_case3() {
initContact_pos('Neutral');
Test.startTest();
Test.stopTest();
testContact_updated = [SELECT Id, OwnerId FROM Contact WHERE Id = :testContact.Id];
System.assertEquals(OWNER_NYOUNG, testContact_Updated.OwnerId, 'N - assigned to Nancy');
}
/* Negative path. */
// Case 4: Not a City Vision Contact.
static testMethod void assignContact_case4_neg() {
initContact_neg('Alpha');
Test.startTest();
Test.stopTest();
testContact_updated = [SELECT Id, OwnerId FROM Contact WHERE Id = :testContact.Id];
System.assertEquals(OWNER_EDONOVAN, testContact_Updated.OwnerId, 'not a City Vision Contact - assignment not switched');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment