Need a Button on the Catalog Item Form

TEJAS PAGARE1
Tera Contributor

Hello There,

 

On catalog item form I have few fields:

1) Registration Number (Primary Key)

2) ID

3) City

4) Country

 

We have an integration with third party tool and we have to make a query to their table and bring in (ID, City, Country) and auto-populate on the form.

 

Requirement (for an instance let suppose the third party tool table is ServiceNow' table):

Create all the four fields on the catalog item form and create a button (using custom variable with widget) as 'Search' beneath Registration Number, user will enter Registration Number and click on the 'Search' button which will pass that into GlideRecord query to that table and if match Registration Number match found it should auto-populate subsequent fields ID, City, Country. Need a code for widget to achieve this functionality

3 REPLIES 3

Rafael Batistot
Kilo Patron

Hi @TEJAS PAGARE1 

 

Create these variables on the catalog item:

 

Registration Number

registration_number

Single Line Text

ID

ext_id

Single Line Text

City

city

Single Line Text

Country

country

Single Line Text

Search

search_widget

Custom Widget

 

in widget:

 

SERVER 

 

(function () {
input && input.registrationNumber
? data.result = fetchRecord(input.registrationNumber)
: data.result = null

function fetchRecord(registrationNumber) {
var gr = new GlideRecord('u_third_party_table')
return gr.get('u_registration_number', registrationNumber)
? {
id: gr.getValue('u_id'),
city: gr.getValue('u_city'),
country: gr.getValue('u_country')
}
: {}
}
})()

 

CLIENT 

 

function ($scope) {
$scope.search = function () {
var reg = g_form.getValue('registration_number')

reg
? $scope.server.get({ registrationNumber: reg }).then(applyResult)
: clearFields()
}

function applyResult(response) {
var r = response.data.result

r && r.id
? (
g_form.setValue('ext_id', r.id),
g_form.setValue('city', r.city),
g_form.setValue('country', r.country)
)
: clearFields()
}

function clearFields() {
g_form.setValue('ext_id', '')
g_form.setValue('city', '')
g_form.setValue('country', '')
}
}

 

HTML 

 

<div class="form-group">
<button class="btn btn-primary" ng-click="search()">Search</button>
</div>

 

If this response was helpful, please mark it as Helpful and, if applicable, as Correct, this helps other users find accurate and useful information more easily.

AndersBGS
Tera Patron

Hi @TEJAS PAGARE1 

 

I would advise against customizing the widget for the catalog items. Why not just create a client script and script include if you need to search in database? By doing that, the system can search based on the input and automatic provide the value .

 

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Anders

Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/

Ankur Bawiskar
Tera Patron

@TEJAS PAGARE1 

you can have onChange catalog client script running on 1st variable

Then use GlideAjax and Script include to make the API Call and bring the data and then populate it

where are you stuck?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader