Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-08-06· OpenAIAi2 WildChat-4.8M (AI2)

in catalog item there is one variable i need to see only active records in that servicenow

  1. User

    in catalog item there is one variable i need to see only active records in that servicenow

  2. Assistant

    To view only active records for a particular catalog item in ServiceNow, you would typically apply a filter based on the Active field within the relevant table. Here’s a step-by-step guide to view only active records:

    1. Navigate to the Correct Module:

      • Log in to your ServiceNow instance.
      • Go to the module that contains the records you want to filter. For catalog items, you might be working within the Service Catalog > Catalog Definitions > Maintain Items module.
    2. Open the List View:

      • Once you're in the correct module, switch to the list view of the records (if you're not there already).
    3. Create a Filter:

      • At the top of the list, you’ll typically find a filter bar. If it's not visible, click on the filter icon or "Show Filter" to display it.
      • Add a filter condition:
        1. Select the field you want to filter by. For active records, you'll use the field usually called Active (or similar, depending on customization).
        2. Set the condition to Active is true or select Active and enter true or click the checkbox depending on your instance setup.
    4. Apply the Filter:

      • After setting up your filter conditions, click the search or filter button to apply the filter. The list should now display only the records where the Active field is true.
    5. Save the Filter (Optional):

      • If you regularly need to view active items, you can save this filter for future use. Look for a save or bookmark option in the filter interface.

    This process allows you to focus on active records and is a fundamental feature of ServiceNow's list functionality. If you're working in a custom table or the ServiceNow interface has been significantly customized, the exact steps may vary slightly.

  3. User

    In catalog item there is one variable reference to alm hardware table and all hardware records is showing in this. I want to show only active records to the users if they select any value. How to do it via script

  4. Assistant

    To restrict a reference variable in a ServiceNow catalog item to display only active records from the alm_hardware table, you can use a Reference Qualifier. You can define a scripted Reference Qualifier to filter the results dynamically. Here’s how you can do this:

    1. Navigate to the Catalog Item Form:

      • Log in to your ServiceNow instance.
      • Navigate to the Service Catalog and find the catalog item you are working on.
    2. Open the Variable:

      • Open the record for the catalog item and go to the variables tab.
      • Find and open the reference variable that points to the alm_hardware table.
    3. Set the Reference Qualifier:

      • In the variable record, look for the field labeled "Reference qualifier."
      • Set it to "Advanced" to enable scripting.
    4. Write a Script for the Reference Qualifier:

      • Click the "Reference qualifier script" field to open the script editor.
      • Write a script to filter out only active records. Here's a simple example:
      // The script must return a query string to filter records
      answer = 'active=true';
      
    5. Save Changes:

      • Save or update the variable record.

    This script ensures that only records from the alm_hardware table with active=true are shown in the reference field dropdown when users interact with the catalog item.

    Important Considerations:

    • Permissions: Ensure that users have the appropriate permissions to see and query the alm_hardware table and its fields.

    • Data Consistency: Make sure that the Active field used in the script is the correct field name and is consistently maintained across the records in the alm_hardware table.

    • Testing: After implementing the script, test it thoroughly with different user roles to confirm that the filtering behaves as expected.

    By using a scripted reference qualifier, you can dynamically control which records appear, enhancing the user experience and maintaining data relevance.

  5. User

    I am not able to edit reference qualifiers , please suggest any other method

  6. Assistant

    If you are unable to edit the reference qualifiers directly through the catalog item variable configuration in ServiceNow, you might still have a few alternative approaches to achieve filtering:

    Use a Client Script:

    1. Create a Client Script:

      • Navigate to the catalog item and go to the Catalog Client Scripts related list.
      • Click "New" to create a new client script.
    2. Configure the Client Script:

      • Type: Set this to "onLoad" or "onChange", depending on when you want the filter to be applied.
      • Applies To: Catalog Item.
      • Name: Provide a meaningful name for the script.
      • Script: Implement a script that filters the reference field using GlideForm. Below is a sample script to achieve the restriction:
      function onLoad() {
          // 'current' is the GlideForm instance representing the current catalog item form
          var ga = new GlideAjax('GetActiveHardware'); // Server-side script include
          ga.addParam('sysparm_name', 'getActiveHardware');
          ga.getXMLAnswer(function(response) {
              var filter = response || ''; // Default to empty string if no response
              // Replace 'your_reference_variable_name' with the actual name of your reference variable
              g_form.setDependent('your_reference_variable_name', filter);
          });
      }
      
    3. Create a Script Include:

      • If you're using GlideAjax, you need a script include on the server-side to return the filtered sys_ids or a query:
      var GetActiveHardware = Class.create();
      GetActiveHardware.prototype = Object.extendsObject(AbstractAjaxProcessor, {
      
          getActiveHardware: function() {
              // Craft a query to fetch active hardware sys_ids
              var query = 'active=true';
              var gr = new GlideRecord('alm_hardware');
              gr.addEncodedQuery(query);
              gr.query();
              var sysIdList = [];
              while (gr.next()) {
                  sysIdList.push(gr.getValue('sys_id'));
              }
              // Return the sys_id list as a semicolon-delimited string, or adapt to your needs
              return sysIdList.join(',');
          }
      });
      

    Considerations:

    • Client Scripting Limitations: It's essential to ensure your users have appropriate permissions to execute scripts, and client scripts should be optimized for performance.

    • Test Thoroughly: After implementing changes, test extensively across different user roles to ensure the functionality works as expected and does not introduce any performance issues or errors on the catalog form.

    This approach using client scripts combined with a server-side Script Include can help you filter the reference fields even when direct editing of the reference qualifier is restricted or non-functional due to permissions or other constraints.