diff --git a/src/classes/TDTM_Filter.cls b/src/classes/TDTM_Filter.cls index d1ebbcdc04..dd0286621a 100644 --- a/src/classes/TDTM_Filter.cls +++ b/src/classes/TDTM_Filter.cls @@ -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. */ @@ -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 newList, List oldList, + public TDTM_Filter(String classToRunName, SObject classToRunRecord, List newList, List oldList, DescribeSObjectResult describeObj) { + this.classToRunName = classToRunName; this.classToRunRecord = classToRunRecord; this.newList = newList; this.oldList = oldList; @@ -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'); } @@ -108,7 +110,7 @@ public with sharing class TDTM_Filter { List 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++) @@ -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 @@ -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(); } } diff --git a/src/classes/TDTM_Filter_TEST.cls b/src/classes/TDTM_Filter_TEST.cls index 68dddb9773..0a5c174337 100644 --- a/src/classes/TDTM_Filter_TEST.cls +++ b/src/classes/TDTM_Filter_TEST.cls @@ -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; diff --git a/src/classes/TDTM_TriggerHandler.cls b/src/classes/TDTM_TriggerHandler.cls index 2023032411..6b8d125e9c 100644 --- a/src/classes/TDTM_TriggerHandler.cls +++ b/src/classes/TDTM_TriggerHandler.cls @@ -177,7 +177,7 @@ 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) { diff --git a/src/labels/CustomLabels.labels b/src/labels/CustomLabels.labels index c99d6e6a9e..cdca33fe3f 100644 --- a/src/labels/CustomLabels.labels +++ b/src/labels/CustomLabels.labels @@ -32,6 +32,14 @@ Female Female + + InvalidFilter + TDTM + en_US + false + Error message for TDTM filtering functionality. + Invalid filtering field name defined on + Male Relationships diff --git a/src/package.xml b/src/package.xml index 015338c724..a31d7a5f89 100644 --- a/src/package.xml +++ b/src/package.xml @@ -232,6 +232,7 @@ DefaultAdminName DefaultHouseholdName Female + InvalidFilter Male Relationship_Explanation_Connector Relationship_Split