-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #140 from Lightning-Flow-Scanner/feature/same-reco…
…rd-field-update feat(RuleEngine): same record field update new rule fixes #134
- Loading branch information
Showing
3 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import * as core from "../internals/internals"; | ||
import { RuleCommon } from "../models/RuleCommon"; | ||
|
||
export class SameRecordFieldUpdates extends RuleCommon implements core.IRuleDefinition { | ||
protected qualifiedRecordTriggerTypes: Set<string> = new Set<string>(["Create", "Update"]); | ||
|
||
constructor() { | ||
super( | ||
{ | ||
name: "SameRecordFieldUpdates", | ||
label: "Same Record Field Updates", | ||
description: | ||
"Before-save same-record field updates allows you to update the record using variable assignments to `$Record`. This is significantly faster than doing another DML on the same-record that triggered the flow", | ||
supportedTypes: [...core.FlowType.backEndTypes], | ||
docRefs: [ | ||
{ | ||
label: "Learn about same record field updates", | ||
path: "https://architect.salesforce.com/decision-guides/trigger-automation#Same_Record_Field_Updates", | ||
}, | ||
], | ||
isConfigurable: false, | ||
autoFixable: false, | ||
}, | ||
{ severity: "warning" } | ||
); | ||
} | ||
|
||
public execute(flow: core.Flow): core.RuleResult { | ||
const results: core.ResultDetails[] = []; | ||
|
||
const isBeforeSaveType = flow.start?.triggerType === "RecordBeforeSave"; | ||
const isQualifiedTriggerTypes = this.qualifiedRecordTriggerTypes.has( | ||
flow.start?.recordTriggerType | ||
); | ||
|
||
if (!isBeforeSaveType || !isQualifiedTriggerTypes) { | ||
return new core.RuleResult(this, []); | ||
} | ||
|
||
const resultDetails: core.ResultDetails[] = []; | ||
|
||
const potentialElements = flow.elements?.filter( | ||
(node) => node.subtype === "recordUpdates" | ||
) as core.FlowNode[]; | ||
|
||
for (const node of potentialElements) { | ||
if ( | ||
typeof node.element === "object" && | ||
"inputReference" in node.element && | ||
node.element.inputReference === "$Record" | ||
) { | ||
resultDetails.push(new core.ResultDetails(node)); | ||
} | ||
} | ||
|
||
return new core.RuleResult(this, resultDetails); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import "mocha"; | ||
|
||
import { ParsedFlow } from "../src/main/models/ParsedFlow"; | ||
import { SameRecordFieldUpdates } from "../src/main/rules/SameRecordFieldUpdates"; | ||
import { RuleResult, Flow } from "../src"; | ||
|
||
describe("SameRecordFieldUpdates", () => { | ||
let expect; | ||
let rule; | ||
before(async () => { | ||
expect = (await import("chai")).expect; | ||
rule = new SameRecordFieldUpdates(); | ||
}); | ||
|
||
it("should flag same record updates on before context flows", async () => { | ||
const testData: ParsedFlow = { | ||
flow: { | ||
start: { | ||
locationX: "50", | ||
locationY: "0", | ||
connector: { targetReference: "Update_triggering_records" }, | ||
object: "Account", | ||
recordTriggerType: "Create", | ||
triggerType: "RecordBeforeSave", | ||
}, | ||
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.true; | ||
}); | ||
|
||
it("should not flag same record updates on after context flows", async () => { | ||
const testData: ParsedFlow = { | ||
flow: { | ||
start: { | ||
locationX: "50", | ||
locationY: "0", | ||
connector: { targetReference: "Update_triggering_records" }, | ||
object: "Account", | ||
recordTriggerType: "Create", | ||
triggerType: "RecordAfterSave", | ||
}, | ||
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: "RecordAfterSave", | ||
}, | ||
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; | ||
}); | ||
}); |