Which property to use with components field in behaviours

Hi folks,

When using behaviours, it is very easy to set value of a field based on the other field’s value, if it is a single choice picker field or system field, like priority, because you get to use .name property:

const changedField = getChangeField();

const accountId = "123456-8545622-54522";

if (changedField.getName() === 'Priority') {

    const priorityName = changedField.getValue().name.toString();

    if (priorityName === "High") {
        getFieldById("assignee").setValue(accountId);
    }else{
        getFieldById("assignee").setValue(null);
    }
}

For labels I figured out I can do .includes(‘label’), and set assignee if it returns true, but I cannot figure out how to build this logic with system components field. It requires an ID and a string name. I suspect this is also the case with multiple picker custom fields.

If anyone used any components based behaviours, I would appreciate your input. Thanks!

Hi Mykhailo,

Just wanted to point you to Behaviour Bot which can handle cases like this pretty easily:

I think the key thing here is the typescript method “some” - which checks if any in an array matches some condition.

As well as writing the code you can test it from the same tool.

Note also you can click this “Show values” checkbox and it will show you the structure of the object that you will get back from getValue so you can verify the code is valid:

image

I suspect this is also the case with multiple picker custom fields.

You would use some as above but multi-select and checkbox fields you use .value to get the string value of the option:

Give me a shout if you have any questions.

cheers, jamie

1 Like

Hi Jamie,

Thank you, this actually solves my problem entirely.
I opted to use mapping and for loop, since this is easier to maintain in the future, but here is a working script that parses component names

const componentsField = getFieldById("components");
const assigneeField = getFieldById("assignee");

// Define the mapping of component names to assignee account IDs
const componentAssigneeMapping = {
    'component_1': "user1", // name of user 1
    'component_2': "user2", // name of user 2
    // Add more component-to-assignee mappings as needed
};

const components = componentsField.getValue() || [];

let assigneeSet = false;

for (let component of components) {
    const assigneeForComponent = componentAssigneeMapping[component.name];
    if (assigneeForComponent !== undefined) {
        assigneeField.setValue(assigneeForComponent);
        assigneeSet = true;
        break; // Stop after assigning the first matched component's assignee
    }
}
1 Like