From 081e1533f5683b29256071990fee700fd59222b9 Mon Sep 17 00:00:00 2001 From: ceiroa Date: Mon, 20 Apr 2015 16:06:22 -0500 Subject: [PATCH] Auto-creating Academic Record for flagged Account record type. --- src/classes/ACCT_IndividualAccounts_TEST.cls | 2 +- src/classes/AFFL_MultiRecordTypeMapper.cls | 42 +++++++++++----- src/classes/AFFL_MultiRecordType_TDTM.cls | 50 ++++++++++++++------ src/classes/AFFL_MultiRecordType_TEST.cls | 48 +++++++++++++++---- src/classes/STG_InstallScript.cls | 4 +- src/classes/TDTM_DefaultConfig.cls | 2 +- 6 files changed, 110 insertions(+), 38 deletions(-) diff --git a/src/classes/ACCT_IndividualAccounts_TEST.cls b/src/classes/ACCT_IndividualAccounts_TEST.cls index b2045d8e9b..132a330cde 100644 --- a/src/classes/ACCT_IndividualAccounts_TEST.cls +++ b/src/classes/ACCT_IndividualAccounts_TEST.cls @@ -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(); diff --git a/src/classes/AFFL_MultiRecordTypeMapper.cls b/src/classes/AFFL_MultiRecordTypeMapper.cls index 7c65489d04..a461356c6b 100644 --- a/src/classes/AFFL_MultiRecordTypeMapper.cls +++ b/src/classes/AFFL_MultiRecordTypeMapper.cls @@ -44,7 +44,12 @@ public with sharing class AFFL_MultiRecordTypeMapper { /******************************************************************************************************* * @description Affiliation Account-record-type-to-Contact-field mappings. ********************************************************************************************************/ - public Map mappingsRecTypesToLabels; + public Map mapAccRecTypeToContactLabel; + + /******************************************************************************************************* + * @description Affiliation Account-record-type-to-Academic-auto-create-field mappings. + ********************************************************************************************************/ + public Map mapAccRecTypeToAcadAutoCreate; /******************************************************************************************************* * @description List of primary affiliation lookup fields names on Contact. @@ -65,11 +70,11 @@ public with sharing class AFFL_MultiRecordTypeMapper { List 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 primaryAfflFieldLabels = mappingsRecTypesToLabels.values(); + List primaryAfflFieldLabels = mapAccRecTypeToContactLabel.values(); UTIL_Debug.debug('****MRT: primaryAfflFieldLabels: ' + JSON.serializePretty(primaryAfflFieldLabels)); //List of primary affiliation Contact fields. @@ -85,17 +90,18 @@ public with sharing class AFFL_MultiRecordTypeMapper { return labelsnames; } - private Map getPrimaryAfflMappings() { - Map mappings = new Map(); - //Get the affiliation Account-record-type-to-Contact-field mappings. - List primaryAfflMappings = [select Account_Record_Type__c, Primary_Affl_Field__c from Affl_Mappings__c]; + private void populateMaps() { + mapAccRecTypeToContactLabel = new Map(); + mapAccRecTypeToAcadAutoCreate = new Map(); + //Get the affiliation mappings. + List 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; } /******************************************************************************************************* @@ -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; + } } \ No newline at end of file diff --git a/src/classes/AFFL_MultiRecordType_TDTM.cls b/src/classes/AFFL_MultiRecordType_TDTM.cls index c98bca41db..258960f462 100644 --- a/src/classes/AFFL_MultiRecordType_TDTM.cls +++ b/src/classes/AFFL_MultiRecordType_TDTM.cls @@ -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. @@ -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 relatedContactsMap; @@ -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. @@ -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]; @@ -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); @@ -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; } @@ -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); + } + } + } } \ No newline at end of file diff --git a/src/classes/AFFL_MultiRecordType_TEST.cls b/src/classes/AFFL_MultiRecordType_TEST.cls index ea30395110..96cb745eaa 100644 --- a/src/classes/AFFL_MultiRecordType_TEST.cls +++ b/src/classes/AFFL_MultiRecordType_TEST.cls @@ -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(); @@ -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 @@ -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(); @@ -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 @@ -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(); @@ -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 @@ -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(); @@ -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(); @@ -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 academicRecords = [select ID from Academic_Record__c where Affiliation__c = :affl.ID]; + System.assertEquals(1, academicRecords.size()); + } } \ No newline at end of file diff --git a/src/classes/STG_InstallScript.cls b/src/classes/STG_InstallScript.cls index 99aad119bc..f594c69878 100644 --- a/src/classes/STG_InstallScript.cls +++ b/src/classes/STG_InstallScript.cls @@ -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; } diff --git a/src/classes/TDTM_DefaultConfig.cls b/src/classes/TDTM_DefaultConfig.cls index 4814dfdb5d..33695fd95a 100644 --- a/src/classes/TDTM_DefaultConfig.cls +++ b/src/classes/TDTM_DefaultConfig.cls @@ -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,