The CreatorCon Call for Content is officially open! Get started here.

Pradyumna Das
Tera Expert

We can call AI search in server side script.

  1. ServiceNow has provided one OOB script include(AISASearchUtil).
  2. Before calling that script include make sure you are in global scope.
  3. In the below script I have extracted KB articles from the response of the AI search Script include. Similarly we can extract other results as well from AI search.

add below script in background script 

function doAISearch(searchTerm){
    /**
    * Description: By using AI search will get all the data
    * @Param1 :{String} : search string 
    * @returns:{JSON}   : reponse of AI search API
    */
    var data = {
        "rpSysId": 'SYS_ID', //aisa_rp_config  - Record Producer Configurations (OOB one record will be present add tahat sys_id)
        "searchContextConfigId": 'SYS_ID', //sys_search_context_config -  Search Applications Configuration(created your AI search configuration)
        "searchTerm": searchTerm
    };
    return new global.AISASearchUtil().search(data);
}
function getKBFromAISearch(obj) {
        /**
         * Description: function to retreive all the kb related information from the JSON
         * @Param1 :{JSON}  : AI Search response
         * @returns:{JSON}  : custom JSON with all the kb details
         */
        var result = {};
        var kb = [];
        var query = [];
        if (obj.status == 200 || obj.status == '200') {
            for (var i in obj.data.search.searchResults) {
                if (obj.data.search.searchResults[i].table == 'kb_knowledge') {
                    var tempJSON = {};
                    tempJSON.sysId = obj.data.search.searchResults[i].sysId + '';
                    query.push(tempJSON.sysId);
                    tempJSON.table = obj.data.search.searchResults[i].table + '';
                    for (var j in obj.data.search.searchResults[i].columns) {
                        if (obj.data.search.searchResults[i].columns[j].fieldName == 'short_description') {
                            tempJSON.title = obj.data.search.searchResults[i].columns[j].displayValue + '';
                        }
                        if (obj.data.search.searchResults[i].columns[j].fieldName == 'number') {
                            tempJSON.number = obj.data.search.searchResults[i].columns[j].displayValue + '';
                        }
                    }
                    kb.push(tempJSON);
                }
            }
        }
        result.query = 'sys_idIN' + query.toString();
        result.kb = kb;
        return result;
    }

gs.info(JSON.stringify(getKBFromAISearch(doAISearch('WVD issue'))));

Expected Output:

{
"query":"sys_idINe19186311b8f911495901178b04bcbe3,a86ed1cf1b9195d4834d9978b04bcb95,50e215581b37b8d495901178b04bcb2f,38330e311bcf911495901178b04bcb8f,989e401f1b6db010ab948594e34bcbb2,78c72e591b3c5550834d9978b04bcb3d,5d4dc3ed1b23a810d8a7a791f54bcb22,9283b2b91bb8fd904e9cdb98bd4bcbe4,6ee6f95b1b349510834d9978b04bcb5d,e96b70da1b94c11495901178b04bcb54",
   "kb":[
      {
         "sysId":"e19186311b8f911495901178b04bcbe3",
         "table":"kb_knowledge",
         "title":"Known Issue in WVD - IBM Users ",
         "number":"KB0022364"
      },
      {
         "sysId":"a86ed1cf1b9195d4834d9978b04bcb95",
         "table":"kb_knowledge",
         "title":"WVD Reference Guide FAQs",
         "number":"KB0023376"
      },
      {
         "sysId":"50e215581b37b8d495901178b04bcb2f",
         "table":"kb_knowledge",
         "title":"Reference guide for SAP AFO issue in Windows Virtual Desktop (WVD)",
         "number":"KB0023096"
      },
      {
         "sysId":"38330e311bcf911495901178b04bcb8f",
         "table":"kb_knowledge",
         "title":"Windows Virtual Desktop (WVD) - Using Microsoft Teams in WVD",
         "number":"KB0023518"
      },
      {
         "sysId":"989e401f1b6db010ab948594e34bcbb2",
         "table":"kb_knowledge",
         "title":"Windows Virtual Desktop (WVD) FAQs",
         "number":"KB0022126"
      },
      {
         "sysId":"78c72e591b3c5550834d9978b04bcb3d",
         "table":"kb_knowledge",
         "title":"How to add to WVD Security groups",
         "number":"KB0023751"
      },
      {
         "sysId":"5d4dc3ed1b23a810d8a7a791f54bcb22",
         "table":"kb_knowledge",
         "title":"Windows Virtual Desktop (WVD) – Service Desk reference guide",
         "number":"KB0022129"
      },
      {
         "sysId":"9283b2b91bb8fd904e9cdb98bd4bcbe4",
         "table":"kb_knowledge",
         "title":"Windows Virtual Desktop (WVD) - configuration & general troubleshooting",
         "number":"KB0023517"
      },
      {
         "sysId":"6ee6f95b1b349510834d9978b04bcb5d",
         "table":"kb_knowledge",
         "title":"Updating accurate Category/Sub Category/Category Item for WVD related tickets",
         "number":"KB0023724"
      },
      {
         "sysId":"e96b70da1b94c11495901178b04bcb54",
         "table":"kb_knowledge",
         "title":"Teams Audio/Video not working on WVD",
         "number":"KB0023574"
      }
   ]
}

 

Please mark this article helpful if it helps you in any way.

Thanks, 

Pradyumna Das

Comments
Rajeev Vunukond
Tera Guru

it's very helpful, Can we call this API from External source?

Jan Moser
Tera Expert

@Pradyumna Das Where do we set the order of the results? It should be by Relevancy by default, but do you know where is the place of this settings? Our results seems little bit weird so I would like to check how the sorting is configured. Thank you. Jan

Version history
Last update:
‎08-24-2023 09:23 AM
Updated by:
Contributors