Skip to content

Commit

Permalink
Auto-creating Academic Record for flagged Account record type.
Browse files Browse the repository at this point in the history
  • Loading branch information
ceiroa committed Apr 20, 2015
1 parent 7034bd3 commit 081e153
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/classes/ACCT_IndividualAccounts_TEST.cls
Expand Up @@ -203,7 +203,7 @@ private class ACCT_IndividualAccounts_TEST {
Contact con2 = new Contact(FirstName='John', LastName='Smith', AccountId = accountId);

Test.startTest();
AFFL_MultiRecordType_TDTM.afflMultiHasRun = false;
AFFL_MultiRecordType_TDTM.afflMultiHasRunBefore = false;
insert con2;
Test.stopTest();

Expand Down
42 changes: 30 additions & 12 deletions src/classes/AFFL_MultiRecordTypeMapper.cls
Expand Up @@ -44,7 +44,12 @@ public with sharing class AFFL_MultiRecordTypeMapper {
/*******************************************************************************************************
* @description Affiliation Account-record-type-to-Contact-field mappings.
********************************************************************************************************/
public Map<String, String> mappingsRecTypesToLabels;
public Map<String, String> mapAccRecTypeToContactLabel;

/*******************************************************************************************************
* @description Affiliation Account-record-type-to-Academic-auto-create-field mappings.
********************************************************************************************************/
public Map<String, Boolean> mapAccRecTypeToAcadAutoCreate;

/*******************************************************************************************************
* @description List of primary affiliation lookup fields names on Contact.
Expand All @@ -65,11 +70,11 @@ public with sharing class AFFL_MultiRecordTypeMapper {
List<String> contactFieldNames = contactLabelNames.values();

//Affiliation Account-record-type-to-Contact-field mappings.
mappingsRecTypesToLabels = getPrimaryAfflMappings();
UTIL_Debug.debug('****MRT: primaryAfflMappings: ' + JSON.serializePretty(mappingsRecTypesToLabels));
populateMaps();
UTIL_Debug.debug('****MRT: primaryAfflMappings: ' + JSON.serializePretty(mapAccRecTypeToContactLabel));

//List of primary affiliation lookup fields labels.
List<String> primaryAfflFieldLabels = mappingsRecTypesToLabels.values();
List<String> primaryAfflFieldLabels = mapAccRecTypeToContactLabel.values();
UTIL_Debug.debug('****MRT: primaryAfflFieldLabels: ' + JSON.serializePretty(primaryAfflFieldLabels));

//List of primary affiliation Contact fields.
Expand All @@ -85,17 +90,18 @@ public with sharing class AFFL_MultiRecordTypeMapper {
return labelsnames;
}

private Map<String, String> getPrimaryAfflMappings() {
Map<String, String> mappings = new Map<String, String>();
//Get the affiliation Account-record-type-to-Contact-field mappings.
List<Affl_Mappings__c> primaryAfflMappings = [select Account_Record_Type__c, Primary_Affl_Field__c from Affl_Mappings__c];
private void populateMaps() {
mapAccRecTypeToContactLabel = new Map<String, String>();
mapAccRecTypeToAcadAutoCreate = new Map<String, Boolean>();
//Get the affiliation mappings.
List<Affl_Mappings__c> primaryAfflMappings = [select Account_Record_Type__c, Primary_Affl_Field__c, Auto_Academic_Record__c from Affl_Mappings__c];
//Put them in a map.
for(Affl_Mappings__c mapping : primaryAfflMappings) {
if(!String.isBlank(mapping.Account_Record_Type__c) && !String.isBlank(mapping.Primary_Affl_Field__c)) {
mappings.put(mapping.Account_Record_Type__c, mapping.Primary_Affl_Field__c);
mapAccRecTypeToContactLabel.put(mapping.Account_Record_Type__c, mapping.Primary_Affl_Field__c);
mapAccRecTypeToAcadAutoCreate.put(mapping.Account_Record_Type__c, mapping.Auto_Academic_Record__c);
}
}
return mappings;
}

/*******************************************************************************************************
Expand Down Expand Up @@ -123,14 +129,26 @@ public with sharing class AFFL_MultiRecordTypeMapper {
* @return String The name of the key affiliation Contact field.
********************************************************************************************************/
public String getKeyAfflFieldByAccRecordType(ID recordTypeId) {
for(String recTypeName : mappingsRecTypesToLabels.keySet()) {
for(String recTypeName : mapAccRecTypeToContactLabel.keySet()) {
ID rcId = Schema.Sobjecttype.Account.getRecordTypeInfosByName().get(recTypeName).getRecordTypeId();
if(rcId == recordTypeId) {
String fieldLabel = mappingsRecTypesToLabels.get(recTypeName);
String fieldLabel = mapAccRecTypeToContactLabel.get(recTypeName);
String fieldName = contactLabelNames.get(fieldLabel);
return fieldName;
}
}
return null;
}

/*******************************************************************************************************
* @description Tells caller if Account record type needs Academic Record auto-creation.
* @param accountRecordType The name of the Account record type as entered in the Affiliation Mappings.
* @return Boolean Whether the Account record type has been flagged as needing Academic Record auto-creation.
********************************************************************************************************/
public Boolean needsAcademicRecord(String accountRecordType) {
if(accountRecordType != null) {
return mapAccRecTypeToAcadAutoCreate.get(accountRecordType);
}
return false;
}
}
50 changes: 36 additions & 14 deletions src/classes/AFFL_MultiRecordType_TDTM.cls
Expand Up @@ -38,8 +38,9 @@ public with sharing class AFFL_MultiRecordType_TDTM extends TDTM_Runnable {
/*******************************************************************************************************
* @description Static flags to prevent recursive call.
********************************************************************************************************/
public static boolean afflMultiHasRun = false;

public static boolean afflMultiHasRunBefore = false;
public static boolean afflMultiHasRunAfter = false;

/*******************************************************************************************************
* @description Handles Affiliation management.
* @param listNew the list of Accounts from trigger new.
Expand All @@ -52,10 +53,9 @@ public with sharing class AFFL_MultiRecordType_TDTM extends TDTM_Runnable {
TDTM_Runnable.Action triggerAction, Schema.DescribeSObjectResult objResult) {

DmlWrapper dmlWrapper = new DmlWrapper();

if(!afflMultiHasRun) {

AFFL_MultiRecordTypeMapper afflMapper = new AFFL_MultiRecordTypeMapper();
AFFL_MultiRecordTypeMapper afflMapper = new AFFL_MultiRecordTypeMapper();

if(!afflMultiHasRunBefore) {

//Query all the primary affiliation lookup fields on the contact - they are not available in the trigger.
Map<ID, Contact> relatedContactsMap;
Expand All @@ -64,12 +64,12 @@ public with sharing class AFFL_MultiRecordType_TDTM extends TDTM_Runnable {
} else if(oldlist != null) {
relatedContactsMap = queryAfflLookupFields(oldlist, afflMapper);
}
if(newlist != null && newlist.size() > 0) {

if(newlist != null && newlist.size() > 0) {
Integer i = 0;
for (SObject so : newlist) {
Affiliation__c affl = (Affiliation__c)so;
String lookupFieldLabel = afflMapper.mappingsRecTypesToLabels.get(affl.Affiliation_Type__c);
String lookupFieldLabel = afflMapper.mapAccRecTypeToContactLabel.get(affl.Affiliation_Type__c);
String lookupFieldName = afflMapper.contactLabelNames.get(lookupFieldLabel);

//Query all the primary affiliation lookup fields on the contact - they are not available in the trigger.
Expand All @@ -80,7 +80,7 @@ public with sharing class AFFL_MultiRecordType_TDTM extends TDTM_Runnable {
if (triggerAction == TDTM_Runnable.Action.BeforeInsert) {
uncheckOtherPrimariesPopulateKeyAffls(affl, lookupFieldName, relatedContact, dmlWrapper);
}

// AFTER UPDATE
if (triggerAction == TDTM_Runnable.Action.AfterUpdate) {
Affiliation__c afflOld = (Affiliation__c)oldlist[i];
Expand All @@ -100,11 +100,11 @@ public with sharing class AFFL_MultiRecordType_TDTM extends TDTM_Runnable {
i++;
}
}

if(oldlist != null && oldlist.size() > 0) {
for(SObject so : oldlist) {
Affiliation__c afflOld = (Affiliation__c)so;
String lookupFieldLabel = afflMapper.mappingsRecTypesToLabels.get(afflOld.Affiliation_Type__c);
String lookupFieldLabel = afflMapper.mapAccRecTypeToContactLabel.get(afflOld.Affiliation_Type__c);
String lookupFieldName = afflMapper.contactLabelNames.get(lookupFieldLabel);

Contact relatedContact = relatedContactsMap.get(afflOld.Contact__c);
Expand All @@ -119,8 +119,20 @@ public with sharing class AFFL_MultiRecordType_TDTM extends TDTM_Runnable {
}
}
}
afflMultiHasRun = true;
}
afflMultiHasRunBefore = true;
}

//AFTER INSERT
if(!afflMultiHasRunAfter && triggerAction == TDTM_Runnable.Action.AfterInsert) {
if(newlist != null && newlist.size() > 0) {
for (SObject so : newlist) {
Affiliation__c affl = (Affiliation__c)so;
createAcademicRecordIfNecessary(affl, dmlWrapper, afflMapper);
}
}
afflMultiHasRunAfter = true;
}

return dmlWrapper;
}

Expand Down Expand Up @@ -200,4 +212,14 @@ public with sharing class AFFL_MultiRecordType_TDTM extends TDTM_Runnable {

return oldLookupsSameType.size();
}

private void createAcademicRecordIfNecessary(Affiliation__c affl, DmlWrapper dmlWrapper, AFFL_MultiRecordTypeMapper afflMapper) {
if(affl != null && affl.Affiliation_Type__c != null) {
Boolean needsAcademicRecord = afflMapper.needsAcademicRecord(affl.Affiliation_Type__c);
if(needsAcademicRecord != null && needsAcademicRecord) {
Academic_Record__c acadRec = new Academic_Record__c(Affiliation__c = affl.ID);
dmlWrapper.objectsToInsert.add(acadRec);
}
}
}
}
48 changes: 40 additions & 8 deletions src/classes/AFFL_MultiRecordType_TEST.cls
Expand Up @@ -85,7 +85,7 @@ public with sharing class AFFL_MultiRecordType_TEST {

//Create abother key affiliation, this time to a biz org
Affiliation__c bizAffl1 = new Affiliation__c(Contact__c = contact.ID, Organization__c = bizOrg1.ID, Primary__c = true);
AFFL_MultiRecordType_TDTM.afflMultiHasRun = false;
AFFL_MultiRecordType_TDTM.afflMultiHasRunBefore = false;
Test.startTest();
insert bizAffl1;
Test.stopTest();
Expand All @@ -103,7 +103,7 @@ public with sharing class AFFL_MultiRecordType_TEST {

//Create second primary affiliation
Affiliation__c bizAffl2 = new Affiliation__c(Contact__c = contact.ID, Organization__c = bizOrg2.ID, Primary__c = true);
AFFL_MultiRecordType_TDTM.afflMultiHasRun = false;
AFFL_MultiRecordType_TDTM.afflMultiHasRunBefore = false;
insert bizAffl2;

//The previous affiliation should not be the primary any more
Expand Down Expand Up @@ -144,7 +144,7 @@ public with sharing class AFFL_MultiRecordType_TEST {
//Create primary Business affiliation
Affiliation__c affl1 = new Affiliation__c(Contact__c = contact.ID, Organization__c = acc1.ID, Primary__c = true);
Test.startTest();
AFFL_MultiRecordType_TDTM.afflMultiHasRun = false;
AFFL_MultiRecordType_TDTM.afflMultiHasRunBefore = false;
insert affl1;
Test.stopTest();

Expand All @@ -153,7 +153,7 @@ public with sharing class AFFL_MultiRecordType_TEST {
System.assertEquals(acc1.ID, Contact.Primary_Organization__c);

//Delete Business affiliation
AFFL_MultiRecordType_TDTM.afflMultiHasRun = false;
AFFL_MultiRecordType_TDTM.afflMultiHasRunBefore = false;
delete affl1;

//Verify Primary Organization lookup field has been cleared
Expand Down Expand Up @@ -188,7 +188,7 @@ public with sharing class AFFL_MultiRecordType_TEST {
//Make the affiliation not primary
affls[0].Primary__c = false;
Test.startTest();
AFFL_MultiRecordType_TDTM.afflMultiHasRun = false;
AFFL_MultiRecordType_TDTM.afflMultiHasRunBefore = false;
update affls[0];
Test.stopTest();

Expand All @@ -215,7 +215,7 @@ public with sharing class AFFL_MultiRecordType_TEST {

//Create non-primary Business affiliation
Affiliation__c affl1 = new Affiliation__c(Contact__c = contact.ID, Organization__c = acc1.ID, Primary__c = false);
AFFL_MultiRecordType_TDTM.afflMultiHasRun = false;
AFFL_MultiRecordType_TDTM.afflMultiHasRunBefore = false;
insert affl1;

//Verify Primary Organization lookup field is blank
Expand All @@ -224,7 +224,7 @@ public with sharing class AFFL_MultiRecordType_TEST {

//Made the business affiliation primary
affl1.Primary__c = true;
AFFL_MultiRecordType_TDTM.afflMultiHasRun = false;
AFFL_MultiRecordType_TDTM.afflMultiHasRunBefore = false;
Test.startTest();
update affl1;
Test.stopTest();
Expand Down Expand Up @@ -254,7 +254,7 @@ public with sharing class AFFL_MultiRecordType_TEST {
//Change the account record type to Business Organization
acc.RecordTypeId = orgRecTypeID;
Test.startTest();
AFFL_MultiRecordType_TDTM.afflMultiHasRun = false;
AFFL_MultiRecordType_TDTM.afflMultiHasRunBefore = false;
update acc;
Test.stopTest();

Expand All @@ -264,4 +264,36 @@ public with sharing class AFFL_MultiRecordType_TEST {
//Verify primary business organization field has been populated
System.assertEquals(acc.ID, contact.Primary_Organization__c);
}

@isTest
public static void autoCreateAcademicRecord() {
if (strTestOnly != '*' && strTestOnly != 'autoCreateAcademicRecord') return;

setup();

//Turn on Academic Record auto-creation for Business Organization mapping - doesn't make a lot of sense for this type, but just for testing
Affl_Mappings__c bizOrgMapping = [select Auto_Academic_Record__c from Affl_Mappings__c where Name = 'Business Organization'];
bizOrgMapping.Auto_Academic_Record__c = true;
update bizOrgMapping;

Contact contact = new Contact(FirstName = 'Test', LastName = 'Testerson');
insert contact;

//Craete account of Business Organization record type
Account acc = new Account(Name='Acme', RecordTypeId = orgRecTypeID);
insert acc;

//Create Business affiliation
Affiliation__c affl = new Affiliation__c(Contact__c = contact.ID, Organization__c = acc.ID);
Test.startTest();
UTIL_Debug.debug('****Creating affiliation that should auto-create Academic Record');
AFFL_MultiRecordType_TDTM.afflMultiHasRunBefore = false;
AFFL_MultiRecordType_TDTM.afflMultiHasRunAfter = false;
insert affl;
Test.stopTest();

//Verify Academic Record has been created
List<Academic_Record__c> academicRecords = [select ID from Academic_Record__c where Affiliation__c = :affl.ID];
System.assertEquals(1, academicRecords.size());
}
}
4 changes: 2 additions & 2 deletions src/classes/STG_InstallScript.cls
Expand Up @@ -56,8 +56,8 @@ global without sharing class STG_InstallScript implements InstallHandler {
mappings.add(new Affl_Mappings__c(Name = 'Administrative', Account_Record_Type__c = 'Administrative', Primary_Affl_Field__c = 'Primary Administrative Account'));
mappings.add(new Affl_Mappings__c(Name = 'Business Organization', Account_Record_Type__c = 'Business Organization', Primary_Affl_Field__c = 'Primary Business Organization'));
mappings.add(new Affl_Mappings__c(Name = 'Household Account', Account_Record_Type__c = 'Household Account', Primary_Affl_Field__c = 'Primary Household'));
mappings.add(new Affl_Mappings__c(Name = 'Educational Institution', Account_Record_Type__c = 'Educational Institution', Primary_Affl_Field__c = 'Primary Educational Institution'));
mappings.add(new Affl_Mappings__c(Name = 'Department', Account_Record_Type__c = 'University Department', Primary_Affl_Field__c = 'Primary Department'));
mappings.add(new Affl_Mappings__c(Name = 'Educational Institution', Account_Record_Type__c = 'Educational Institution', Primary_Affl_Field__c = 'Primary Educational Institution', Auto_Academic_Record__c = true));
mappings.add(new Affl_Mappings__c(Name = 'Department', Account_Record_Type__c = 'University Department', Primary_Affl_Field__c = 'Primary Department', Auto_Academic_Record__c = true));
mappings.add(new Affl_Mappings__c(Name = 'Sports', Account_Record_Type__c = 'Sports Organization', Primary_Affl_Field__c = 'Primary Sports Organization'));
insert mappings;
}
Expand Down
2 changes: 1 addition & 1 deletion src/classes/TDTM_DefaultConfig.cls
Expand Up @@ -55,7 +55,7 @@ public without sharing class TDTM_DefaultConfig {
//Multi-Affiliation Type support on Affiliations - populates primary affl fields
handlers.add(new Trigger_Handler__c(Active__c = true, Asynchronous__c = false,
Class__c = 'AFFL_MultiRecordType_TDTM', Load_Order__c = 2, Object__c = 'Affiliation__c',
Trigger_Action__c = 'BeforeInsert;AfterUpdate;AfterDelete'));
Trigger_Action__c = 'BeforeInsert;AfterInsert;AfterUpdate;AfterDelete'));

//Multi-Affiliation Type support on Account - supports account record type change
handlers.add(new Trigger_Handler__c(Active__c = true, Asynchronous__c = false,
Expand Down

0 comments on commit 081e153

Please sign in to comment.