Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-mini-2024-07-18· OpenAIAi2 WildChat-4.8M (AI2)

in servicenow, i have one script include that will return the records based on sub asset…

  1. User

    in servicenow, i have one script include that will return the records based on sub asset type and i working correctly, my requirement is when the sub asset type is Surface Pro then it will return Laptop Sub Assets also, and for any other sub asset types it is working as same . I provide you my Script include please suggest if it possible or not.

    pdateAssetConsume: function(clocation,assettype){ var assetArr = []; var assetGr = new GlideRecord('alm_hardware'); //Clocation is stockroom from it fetch state and then from alm_hardware fetch all asset of that state var t=new GlideRecord('cmn_location'); t.addQuery('sys_id',clocation); var state; t.query(); if(t.next()){ state=t.state; } assetGr.addEncodedQuery('install_status=6^substatusINAvailable,available,reserved^location.state=' + state.toString()); assetGr.addQuery('u_asset_type', assettype.toString()); // install_status=6^substatusINavailable,reserved

    assetGr.query(); while(assetGr.next()){ assetArr.push(assetGr.sys_id.toString()); }

    return 'sys_idIN' + assetArr;

    },

  2. Assistant

    To modify your existing Script Include function so that when the sub asset type is "Surface Pro", it also returns records for both "Surface Pro" and "Laptop" sub asset types, you can adjust the query logic slightly. You want to achieve this without changing your existing structure significantly.

    Firstly, you would check if the assettype is "Surface Pro" and then adjust the query to include "Laptop" as well. Here’s a revised version of your updateAssetConsume function:

    updateAssetConsume: function(clocation, assettype) {
        var assetArr = [];
        var assetGr = new GlideRecord('alm_hardware');
    
        // Fetch state from location based on `clocation`
        var t = new GlideRecord('cmn_location');
        t.addQuery('sys_id', clocation);
        var state;
        t.query();
        if (t.next()) {
            state = t.state;
        }
    
        // Build the query for alm_hardware
        assetGr.addEncodedQuery('install_status=6^substatusINAvailable,available,reserved^location.state=' + state.toString());
    
        // Adjust query based on assettype
        if (assettype == 'Surface Pro') {
            // If assettype is Surface Pro, include both Surface Pro and Laptop
            assetGr.addQuery('u_asset_type', 'Surface Pro').addOrCondition('u_asset_type', 'Laptop');
        } else {
            // For other asset types, just query the specified type
            assetGr.addQuery('u_asset_type', assettype.toString());
        }
    
        // Execute the query and collect the results
        assetGr.query();
        while (assetGr.next()) {
            assetArr.push(assetGr.sys_id.toString());
        }
    
        return 'sys_idIN' + assetArr; 
    }
    

    Explanation of Changes:

    1. Conditional Check: When the assettype is "Surface Pro", we use addOrCondition to include "Laptop" in the query. This allows you to fetch both Surface Pro and Laptop records.
    2. Standard Query for Other Asset Types: If the assettype is anything else, it simply queries for that specific type as in your original implementation.

    Usage:

    Make sure to test the changes thoroughly to ensure the modified logic captures all the necessary records as per your requirements.