diff --git a/src/main/rules/SameRecordFieldUpdates.ts b/src/main/rules/SameRecordFieldUpdates.ts index 6f726f2..d7c1281 100644 --- a/src/main/rules/SameRecordFieldUpdates.ts +++ b/src/main/rules/SameRecordFieldUpdates.ts @@ -34,25 +34,27 @@ export class SameRecordFieldUpdates extends RuleCommon implements core.IRuleDefi ); if (!isBeforeSaveType || !isQualifiedTriggerTypes) { - return new core.RuleResult(this, []); + return new core.RuleResult(this, results); } - const resultDetails: core.ResultDetails[] = []; - const potentialElements = flow.elements?.filter( (node) => node.subtype === "recordUpdates" ) as core.FlowNode[]; + if (potentialElements == null || typeof potentialElements[Symbol.iterator] !== "function") { + return new core.RuleResult(this, results); + } + for (const node of potentialElements) { if ( typeof node.element === "object" && "inputReference" in node.element && node.element.inputReference === "$Record" ) { - resultDetails.push(new core.ResultDetails(node)); + results.push(new core.ResultDetails(node)); } } - return new core.RuleResult(this, resultDetails); + return new core.RuleResult(this, results); } } diff --git a/tests/SameRecordFieldUpdates.test.ts b/tests/SameRecordFieldUpdates.test.ts index 98ec37e..dab5124 100644 --- a/tests/SameRecordFieldUpdates.test.ts +++ b/tests/SameRecordFieldUpdates.test.ts @@ -170,4 +170,76 @@ describe("SameRecordFieldUpdates", () => { expect(expectedRule).to.be.ok; expect(expectedRule?.occurs).to.be.true; }); + + it("should not error when start element is not existing", async () => { + const testData: ParsedFlow = { + flow: { + elements: [ + { + element: { + description: "test", + name: "Update_triggering_records", + label: "Update triggering records", + locationX: "176", + locationY: "287", + inputAssignments: { field: "Active__c", value: { stringValue: "Yes" } }, + inputReference: "$Record", + }, + subtype: "recordUpdates", + metaType: "node", + connectors: [], + name: "Update_triggering_records", + locationX: "176", + locationY: "287", + }, + { + element: { + locationX: "50", + locationY: "0", + connector: { targetReference: "Update_triggering_records" }, + object: "Account", + recordTriggerType: "Create", + triggerType: "RecordBeforeSave", + }, + subtype: "start", + metaType: "node", + connectors: [ + { + element: { targetReference: "Update_triggering_records" }, + processed: false, + type: "connector", + reference: "Update_triggering_records", + }, + ], + name: "flowstart", + locationX: "50", + locationY: "0", + }, + ], + }, + } as {} as ParsedFlow; + + const ruleResult: RuleResult = rule.execute(testData.flow as Flow); + + expect(ruleResult.occurs).to.be.false; + }); + + it("should not error when elements are missing", async () => { + const testData: ParsedFlow = { + flow: { + start: { + locationX: "50", + locationY: "0", + connector: { targetReference: "Update_triggering_records" }, + object: "Account", + recordTriggerType: "Create", + triggerType: "RecordBeforeSave", + }, + }, + } as {} as ParsedFlow; + + const ruleResult: RuleResult = rule.execute(testData.flow as Flow); + + expect(ruleResult.occurs).to.be.false; + }); });