Lightning-Flow-Scanner / lightning-flow-scanner-sfdx

A Salesforce CLI Plugin designed to pinpoint deviations from Industry Best Practices in Salesforce Flows, ensuring standards of business automation excellence.
https://www.npmjs.com/package/lightning-flow-scanner
GNU Affero General Public License v3.0
112 stars 12 forks source link

Guidance on how to use plugin #64

Closed nebc-jasonfung closed 1 year ago

nebc-jasonfung commented 1 year ago

I've been playing around with the plugin and have created a test flow in order to simulate some of the errors - i'm unable to get this to return any errors.

Here is my example flow:

<?xml version="1.0" encoding="UTF-8"?>
<Flow xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>58.0</apiVersion>
    <description>Flow to test https://github.com/Force-Config-Control/lightning-flow-scanner-sfdx#installation</description>
    <interviewLabel>Flow Scanner {!$Flow.CurrentDateTime}</interviewLabel>
    <label>Flow Scanner</label>
    <loops>
        <name>Loop_Contacts</name>
        <label>Loop Contacts</label>
        <locationX>176</locationX>
        <locationY>539</locationY>
        <collectionReference>Get_Contacts</collectionReference>
        <iterationOrder>Asc</iterationOrder>
        <nextValueConnector>
            <targetReference>Update_Contact_Phone</targetReference>
        </nextValueConnector>
    </loops>
    <processMetadataValues>
        <name>BuilderType</name>
        <value>
            <stringValue>LightningFlowBuilder</stringValue>
        </value>
    </processMetadataValues>
    <processMetadataValues>
        <name>CanvasMode</name>
        <value>
            <stringValue>AUTO_LAYOUT_CANVAS</stringValue>
        </value>
    </processMetadataValues>
    <processMetadataValues>
        <name>OriginBuilderType</name>
        <value>
            <stringValue>LightningFlowBuilder</stringValue>
        </value>
    </processMetadataValues>
    <processType>AutoLaunchedFlow</processType>
    <recordLookups>
        <name>Get_Contacts</name>
        <label>Get Contacts</label>
        <locationX>176</locationX>
        <locationY>323</locationY>
        <assignNullValuesIfNoRecordsFound>false</assignNullValuesIfNoRecordsFound>
        <connector>
            <targetReference>Get_Nebula_Account</targetReference>
        </connector>
        <filterLogic>and</filterLogic>
        <filters>
            <field>AccountId</field>
            <operator>EqualTo</operator>
            <value>
                <elementReference>$Record.Id</elementReference>
            </value>
        </filters>
        <getFirstRecordOnly>false</getFirstRecordOnly>
        <object>Contact</object>
        <storeOutputAutomatically>true</storeOutputAutomatically>
    </recordLookups>
    <recordLookups>
        <name>Get_Nebula_Account</name>
        <label>Get Nebula Account</label>
        <locationX>176</locationX>
        <locationY>431</locationY>
        <assignNullValuesIfNoRecordsFound>false</assignNullValuesIfNoRecordsFound>
        <connector>
            <targetReference>Loop_Contacts</targetReference>
        </connector>
        <filterLogic>and</filterLogic>
        <filters>
            <field>Id</field>
            <operator>EqualTo</operator>
            <value>
                <stringValue>someTestId</stringValue>
            </value>
        </filters>
        <getFirstRecordOnly>true</getFirstRecordOnly>
        <object>Account</object>
        <storeOutputAutomatically>true</storeOutputAutomatically>
    </recordLookups>
    <recordUpdates>
        <name>Update_Contact_Phone</name>
        <label>Update Contact Phone</label>
        <locationX>264</locationX>
        <locationY>647</locationY>
        <connector>
            <targetReference>Loop_Contacts</targetReference>
        </connector>
        <filterLogic>and</filterLogic>
        <filters>
            <field>Phone</field>
            <operator>EqualTo</operator>
            <value>
                <stringValue></stringValue>
            </value>
        </filters>
        <inputAssignments>
            <field>Phone</field>
            <value>
                <stringValue>07111111111</stringValue>
            </value>
        </inputAssignments>
        <object>Contact</object>
    </recordUpdates>
    <start>
        <locationX>50</locationX>
        <locationY>0</locationY>
        <connector>
            <targetReference>Get_Contacts</targetReference>
        </connector>
        <object>Account</object>
        <recordTriggerType>CreateAndUpdate</recordTriggerType>
        <triggerType>RecordAfterSave</triggerType>
    </start>
    <status>Active</status>
    <variables>
        <name>Unused_Variable</name>
        <dataType>String</dataType>
        <isCollection>false</isCollection>
        <isInput>true</isInput>
        <isOutput>true</isOutput>
    </variables>
