Skip to content

Commit e251ea1

Browse files
authored
Closes #20605: Document variable prefilling via URL parameters (#20619)
1 parent a1aaf46 commit e251ea1

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

docs/customization/custom-scripts.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,61 @@ A complete date & time. Returns a `datetime.datetime` object.
404404

405405
Custom scripts can be run via the web UI by navigating to the script, completing any required form data, and clicking the "run script" button. It is possible to schedule a script to be executed at specified time in the future. A scheduled script can be canceled by deleting the associated job result object.
406406

407+
#### Prefilling variables via URL parameters
408+
409+
Script form fields can be prefilled by appending query parameters to the script URL. Each parameter name must match the variable name defined on the script class. Prefilled values are treated as initial values and can be edited before execution. Multiple values can be supplied by repeating the same parameter. Query values must be percent‑encoded where required (for example, spaces as `%20`).
410+
411+
Examples:
412+
413+
For string and integer variables, when a script defines:
414+
415+
```python
416+
from extras.scripts import Script, StringVar, IntegerVar
417+
418+
class MyScript(Script):
419+
name = StringVar()
420+
count = IntegerVar()
421+
```
422+
423+
the following URL prefills the `name` and `count` fields:
424+
425+
```
426+
https://<netbox>/extras/scripts/<script_id>/?name=Branch42&count=3
427+
```
428+
429+
For object variables (`ObjectVar`), supply the object’s primary key (PK):
430+
431+
```
432+
https://<netbox>/extras/scripts/<script_id>/?device=1
433+
```
434+
435+
If an object ID cannot be resolved or the object is not visible to the requesting user, the field remains unpopulated.
436+
437+
Supported variable types:
438+
439+
| Variable class | Expected input | Example query string |
440+
|--------------------------|---------------------------------|---------------------------------------------|
441+
| `StringVar` | string (percent‑encoded) | `?name=Branch42` |
442+
| `TextVar` | string (percent‑encoded) | `?notes=Initial%20value` |
443+
| `IntegerVar` | integer | `?count=3` |
444+
| `DecimalVar` | decimal number | `?ratio=0.75` |
445+
| `BooleanVar` | value → `True`; empty → `False` | `?enabled=true` (True), `?enabled=` (False) |
446+
| `ChoiceVar` | choice value (not label) | `?role=edge` |
447+
| `MultiChoiceVar` | choice values (repeat) | `?roles=edge&roles=core` |
448+
| `ObjectVar(Device)` | PK (integer) | `?device=1` |
449+
| `MultiObjectVar(Device)` | PKs (repeat) | `?devices=1&devices=2` |
450+
| `IPAddressVar` | IP address | `?ip=198.51.100.10` |
451+
| `IPAddressWithMaskVar` | IP address with mask | `?addr=192.0.2.1/24` |
452+
| `IPNetworkVar` | IP network prefix | `?network=2001:db8::/64` |
453+
| `DateVar` | date `YYYY-MM-DD` | `?date=2025-01-05` |
454+
| `DateTimeVar` | ISO datetime | `?when=2025-01-05T14:30:00` |
455+
| `FileVar` | — (not supported) ||
456+
457+
!!! note
458+
- The parameter names above are examples; use the actual variable attribute names defined by the script.
459+
- For `BooleanVar`, only an empty value (`?enabled=`) unchecks the box; any other value including `false` or `0` checks it.
460+
- File uploads (`FileVar`) cannot be prefilled via URL parameters.
461+
407462
### Via the API
408463

409464
To run a script via the REST API, issue a POST request to the script's endpoint specifying the form data and commitment. For example, to run a script named `example.MyReport`, we would make a request such as the following:

0 commit comments

Comments
 (0)