-
Notifications
You must be signed in to change notification settings - Fork 77
/
LaunchAutolaunchedFlow.cls
126 lines (121 loc) · 3.68 KB
/
LaunchAutolaunchedFlow.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
global class LaunchAutolaunchedFlow {
/**
* Launches an Autolaunched Flow with up to 3 parameters
* Inputs:
* String flowApiName
* String namespace
* String param1Name
* String param1Value
* String param2Name
* String param2Value
* String param3Name
* String param3Value
* Outputs:
* Boolean isSuccess
* String errorMessage
*/
@InvocableMethod(label='Launches an Autolauched Flow' category='Flows')
global static List<OutputParameters> bulkInvoke(
List<InputParameters> inputs
) {
List<OutputParameters> outputs = new List<OutputParameters>();
for (InputParameters input : inputs) {
outputs.add(invoke(input));
}
return outputs;
}
private static OutputParameters invoke(InputParameters input) {
OutputParameters output = new OutputParameters();
try {
// Prepare flow parameters
Map<String, String> flowParams = new Map<String, String>();
generateFlowParam(
input.param1Name,
input.param1Value,
1,
flowParams
);
generateFlowParam(
input.param2Name,
input.param2Value,
2,
flowParams
);
generateFlowParam(
input.param3Name,
input.param3Value,
3,
flowParams
);
// Start flow interview
if (!Test.isRunningTest()) {
Flow.Interview newInterview = Flow.Interview.createInterview(
input.namespace,
input.flowApiName,
flowParams
);
newInterview.start();
}
output.isSuccess = true;
} catch (Exception e) {
output.isSuccess = false;
output.errorMessage = e.getMessage();
}
return output;
}
private static void generateFlowParam(
String name,
String value,
Integer index,
Map<String, String> flowParams
) {
// If name or value is present but the other is null, throw error
if (name != null && value == null) {
throw new InvocableException(
'Parameter ' + index + ' does not have a value assigned'
);
} else if (name == null && value != null) {
throw new InvocableException(
'You did not specify a name for parameter ' + index
);
}
// If neither are present, skip parameter
if (name == null && value == null) {
return;
}
// Add parameter to list
flowParams.put(name, value);
}
global class InvocableException extends Exception {
}
/**
* Wrapper class for input parameters
*/
global class InputParameters {
@InvocableVariable(label='Flow API Name' required=true)
global String flowApiName;
@InvocableVariable
global String namespace;
@InvocableVariable
global String param1Name;
@InvocableVariable
global String param1Value;
@InvocableVariable
global String param2Name;
@InvocableVariable
global String param2Value;
@InvocableVariable
global String param3Name;
@InvocableVariable
global String param3Value;
}
/**
* Wrapper class for output parameters
*/
global class OutputParameters {
@InvocableVariable
global Boolean isSuccess;
@InvocableVariable
global String errorMessage;
}
}