SetAndHandleEmpty

Set a value in context under the key you entered. If no value is entered, the script doesn't do anything. This automation runs using the default Limited User role, unless you explicitly change the permissions. For more information, see the section about permissions here: - For Cortex XSOAR 6 see https://docs-cortex.paloaltonetworks.com/r/Cortex-XSOAR/6.x/Cortex-XSOAR-Playbook-Design-Guide/Automations - For Cortex XSOAR 8 Cloud see https://docs-cortex.paloaltonetworks.com/r/Cortex-XSOAR/8/Cortex-XSOAR-Cloud-Documentation/Create-a-script - For Cortex XSOAR 8.7 On-prem see https://docs-cortex.paloaltonetworks.com/r/Cortex-XSOAR/8.7/Cortex-XSOAR-On-prem-Documentation/Create-a-script

javascript · Common Scripts

Source

function getValue(value, stringify = false) {
    if (stringify) {
        if (value === null || value === undefined) {
            return '';
        }
        return String(value);
    } else if (!value || !(typeof value === 'string')) {
        return value;
    } else {
        try {
            return JSON.parse(value);
        } catch (error) {
            return value;
        }
    }
}

function main() {
    const keys = argToList(args.key);
    let value = args.value;
    const force = args.force === 'true';
    value = getValue(value, args.stringify === 'true');

    let results = [];
    for (let i = 0; i < keys.length; i++) {
        let humanReadable = '';
        let contextEntry = {};

        // Treat null, undefined and empty string as "no value".
        // Falsy values such as 0 or false should be treated as valid values.
        const isEmpty = value === null || value === undefined || value === '';
        if (!isEmpty || force) {
            humanReadable = `Key ${keys[i]} set`;
            contextEntry = { [keys[i]]: value };
        } else {
            humanReadable = 'value is None';
        }

        if (args.append === 'false' && Object.keys(contextEntry).length > 0) {
            executeCommand('DeleteContext', { key: keys[i], subplaybook: 'auto' });
        }
        results.push({
            Type: entryTypes.note,
            EntryContext: contextEntry,
            ContentsFormat: formats.json,
            Contents: humanReadable,
            HumanReadable: humanReadable
        });
    };
    return results;
}

try {
    return main();
} catch (error) {
    throw 'Error occurred while running the script:\n' + error;
}

README

Set a value in context under the key you entered. If no value is entered, the script doesn’t do anything.

This automation runs using the default Limited User role, unless you explicitly change the permissions.
For more information, see the section about permissions here: For Cortex XSOAR 6, see the https://docs-cortex.paloaltonetworks.com/r/Cortex-XSOAR/6.x/Cortex-XSOAR-Playbook-Design-Guide/Automations for Cortex XSOAR 8 Cloud, see the https://docs-cortex.paloaltonetworks.com/r/Cortex-XSOAR/8/Cortex-XSOAR-Cloud-Documentation/Create-a-script for Cortex XSOAR 8 On-prem, see the https://docs-cortex.paloaltonetworks.com/r/Cortex-XSOAR/8.7/Cortex-XSOAR-On-prem-Documentation/Create-a-script.

Script Data


Name Description
Script Type javascript
Tags Utility
Cortex XSOAR Version 5.0.0

Inputs


Argument Name Description
key The key to set in context.
value The value of the key to set in context. The value is usually a DQ expression. Can be an array.
append Whether to append the new context key to the existing context key. If “false”, then the existing context key will be overwritten with the new context key.
stringify Whether to save the argument as a string. The default value is “false”.
force Whether to force the creation of the context. The default value is “false”.

Outputs


There are no outputs for this script.