</Flow>

Here's the output after running the scan:

image

What I'm expecting to see are the following rules being triggered:

Am I missing something?

RubenHalman commented 1 year ago

can you verify if you what version you are using and if its the latest version of the sfdx plugin? I believe we have had this with v2 and was patched with 2.1 . Also are you using any flowscanner configuration file specifying which rules to run?(this is optional but if you do, it needs to contain rules)

nebc-jasonfung commented 1 year ago

Hi @RubenHalman, it appears I'm on the latest version:

image

I have tried both sfdx flow:scan and sfdx flow:scan --config path/to/.flow-scanner.json where the defined config rules is:

{
  "rules": [
    {
      "DMLStatementInLoop": {
        "severity": "warning"
      },
      "HardcodedIds": {
        "severity": "warning"
      },
      "MissingFlowDescription": {
        "severity": "warning"
      },
      "UnusedVariables": {
        "severity": "error"
      }
    }
  ]
}

sfdx flow:scan returns 0 errors found and with the config flag it returns the below and stops:

image
RubenHalman commented 1 year ago

Thank you for verifying the version. Try to delete the configuration file and then run the scan or put all the rules of severity 'error'. warning levels only show when you configure the threshold to warning too. The configuration file is automatically detected if it exists, so in your case only one rule is really running.

If you run the --json flag on addition to your commands, you should see some of the rules have warnings that did not show up as error

nebc-jasonfung commented 1 year ago

Thank you for the response, I have gone and deleted the config file and ran sfdx flow:scan --json and the following is the output:

{
  "status": 0,
  "result": {
    "summary": {
      "flowsNumber": 28,
      "errors": 0,
      "message": "A total of \u001b[1m0\u001b[22m errors have been found in \u001b[1m28\u001b[22m flows.",
      "errorLevelsDetails": {}
    },
    "status": 0,
    "results": []
  }
}

It appears it's still not detecting any errors

nebc-jasonfung commented 1 year ago

@RubenHalman it appears to be a problem specific to v2.2.0 - after installing v2.1.0 the errors are surfacing as expected.

RubenHalman commented 1 year ago

I am happy it works for you now and I'll look into 2.2. actually the latest version should be 2.3. It will help me if you would be willing to try that version to see if you are having issues too probably.

nebc-jasonfung commented 1 year ago

Thanks, @RubenHalman, 2.3 seems to work okay for sf flow:scan command, for sf flow:scan --config path/to/config.json it doesn't seem to output the results:

image

What I want to achieve is suppressing the warns and only surface the errors. Here is my config file:

{
  "rules": [
    {
      "DMLStatementInLoop": {
        "severity": "error"
      },
      "DuplicateDMLOperations": {
        "severity": "warning"
      },
      "NoHardcodedIds": {
        "severity": "error"
      },
      "MissingFlowDescription": {
        "severity": "warning"
      },
      "MissingFaultPath": {
        "severity": "warning"
      },
      "NoUnusedParameters": {
        "severity": "warning"
      },
      "MissingNullHandler": {
        "severity": "warning"
      },
      "UnconnectedElements": {
        "severity": "warning"
      },
      "UnusedVariables": {
        "severity": "warning"
      }
    }
  ]
}
RubenHalman commented 1 year ago

great thanks. your rules are configured slightly incorrect: they need to be an object like so {{ "rules": { "MissingFlowDescription": { "severity": "warning" }, "UnusedVariables": { "severity": "error" } } }.

just remove the '[]'

meaning it is one object and not an array. an error should probably be thrown in this case.

Finally note you can also "deactivate" rules from running by only specifying rules you are interested in

RubenHalman commented 1 year ago

@nebc-jasonfung did it work? if so please close this ticket. thanks!

nebc-jasonfung commented 1 year ago

That did work @RubenHalman - many thanks for your help!