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

Hi @Vanderlei 

 

in portal i created field name as "select record producer" dropdown 

below table have "catalog item" field this is record producer and  i want to display this catalog item dropdown value  based on topic page in portal

Bhavani3_0-1697739834175.png

 

Portal -based on topic id yellow highlighted then in record producer field displaying the values

Bhavani3_1-1697816803631.png

 

html code

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

<div class="submit-btn">
<input class="btn btn-primary btn-block" ng-click="c.submit(catalog_item)" type="submit" value="Submit">
</div>

 

client script

c.submit=function(curr_item) {
url="/esc?id=sc_cat_item&sys_id=" + curr_item.value;
var win = window.open(url, '_blank');
win.focus();

 

above code i working on it but still facing issue

issue what i facing

1.when select catalog item and submit the widget then it not redirect to record producer form in portal,Is it possible to make to redirect to record producer form?

2.based on topic_id i need to show field dropdown values in portal?

what server script code functionality i need to given?

 

can you please help me on this

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

 

Hi @Vanderlei 

 

But I created custom widget for Record Producer dropdown field on each page of toipc portal, so Based on topic_id  on each page Record Producer dropdown values needs to show.

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
}

 

 

Hi @Vanderlei 

 

Thank you  for the above code ,its really  helpful me

 

small issue while submitting can you please help me on this ?

for above code i get output as name and thier sysid  from data.options

{"name":"Request Relocation Assistance","id":"2c6295d53b422200d901655593efc43d"}

{"name":"Beneficiaries Inquiry","id":"edec71099f231200d9011977677fcf93"}

 

for this all id example-id--2c6295d53b422200d901655593efc43d i need to fetch this sysid automatically  to url 

url="/esc?id=sc_cat_item&sys_id="-----i givend in client script

but when i submit this shown as error message(You are either not authorized or record is not valid.) like https://dev84821.service-now.com/esc?id=sc_cat_item&sys_id=undefined

I want to achieve like below url like

https://dev84821.service-now.com/esc?id=sc_cat_item&sys_id=2c6295d53b422200d901655593efc43d-----when i given submit then it needs to show like this

 

in client script

c.submit=function(curr_item) { 
 
url="/esc?id=sc_cat_item&sys_id=" + curr_item.value; // redirect to service portal link
var win = window.open(url, '_blank');
win.focus();
}
 
HTML code

<div>
{{data.options}}
<select name="catalog_item" id="catalog_item" ng-model="catalog_item" ng-required="true" class="form-control">
<option value="{{option.id}}" ng-repeat="option in data.options">{{option.name}}</option>
</select>


</div>

<div class="submit-btn">
<input class="btn btn-primary btn-block" ng-click="c.submit(catalog_item)" type="submit" value="submit">
</div>