Forum Discussion

FICO's avatar
FICO
Community Manager
3 years ago

How to perform pre-check before calling custom run mode

I have a custom run mode that adds a value user entered into a list. However, I would like to check if this value already exists in the current list before calling the custom run mode. I don't think it is possible to perform the check in VDL so am working on a JavaScript function to perform the check. However, I'm not sure on how to access entity values in the JS function. I've used scenario observer to access and modify entity values before but in this case, I need to get to the value right away instead of putting the checking logic into a callback function, I think.

My vdl code looks like below and the JS function is check_tab_name.

<vdl-action-group name="executeAddNewTab">

    <vdl-action command="check_tab_name"></vdl-action>

    <vdl-action-execute mode="ADD_NEW_TAB_NAME" vdl-if="=vars.newValueIndeed"></vdl-action-execute>

   </vdl-action-group>

Appreciate any help on this issue.

1 Reply

  • FICO's avatar
    FICO
    Community Manager

    OK, I found the answer in your VDL Language app and adapted the following code. Thank you. :-)

    function checkDuplicateSetEntry(setName, value, viewModel) {

    var entryExists = viewModel.scenario.entities[setName]()

    .map(function (entry) {

    return entry.value;

    }).indexOf(value) !== -1;

    if (entryExists) {

    // This will stop the VDL action chain,

    // so the value will not be saved to the server

    throw Error('Value "' + value + '" already exists in set "'

    + setName + '".');

    }

    // Return value to pass onto the next VDL action

    return value;

    }