Skip to content

Commit

Permalink
Merge pull request #58 from SalesforceFoundation/feature/7597-enhance…
Browse files Browse the repository at this point in the history
…d-tdtm-filtering

Feature/7597 enhanced tdtm filtering
  • Loading branch information
ceiroa committed Aug 20, 2015
2 parents cd29542 + e69dd81 commit 65fd4dc
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 8 deletions.
25 changes: 19 additions & 6 deletions src/classes/TDTM_Filter.cls
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
* Those that do are taken out from the list of records to process.
*/
public with sharing class TDTM_Filter {


/* @description The name of the class being run.*/
String classToRunName;
/* @description An instance of the class being run.*/
SObject classToRunRecord;
/* * @description The records that were passed to the trigger as trigger.new. */
Expand Down Expand Up @@ -63,8 +65,9 @@ public with sharing class TDTM_Filter {
* @param oldList The records that were passed to the trigger as trigger.old.
* @param describeObj The type of SObject the class runs for.
*/
public TDTM_Filter(SObject classToRunRecord, List<SObject> newList, List<SObject> oldList,
public TDTM_Filter(String classToRunName, SObject classToRunRecord, List<SObject> newList, List<SObject> oldList,
DescribeSObjectResult describeObj) {
this.classToRunName = classToRunName;
this.classToRunRecord = classToRunRecord;
this.newList = newList;
this.oldList = oldList;
Expand All @@ -90,7 +93,6 @@ public with sharing class TDTM_Filter {
return filtered;
}
} catch(Exception e) {
UTIL_Debug.debug(LoggingLevel.WARN, '****Invalid filtering condition');
UTIL_Debug.debug(LoggingLevel.WARN, '****Exception: ' + e.getMessage());
UTIL_Debug.debug(LoggingLevel.WARN, '\n****Stack Trace:\n' + e.getStackTraceString() + '\n');
}
Expand All @@ -108,7 +110,7 @@ public with sharing class TDTM_Filter {
List<String> filterFullChain = (filterField.split('\\.', 0)); //separate cross object references, i.e. account.name
fieldName = filterFullChain[filterFullChain.size() - 1]; //get the field name itself
String parentObjectName = filterFullChain[filterFullChain.size() - 2]; //get the name of the field parent = last object in the chain
UTIL_Debug.debug('****filterFullChain: ' + filterFullChain);
UTIL_Debug.debug('****filterFullChain: ' + filterFullChain);

//remove the field, to have only the parent object chain
for(Integer i = 0; i < (filterFullChain.size() - 1); i++)
Expand All @@ -127,10 +129,21 @@ public with sharing class TDTM_Filter {
UTIL_Debug.debug('****Filter value: ' + filterValue);
filterByCondition(newListRelatedFields, oldListRelatedFields);
} else {
UTIL_Debug.debug('****The field name is invalid.');
addErrorToAll();
}
}

/*******************************************************************************************************
* @description Adds an error message to all the records in the trigger.
* @return void
*/
private void addErrorToAll() {
for(Integer i = 0; i < newList.size(); i++) {
newList[i].addError(Label.InvalidFilter + ' ' + classToRunName);
filtered.newList.add(newList[i]);
}
}

/*******************************************************************************************************
* @description We need the SObjectField to know the type of the filter field and determine if any manipulation
* is necessary. All the filter conditions are stored as strings, but some many need to be transformed to
Expand Down Expand Up @@ -313,7 +326,7 @@ public with sharing class TDTM_Filter {
UTIL_Debug.debug('****Filter value: ' + filterValue);
filterByCondition(null, null);
} else {
UTIL_Debug.debug('****The field name is invalid.');
addErrorToAll();
}
}

Expand Down
27 changes: 27 additions & 0 deletions src/classes/TDTM_Filter_TEST.cls
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,33 @@ public with sharing class TDTM_Filter_TEST {
Relationship__c[] rels = [select Contact__c, RelatedContact__c from Relationship__c];
System.assertEquals(0, rels.size());
}

public static testmethod void InvalidFilterField() {
if (strTestOnly != '*' && strTestOnly != 'InvalidFilterField') return;

insert new Relationship_Auto_Create__c(Name='AutoCreate2',Object__c='Contact',
Field__c='ReportsToId', Relationship_Type__c = 'TestType');

//Creating filter condition.
insert new Trigger_Handler__c(Active__c = true, Asynchronous__c = false,
Class__c = 'REL_Relationships_Con_TDTM', Load_Order__c = 1, Object__c = 'Contact',
Trigger_Action__c = 'AfterInsert;AfterUpdate;AfterDelete', Filter_Field__c = 'InvalidField',
Filter_Value__c = 'Anne');

//Creating four contacts. The second one meets the filtering criteria.
Contact c1 = new Contact(FirstName = 'Test', LastName = 'Testerson1', AssistantName = 'Nancy');
Contact c2 = new Contact(FirstName = 'Test', LastName = 'Testerson2', AssistantName = 'Anne');
Contact c3 = new Contact(FirstName = 'Test', LastName = 'Testerson3', AssistantName = null);
Contact c4 = new Contact(FirstName = 'Test', LastName = 'Testerson4', AssistantName = 'John');
Contact[] contacts = new Contact[] {c1, c2, c3, c4};
try {
insert contacts;
System.assertEquals('We should never get here.', 'We got here.');
} catch(System.DmlException e) {
System.assertEquals(true, e.getMessage().contains('FIELD_CUSTOM_VALIDATION_EXCEPTION'));
System.assertEquals(true, e.getMessage().contains(Label.InvalidFilter));
}
}

public static testmethod void checkboxField() {
if (strTestOnly != '*' && strTestOnly != 'checkboxField') return;
Expand Down
3 changes: 1 addition & 2 deletions src/classes/TDTM_TriggerHandler.cls
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,10 @@ public with sharing class TDTM_TriggerHandler {
} else {
UTIL_Debug.debugWithInfo('****Calling synchronously: ' + classToRunName);

TDTM_Filter filter = new TDTM_Filter(classToRunRecord, newList, oldList, describeObj);
TDTM_Filter filter = new TDTM_Filter(classToRunName, classToRunRecord, newList, oldList, describeObj);
TDTM_Filter.FilteredLists filtered = filter.filter();

if(filtered != null) {
UTIL_Debug.debug('****newList after filtering: ' + JSON.serializePretty(filtered.newList));
return classToRun.run(filtered.newList, filtered.oldList, thisAction, describeObj);
} else {
return classToRun.run(newList, oldList, thisAction, describeObj);
Expand Down
8 changes: 8 additions & 0 deletions src/labels/CustomLabels.labels
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
<shortDescription>Female</shortDescription>
<value>Female</value>
</labels>
<labels>
<fullName>InvalidFilter</fullName>
<categories>TDTM</categories>
<language>en_US</language>
<protected>false</protected>
<shortDescription>Error message for TDTM filtering functionality.</shortDescription>
<value>Invalid filtering field name defined on </value>
</labels>
<labels>
<fullName>Male</fullName>
<categories>Relationships</categories>
Expand Down
1 change: 1 addition & 0 deletions src/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@
<members>DefaultAdminName</members>
<members>DefaultHouseholdName</members>
<members>Female</members>
<members>InvalidFilter</members>
<members>Male</members>
<members>Relationship_Explanation_Connector</members>
<members>Relationship_Split</members>
Expand Down

0 comments on commit 65fd4dc

Please sign in to comment.