Configurable Workspace / Ui Builder - Multi-Select Filter Not Updating on Button Script

M_iA
Kilo Sage

Hello Community,

I'm working in a ServiceNow Configurable Workspace in UI builder and encountering a persistent issue with setting the value of a multi-select filter component. I need to clear all existing filters on the page and then set this specific multi-select filter to the sys_id of the currently logged-in user. This action is triggered by a button click.

I've performed extensive troubleshooting and confirmed several aspects of my script's behavior, but the filter UI itself does not update, and the records are not filtered as expected. I'm hoping someone with deeper experience in UI Builder's component state management can offer insights.

 

My Goal: To create a script (triggered by a button) that:

  1. Clears all current parFilters.
  2. Sets a specific multi-select filter component (filter_2) to the logged-in user's sys_id.

I am using the OOB data resource for getting the current users sys_id: get_current_user_id_1

  1.  

My Current Script:

 

function clearAllAndSetUserFilter ({ api }) {
    console.log('--- Script Execution Start (Addressing Default Value) ---');
    console.log('Current Timestamp:', new Date().toISOString());

    // Step 1: Clear all existing 'parFilters'
    api.setState('parFilters', []);
    console.log('Filters cleared: parFilters set to []');

    let currentUserSysId = null;

    // Retrieve the user's sys_id
    if (api.data && api.data.get_current_user_id_1 &&
        api.data.get_current_user_id_1.output &&
        api.data.get_current_user_id_1.output.data &&
        api.data.get_current_user_id_1.output.data.GlideDomain_Query &&
        api.data.get_current_user_id_1.output.data.GlideDomain_Query.user &&
        api.data.get_current_user_id_1.output.data.GlideDomain_Query.user.sys_id)
    {
        currentUserSysId = api.data.get_current_user_id_1.output.data.GlideDomain_Query.user.sys_id;
        console.log('Successfully retrieved currentUserSysId:', currentUserSysId);
    } else {
        console.error('ERROR: Could not retrieve sys_id. Path to sys_id is incomplete or undefined.');
    }

    // Step 3: Set the value of the 'filter_2' component to the user.
    if (currentUserSysId) {
        const filterValue = [{
            id: currentUserSysId
        }];

        // Set the filter component's value
        api.setState('filter_2', filterValue);
        console.log('Called api.setState("filter_2", ...). Value passed:', filterValue);

        // --- NEW LOGIC TO CONTROL THE DEFAULT VALUE / USER PREFERENCE SCRIPT ---
        // Set page state parameters to indicate the filter has been manually changed
        // This should prevent the evaluateProperty script from re-applying user preferences immediately.
        api.setState('filterChangedAgent', true);
        api.setState('prefValAgent', currentUserSysId); // Update prefValAgent with the new value
        console.log('Updated page state: filterChangedAgent = true, prefValAgent =', currentUserSysId);
        // --- END NEW LOGIC ---

    } else {
        console.warn('api.setState for filter_2 was NOT called because currentUserSysId was empty/null/undefined.');
    }

    console.log('--- Script Execution End ---');
}

 

 

0 REPLIES 0