Skip to content

Commit 41a54d9

Browse files
committed
Update TriggerActionFlow with additional error handling and validation messages for flow type checks.
1 parent f7c8caa commit 41a54d9

File tree

2 files changed

+77
-15
lines changed

2 files changed

+77
-15
lines changed

trigger-actions-framework/main/default/classes/TriggerActionFlow.cls

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,24 @@
1919
* @description This class implements the TriggerAction interface and provides a framework for
2020
* executing Flows before or after the insert, update, delete, or undelete of records.
2121
*/
22-
@SuppressWarnings('PMD.CyclomaticComplexity')
22+
@SuppressWarnings('PMD.CyclomaticComplexity, PMD.CognitiveComplexity')
2323
public virtual inherited sharing class TriggerActionFlow implements TriggerAction.BeforeInsert, TriggerAction.AfterInsert, TriggerAction.BeforeUpdate, TriggerAction.AfterUpdate, TriggerAction.BeforeDelete, TriggerAction.AfterDelete, TriggerAction.AfterUndelete {
2424
@TestVisible
2525
private static final String RECORD_VARIABLE_NOT_FOUND_ERROR = 'There must be a variable defined in this flow with api name of "record" and type of "record" that is marked as "available for output"';
2626
@TestVisible
2727
private static final String FLOW_INTERVIEW_PREFIX = 'Flow.Interview';
2828
@TestVisible
29+
private static final String TYPE_MUST_BE_PROVIDED_ERROR = 'Type must be provided to get the flow name.';
30+
@TestVisible
31+
private static final String TYPE_MUST_REPRESENT_FLOW_ERROR = 'Type must represent a Flow (e.g., Flow.Interview.MyFlow.class), but got: ';
32+
@TestVisible
33+
private static final String FLOW_DOES_NOT_EXIST_ERROR = 'Flow does not exist. Please check that the flow API name is correct.';
34+
@TestVisible
2935
private static Set<String> bypassedFlows = new Set<String>();
3036
@TestVisible
3137
private static InvocableAction invocableAction = new InvocableAction();
3238
@TestVisible
33-
private static NameExtractor nameExtractor = new NameExtractor();
39+
private static TriggerActionFlow.NameExtractor nameExtractor = new TriggerActionFlow.NameExtractor();
3440

3541
public String flowName;
3642
public Boolean allowRecursion;
@@ -112,18 +118,18 @@ public virtual inherited sharing class TriggerActionFlow implements TriggerActio
112118
* @throws IllegalArgumentException if the type does not represent a Flow.
113119
*/
114120
private static String getFlowNameFromType(System.Type flowType) {
121+
if (flowType == null) {
122+
throw new IllegalArgumentException(TYPE_MUST_BE_PROVIDED_ERROR);
123+
}
115124
String typeName = nameExtractor.extractName(flowType);
116-
if (!typeName.startsWith(FLOW_INTERVIEW_PREFIX)) {
125+
if (typeName == null || !typeName.startsWith(FLOW_INTERVIEW_PREFIX)) {
117126
throw new IllegalArgumentException(
118-
'Type must represent a Flow (e.g., Flow.Interview.MyFlow.class), but got: ' +
119-
typeName
127+
TYPE_MUST_REPRESENT_FLOW_ERROR + typeName
120128
);
121129
}
122130
// If the type name is exactly "Flow.Interview", it means the flow doesn't exist
123131
if (typeName.equals(FLOW_INTERVIEW_PREFIX)) {
124-
throw new IllegalArgumentException(
125-
'Flow does not exist. Please check that the flow API name is correct.'
126-
);
132+
throw new IllegalArgumentException(FLOW_DOES_NOT_EXIST_ERROR);
127133
}
128134
return typeName.replace(FLOW_INTERVIEW_PREFIX + '.', '');
129135
}

trigger-actions-framework/main/default/classes/TriggerActionFlowTest.cls

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
limitations under the License.
1515
*/
1616

17-
@SuppressWarnings('PMD.ApexDoc, PMD.ApexUnitTestClassShouldHaveRunAs')
17+
@SuppressWarnings(
18+
'PMD.ApexDoc, PMD.ApexUnitTestClassShouldHaveRunAs, PMD.CyclomaticComplexity'
19+
)
1820
@IsTest(isParallel=true)
1921
private class TriggerActionFlowTest {
2022
private static final String BOGUS = 'Bogus';
@@ -89,8 +91,7 @@ private class TriggerActionFlowTest {
8991
myException = e;
9092
}
9193

92-
System.Assert.areEqual(
93-
null,
94+
System.Assert.isNull(
9495
myException,
9596
'There should be no exception thrown and the System should do nothing when the flow is bypassed'
9697
);
@@ -416,7 +417,8 @@ private class TriggerActionFlowTest {
416417
System.Assert.areNotEqual(null, myException, EXCEPTION_SHOULD_BE_THROWN);
417418
System.Assert.areEqual(
418419
true,
419-
myException.getMessage().contains('Type must represent a Flow'),
420+
myException.getMessage()
421+
.contains(TriggerActionFlow.TYPE_MUST_REPRESENT_FLOW_ERROR),
420422
EXCEPTION_SHOULD_HAVE_THE_CORRECT_MESSAGE
421423
);
422424
}
@@ -432,7 +434,8 @@ private class TriggerActionFlowTest {
432434
System.Assert.areNotEqual(null, myException, EXCEPTION_SHOULD_BE_THROWN);
433435
System.Assert.areEqual(
434436
true,
435-
myException.getMessage().contains('Type must represent a Flow'),
437+
myException.getMessage()
438+
.contains(TriggerActionFlow.TYPE_MUST_REPRESENT_FLOW_ERROR),
436439
EXCEPTION_SHOULD_HAVE_THE_CORRECT_MESSAGE
437440
);
438441
}
@@ -448,7 +451,8 @@ private class TriggerActionFlowTest {
448451
System.Assert.areNotEqual(null, myException, EXCEPTION_SHOULD_BE_THROWN);
449452
System.Assert.areEqual(
450453
true,
451-
myException.getMessage().contains('Type must represent a Flow'),
454+
myException.getMessage()
455+
.contains(TriggerActionFlow.TYPE_MUST_REPRESENT_FLOW_ERROR),
452456
EXCEPTION_SHOULD_HAVE_THE_CORRECT_MESSAGE
453457
);
454458
}
@@ -466,7 +470,59 @@ private class TriggerActionFlowTest {
466470
System.Assert.areNotEqual(null, myException, EXCEPTION_SHOULD_BE_THROWN);
467471
System.Assert.areEqual(
468472
true,
469-
myException.getMessage().contains('Flow does not exist'),
473+
myException.getMessage()
474+
.contains(TriggerActionFlow.FLOW_DOES_NOT_EXIST_ERROR),
475+
EXCEPTION_SHOULD_HAVE_THE_CORRECT_MESSAGE
476+
);
477+
}
478+
479+
@IsTest
480+
private static void bypassWithNullTypeShouldThrowException() {
481+
try {
482+
TriggerActionFlow.bypass((System.Type) null);
483+
} catch (Exception e) {
484+
myException = e;
485+
}
486+
487+
System.Assert.areNotEqual(null, myException, EXCEPTION_SHOULD_BE_THROWN);
488+
System.Assert.areEqual(
489+
true,
490+
myException.getMessage()
491+
.contains(TriggerActionFlow.TYPE_MUST_BE_PROVIDED_ERROR),
492+
EXCEPTION_SHOULD_HAVE_THE_CORRECT_MESSAGE
493+
);
494+
}
495+
496+
@IsTest
497+
private static void clearBypassWithNullTypeShouldThrowException() {
498+
try {
499+
TriggerActionFlow.clearBypass((System.Type) null);
500+
} catch (Exception e) {
501+
myException = e;
502+
}
503+
504+
System.Assert.areNotEqual(null, myException, EXCEPTION_SHOULD_BE_THROWN);
505+
System.Assert.areEqual(
506+
true,
507+
myException.getMessage()
508+
.contains(TriggerActionFlow.TYPE_MUST_BE_PROVIDED_ERROR),
509+
EXCEPTION_SHOULD_HAVE_THE_CORRECT_MESSAGE
510+
);
511+
}
512+
513+
@IsTest
514+
private static void isBypassedWithNullTypeShouldThrowException() {
515+
try {
516+
TriggerActionFlow.isBypassed((System.Type) null);
517+
} catch (Exception e) {
518+
myException = e;
519+
}
520+
521+
System.Assert.areNotEqual(null, myException, EXCEPTION_SHOULD_BE_THROWN);
522+
System.Assert.areEqual(
523+
true,
524+
myException.getMessage()
525+
.contains(TriggerActionFlow.TYPE_MUST_BE_PROVIDED_ERROR),
470526
EXCEPTION_SHOULD_HAVE_THE_CORRECT_MESSAGE
471527
);
472528
}

0 commit comments

Comments
 (0)