-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-decision-node.ts
166 lines (149 loc) · 4.9 KB
/
add-decision-node.ts
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* Advanced example: Adding a decision node to a Flow
*
* This example demonstrates more advanced usage of the sf-flow-parser library:
* - Creating a new decision node
* - Inserting it into an existing Flow
* - Rerouting connections between nodes
* - Modifying the Flow structure
*
* Run with:
* deno run --allow-read --allow-write examples/add-decision-node.ts path/to/flow.xml
*/
import {
BaseFlowNodeWithConnector,
findParentFlowNodes,
getConnectors,
parseFromFile,
stringifyToFile,
} from "../src/main.ts";
import {FlowCondition, FlowDecision} from "@salesforce/types/metadata";
/**
* Create a decision node with the specified connections
*
* @param name - Name of the decision node
* @param defaultTarget - Target node for the default path
* @param conditionTarget - Target node for the condition path
* @param fieldName - Optional field name to use in the condition (defaults to "$Record.SomeField__c")
* @returns A new FlowDecision object
*/
function createDecisionNode(
name: string,
defaultTarget: string,
conditionTarget: string,
fieldName = "$Record.SomeField__c",
): FlowDecision {
// Create a condition that checks if the field equals "SomeValue"
const condition: FlowCondition = {
leftValueReference: fieldName,
operator: "EqualTo",
rightValue: {
stringValue: "SomeValue",
},
processMetadataValues: [],
};
return {
name: name,
label: "Custom Decision",
locationX: 0,
locationY: 0,
processMetadataValues: [],
defaultConnector: {
targetReference: defaultTarget,
processMetadataValues: [],
},
defaultConnectorLabel: "Default",
rules: [
{
name: "Condition1",
conditionLogic: "and",
conditions: [condition],
connector: {
targetReference: conditionTarget,
processMetadataValues: [],
},
label: "Condition 1",
processMetadataValues: [],
},
],
} as FlowDecision;
}
/**
* Main function to run the example
*/
function main() {
// Check if a file path was provided
if (Deno.args.length < 1) {
console.error("Please provide a path to a Flow XML file");
console.error(
"Usage: deno run --allow-read --allow-write examples/add-decision-node.ts path/to/flow.xml",
);
Deno.exit(1);
}
const inputPath = Deno.args[0];
const outputPath = "modified-flow.xml";
try {
// Parse the Flow XML file
console.log(`Parsing Flow from ${inputPath}...`);
const flow = parseFromFile(inputPath);
// Find a target node to insert our decision before
// For this example, we'll use the first subflow if available
if (!flow.subflows || flow.subflows.length === 0) {
console.error(
"No subflows found in the Flow. This example requires a Flow with at least one subflow.",
);
Deno.exit(1);
}
const targetSubflow = flow.subflows[0];
const targetName = targetSubflow.name || "";
const nextNodeName = targetSubflow.connector?.targetReference || "";
console.log(`Target subflow: ${targetName}`);
console.log(`Next node: ${nextNodeName}`);
// Create a new decision node
const decisionNode = createDecisionNode(
"Custom_Decision_" + Date.now(),
targetName, // Default path goes to the original target
nextNodeName, // Condition path goes to where the target was going
);
console.log(`Created decision node: ${decisionNode.name}`);
// Method 1: Find and update parent nodes manually
const parents = findParentFlowNodes(flow, targetName);
console.log(`Found ${parents.length} parent nodes`);
parents.forEach((parent) => {
console.log(`Updating connectors in parent: ${parent.name}`);
getConnectors(parent as BaseFlowNodeWithConnector).forEach(
(connector) => {
if (connector.targetReference === targetName) {
console.log(
` Changing connector target from ${connector.targetReference} to ${decisionNode.name}`,
);
connector.targetReference = decisionNode.name || "";
}
},
);
});
// Method 2: Alternative approach using the reparentNode function
// Uncomment the line below to use this approach instead
// const updatedCount = reparentNode(flow, targetName, decisionNode.name || "");
// console.log(`Updated ${updatedCount} connections using reparentNode function`);
// Add the decision node to the Flow
if (!flow.decisions) {
flow.decisions = [];
}
flow.decisions.push(decisionNode);
// Write the modified Flow back to a file
console.log(`Writing modified Flow to ${outputPath}...`);
stringifyToFile(flow, outputPath);
console.log(`Successfully wrote modified Flow to ${outputPath}`);
} catch (error: any) {
console.error(`Error: ${error.message}`);
Deno.exit(1);
}
}
// Run the main function
try {
main();
} catch (error: any) {
console.error("Error:", error);
Deno.exit(1);
}