Skip to content

Instantly share code, notes, and snippets.

@pbsull
Last active February 20, 2025 14:59
Show Gist options
  • Select an option

  • Save pbsull/eb7136b26122384e9526c8c8fbfd03c6 to your computer and use it in GitHub Desktop.

Select an option

Save pbsull/eb7136b26122384e9526c8c8fbfd03c6 to your computer and use it in GitHub Desktop.
Salesforce - Find Unused Variables and Formulas in Flows

Salesforce Flow Unused Elements Finder

Overview

This Go script is designed to help Salesforce developers identify unused formulas and variables in their Salesforce Flow XML files. By parsing the XML files, the script checks for any formulas and variables that are defined but not used anywhere within the flow. This can help in cleaning up the flow definitions, making them more maintainable and efficient.

Why Use This Script?

  1. Maintainability: Keeping your flow definitions clean and free of unused elements makes them easier to understand and maintain.
  2. Performance: Removing unused elements can potentially improve the performance of your flows.
  3. Best Practices: Adhering to best practices by ensuring that only necessary elements are included in your flow definitions.

How to Use

  1. Prerequisites:

    • Ensure you have Go installed on your machine. You can download it from golang.org.
  2. Save the Script:

    • Save the provided Go script to a file, e.g., main.go.
  3. Run the Script:

    • Open your terminal and navigate to the directory where you saved main.go.
    • Run the script with the path to your flows folder as an argument:
      go run main.go /path/to/your/force-app/main/default/flows
    • For example:
      go run main.go /Users/patricksullivan/Desktop/firstarriving-sfdc/force-app/main/default/flows
  4. Output:

    • The script will process each .flow-meta.xml file in the specified folder and output the names of the unused formulas and variables for each file.

Example Output

File: /Users/patricksullivan/Desktop/firstarriving-sfdc/force-app/main/default/flows/Create_New_Expansion_Order_from_Opportunity.flow-meta.xml
- Unused Formulas:
  - ChurnQtyProduct
  - ContQtyProduct

- Unused Variables:
  - TempProductDiscount

This output indicates which formulas and variables are defined but not used in the specified flow file, allowing you to clean them up accordingly.

package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type Flow struct {
XMLName xml.Name `xml:"Flow"`
Formulas []Formula `xml:"formulas"`
Variables []Variable `xml:"variables"`
Elements []Element `xml:",any"`
}
type Formula struct {
Name string `xml:"name"`
DataType string `xml:"dataType"`
Expression string `xml:"expression"`
Scale int `xml:"scale"`
}
type Variable struct {
Name string `xml:"name"`
DataType string `xml:"dataType"`
IsCollection bool `xml:"isCollection"`
IsInput bool `xml:"isInput"`
IsOutput bool `xml:"isOutput"`
Scale int `xml:"scale"`
Value Value `xml:"value"`
}
type Value struct {
NumberValue float64 `xml:"numberValue"`
}
type Element struct {
XMLName xml.Name
Content string `xml:",innerxml"`
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: go run main.go <path-to-xml-folder>")
return
}
folderPath := os.Args[1]
err := filepath.Walk(folderPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && strings.HasSuffix(info.Name(), ".flow-meta.xml") {
processFile(path)
}
return nil
})
if err != nil {
fmt.Println("Error walking through folder:", err)
}
}
func processFile(filePath string) {
xmlFile, err := os.Open(filePath)
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer xmlFile.Close()
byteValue, _ := ioutil.ReadAll(xmlFile)
var flow Flow
err = xml.Unmarshal(byteValue, &flow)
if err != nil {
fmt.Println("Error unmarshalling XML:", err)
return
}
usedFormulas := make(map[string]bool)
usedVariables := make(map[string]bool)
for _, element := range flow.Elements {
for _, formula := range flow.Formulas {
if strings.Contains(element.Content, formula.Name) {
usedFormulas[formula.Name] = true
}
}
for _, variable := range flow.Variables {
if strings.Contains(element.Content, variable.Name) {
usedVariables[variable.Name] = true
}
}
}
fmt.Printf("File: %s\n", filePath)
fmt.Println("- Unused Formulas:")
for _, formula := range flow.Formulas {
if !usedFormulas[formula.Name] {
fmt.Println(" - " + formula.Name)
}
}
fmt.Println("\n- Unused Variables:")
for _, variable := range flow.Variables {
if !usedVariables[variable.Name] {
fmt.Println(" - " + variable.Name)
}
}
fmt.Println()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment