passing server script to html in widget and fetching topic_id from url

Bhavani3
Tera Contributor

based on taxonmy topic , created record producer field in widget  shows  dropdown names 

 

Below i written server script, how to fetch topic_id from portal url(esc?id=emp_taxonomy_topic&topic_id=77b83e4cc3203010069aec4b7d40dd54) and replace it in below code of topic=0ce0fe08c3203010069aec4b7d40ddde

 

i tried pass gc.addQuery('catalog_item', $sp.getParameter("topic_id") ); but this not working well

 

server script

 

(function() {

data.catalog_items=[];


var grRP=new GlideRecord('m2m_connected_content');
grRP.addEncodedQuery("catalog_item.sys_class_name=sc_cat_item_producer^topic=0ce0fe08c3203010069aec4b7d40ddde");
//grRP.addEncodedQuery("catalog_item.sys_class_name=sc_cat_item_producer");
grRP.query();
while(grRP.next())
{
var inc;
inc=grRP.getValue('catalog_item');
data.catalog_items.push(inc);
}

2 ACCEPTED SOLUTIONS

Hi @Bhavani3, do you want to search items based on the topic, right?

You can enable AI Search for your instance.

Go to AI Search > AI Search Status
Enable AI Search

 

If you have a Utah instance, your employee center will have a search on the topic page

Vanderlei_0-1697825815815.png

This is an Out Of The Box functionality 

 

If my answer helped you, please mark my answer as helpful.

 

Vanderlei Catione Junior | LinkedIn

Senior ServicePortal Developer / TechLead at The Cloud People

 

View solution in original post

Hi @Bhavani3, If you really need to create a custom widget(I don't recommend because ServiceNow support only OOTB features ), you can try this

Server Script:

 

data.options = []


var grOptions = new GlideRecordSecure();
grOptions.addEncodedQuery();
grOptions.query()

while(grOptions.next()){
data.options.push({
name: grOptions.getDisplayValue(""),
id: grOptions.getValue("")
})

}

 

 

 

<select name="producers" id="producers-select" ng-change="c.redirect()" ng-bind="c.recordProducer">
  <option value="{{option.id}}" ng-repeat="option in data.options">{{option.name}}</option>
</select>

 

 

Client

 

c.redirect = function(){
window.open("?id=sc_cat_item&id="+c.recordProducer)//replace the id for your Catalog item Page
}

 

 

View solution in original post

9 REPLIES 9

SanjivMeher
Kilo Patron
Kilo Patron

Is topic id a catalog item? - gc.addQuery('catalog_item', $sp.getParameter("topic_id") );

Can you also print the value of the topic id as gs.info('$sp.getParameter("topic_id") -- '+$sp.getParameter("topic_id")).

This way we know if you are getting the right value

 


Please mark this response as correct or helpful if it assisted you with your question.

Vanderlei
Mega Sage

Hi, @Bhavani3 try this code

 

 

(function() {

data.catalog_items=[];
var topic = $sp.getParameter("topic_id")
var grRP=new GlideRecord('m2m_connected_content');
grRP.addEncodedQuery("topic="+topic+"^content_type=98f9a16553622010069addeeff7b1248^catalog_item.sys_class_name=sc_cat_item_producer^ORDERBYorder");
grRP.query();
while(grRP.next())
{
var inc;
inc=grRP.getValue('catalog_item');
data.catalog_items.push(inc);
}

 

 

But I still have some doubts
Where is this code used?
What is his objective?

If my answer helped you, please mark my answer as helpful.

 

Vanderlei Catione Junior | LinkedIn

Senior ServicePortal Developer / TechLead at The Cloud People

 

Hi @Vanderlei 

 

I created widget for in html i creted field name "record producer " drop down values and it placed in portal page .By connected connect table(m2m_connected_content)i get the mapping between record producer and topic .

In portal Based on topic_id url(esc?id=emp_taxonomy_topic&topic_id=77b83e4cc3203010069aec4b7d40dd54) ,need to display record producer names in widget and if we click record producer name then redirect record producer form in portal.

in below snapchat,based on topic this catalog item displayed in widget

Bhavani3_0-1697739834175.png

 

 

HTML code

<div class="record" aria-hidden="true" ng-model="name">
Select Record Producer:<sn-record-picker


id="name"
field="name"
table="'sc_cat_item_producer'"
display-field="'name'"
value-field="'sys_id'"
search-fields="'name'"
page-size="100">

</sn-record-picker>
</div>

Hi @Bhavani3, try this

<div class="record" aria-hidden="true" ng-model="name">
Select Record Producer:
<sn-record-picker field="name"
    table="'sc_cat_item_producer'"
    display-field="'name'"
    value-field="'sys_id'"
    search-fields="'name'"
    default-query="'taxonomy_topic={{c.your_sys_id}}'"
    page-size="100" 
></sn-record-picker>
</div>