SetWithTemplate
Set a value built by a template in context under the key you entered.
python · Common Scripts
Details
| ID | SetWithTemplate |
|---|---|
| Language | python |
| From Version | 6.5.0 |
| Docker Image | demisto/python3:3.12.13.10404775 |
| Tags | Utility |
README
Set a value built by a template in context under the key you entered.
Script Data
| Name | Description |
|---|---|
| Script Type | python3 |
| Tags | Utility |
| Cortex XSOAR Version | 6.5.0 |
Inputs
| Argument Name | Description |
|---|---|
| key | The key to set. Can be a full path such as “Key.ID”. If using append=true can also use a DT selector such as “Data(val.ID == obj.ID)”. |
| template | The template text which can include DT expressions such as ${value}. |
| template_type | The template type. |
| append | If false, the context key will be overwritten. If set to true, the script will be appended to the existing context key. |
| stringify | Whether to save the argument as a string. The default value is “noop”. |
| force | Whether to force the creation of the context. The default value is “false”. |
| context | The context data which overwrites the Demisto context. |
| variable_markers | The pair of start and end markers to bracket a variable name. |
| keep_symbol_to_null | Set to true to not replace a value if the variable is null, otherwise false. |
Outputs
There are no outputs for this script.
Getting Started
The script builds a text from a template text which includes variables such as:
- This is a test message for ${user_name}.
The template will be formatted to This is a test message for John Doe. by replacing variable parameters.
By default, a variable name starts with ${ and ends with } . You can change the start marker and end marker by specifying the variable_markers parameter.
Examples
Replace variables in a text based on the context data
Command
!SetWithTemplate key=out template=${lists.Template}
Lists Library
Template:
My name is ${first_name} ${last_name}.
Context Data
{
"first_name": "John",
"last_name": "Doe"
}
Output
{
"out": "My name is John Doe."
}
Change the variable start and end marker to the windows command shell style such as %name%
Command
!SetWithTemplate key=out template=${lists.Template} variable_markers=%,%
Lists Library
Template:
My name is %first_name% %last_name%.
Context Data
{
"first_name": "John",
"last_name": "Doe"
}
Output
{
"out": "My name is John Doe."
}
Change the variable start and end marker to the UNIX shell style such as $name
Command
!SetWithTemplate key=out template=${lists.Template} variable_markers=$
Lists Library
Template:
My name is $first_name $last_name.
Context Data
{
"first_name": "John",
"last_name": "Doe"
}
Output
{
"out": "My name is John Doe."
}
Keep variable names if they are missing in the context
Command
!SetWithTemplate key=out template=${lists.Template} keep_symbol_to_null=true
Lists Library
Template:
My name is ${first_name} ${last_name}.
Context Data
{
"first_name": "John"
}
Output
{
"out": "My name is John ${last_name}."
}
Use DTs to build variables
Command
!SetWithTemplate key=out template=${lists.Template}
Lists Library
Template:
My name is ${first_name=val.toUpperCase()} ${last_name=val.toUpperCase()}.
Context Data
{
"first_name": "John",
"last_name": "Doe"
}
Output
{
"out": "My name is JOHN DOE."
}
import json import demistomock as demisto from SetWithTemplate import main class SideEffectExecuteCommand: def __init__(self, obj): self.__obj = obj def execute_command(self, cmd, args, fail_on_error): val = None if cmd == "getList": try: name = args["listName"] if isinstance(self.__obj, dict): val = self.__obj.get(name) if val is None: val = demisto.get(self.__obj, name) except Exception: pass if val is None: if fail_on_error: raise ValueError("not found") else: return False, None else: if fail_on_error: raise ValueError("not found") else: return True, val class TestSetWithTemplate: def __side_effect_demisto_dt(self, obj, dt): if isinstance(obj, dict): val = obj.get(dt) if val is not None: return val if dt.startswith("."): key = dt[1:] return demisto.get(obj, key) if key else obj else: return demisto.get(obj, dt) def test_main(self, mocker): with open("./test_data/test.json") as f: test_list = json.load(f) mocker.patch.object(demisto, "dt", side_effect=self.__side_effect_demisto_dt) for t in test_list: mocker.patch("SetWithTemplate.execute_command", side_effect=SideEffectExecuteCommand(t.get("lists")).execute_command) mocker.patch.object(demisto, "incident", return_value=t.get("incident")) mocker.patch.dict(demisto.callingContext, {"context.PlaybookInputs": t.get("inputs")}) mocker.patch.object(demisto, "args", return_value=t["args"]) mocker.patch.object(demisto, "results") main() assert demisto.results.call_count == 1 results = demisto.results.call_args[0][0] entry_context = results.get("EntryContext") assert json.dumps(entry_context) == json.dumps(t["entry_context"])