# `sf` CLI commands for verify-sqlite-schema

All commands assume an authenticated default org. Check with:

```bash
sf org display --json 2>/dev/null | jq -r 'if .status == 0 then .result.alias + " (" + .result.username + ")" else "NOT_CONNECTED" end'
```

If that prints `NOT_CONNECTED`, list any authenticated orgs and prompt the user to pick or log in:

```bash
sf org list --json | jq -r '.result.nonScratchOrgs[]?.username, .result.scratchOrgs[]?.username'
```

If nothing is authenticated, instruct the user to run `sf org login web` first.

## 1. List SObjects (for fuzzy matching)

```bash
sf sobject list --sobject all --json | jq -r '.result[]' | grep -i "<base-word>"
```

## 2. Describe an SObject

```bash
sf sobject describe --sobject <ApiName> --json
```

Full output includes `fields[]` and `fieldSets[]` arrays.

## 3. Check if a field exists on an SObject

```bash
sf sobject describe --sobject <ApiName> --json \
  | jq -r '.result.fields[] | select(.name == "<FieldApiName>") | .name'
```

Output is `<FieldApiName>` if present, empty if absent.

## 4. Check if a field is in the `Mobility_relevant` field set

```bash
sf sobject describe --sobject <ApiName> --json \
  | jq -r '.result.fieldSets[] | select(.name == "Mobility_relevant") | .fields[].name'
```

Lists all field API names in the set. Grep for your target field:

```bash
sf sobject describe --sobject <ApiName> --json \
  | jq -r '.result.fieldSets[] | select(.name == "Mobility_relevant") | .fields[].name' \
  | grep -x "<FieldApiName>" \
  && echo "IN_FIELDSET" || echo "NOT_IN_FIELDSET"
```

## 5. Retrieve the field set metadata for editing

Retrieve into a temp directory:

```bash
TMPDIR=$(mktemp -d /tmp/sf-fieldset-XXXXXX)
cd "$TMPDIR"
sf template generate project --name fieldset-edit
cd fieldset-edit
sf project retrieve start --metadata "FieldSet:<SObject>.Mobility_relevant"
```

Retrieved XML lives at:

```
force-app/main/default/objects/<SObject>/fieldSets/Mobility_relevant.fieldSet-meta.xml
```

After editing, you can deploy from this same directory (see command 6). When done, the temp directory can be safely removed.

## 6. Deploy the edited field set

```bash
sf project deploy start --metadata "FieldSet:<SObject>.Mobility_relevant"
```

Exits non-zero on deploy failure. Stream logs with `--verbose` if needed.

## 7. Check deploy status (async case)

```bash
sf project deploy report --job-id <id>
```

## Authentication troubleshooting

If any command returns `NOT_CONNECTED` or `401`:

```bash
sf org logout --target-org <alias>
sf org login web --alias <alias>
sf config set target-org=<alias>
```
