Api key in client script

kiwi07
Tera Contributor

I am trying to create a catalog and my requirement is to create 2 values in the catalog

1. a referencefield whose value will be from sys_alias table for which I have created 1 record in connection & credential alias and have provided an API key for the same

2. a lookup select field whose values will get populated onchange of the reference filed  

 

I am trying to write a client script onchange of the first field and trying to make a rest api call in the script , but the challenge is how do I pass the api key from the first field so that I can authenticate the api and send the response .

 

I have tried the below option but they don't provide any useful info 

alert(g_form.getDisplayValue('alias_selected'));
alert(g_form.getValue("alias_select"));

 

is there a way to achieve this or any other alternate that i can try 

1 ACCEPTED SOLUTION

@kiwi07 Okay Got it. Gliderecord to the API key table where they are stored and pass the alias name and return the API key

Example:

IF servicenow is alias

OmkarKumbhar_0-1674469409320.png

 

Key will be stored in below connection table

OmkarKumbhar_1-1674469458317.png

Gliderecord connection table with the name as Servicenow alias and return the API key

If I was able to help you with your case, please click the Thumb Icon and mark as Correct.

View solution in original post

9 REPLIES 9

Omkar Kumbhar
Mega Sage
Mega Sage

Hello @kiwi07 ,

You can use the glideAjax in the client script pass the selected value from the Client script to Script include and make a call in SI and return the value to client script set the field value.

 

Client script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}

	var user = g_form.getValue('reference field ');

	//Call script include
	var ga = new GlideAjax('global.sampleUtils');   //Scriptinclude
	ga.addParam('sysparm_name', 'getUserDetails'); //Method
	ga.addParam('userId',user); //Pass the value of reference field
	ga.getXMLAnswer(getResponse);
	
	function getResponse(response){
		console.log(response);
		var res = JSON.parse(response);
		console.log(res);
				g_form.setValue('aliasselect');
	}
	
}

 

Script include:

var sampleUtils = Class.create();
sampleUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getAPIDetails: function(){
		
		
		var userId = this.getParameter('userId');
			
	//Call you rest API here and retutn the value 
	gs.addInfoMessage(obj+JSON.stringify(obj));
	return JSON.stringify(obj);
	},

	type: 'sampleUtils'
});

This is just a sample script please modify according with your requirement and call Rest api in Script include part

 

Thanks,

Omkar

If I was able to help you with your case, please click the Thumb Icon and mark as Correct.

@Omkar Kumbhar  I am a bit confused with the answer in the first reference filed if alias is selected where do we get the API key from the alias to make the api call ?

@kiwi07  Can you please clarify your question

Are you doing any integration with rest message to bring the API key and populate it on the form

OR

Are you just populating the field based on the Alias select field 

 

Can you share the share shots it will be helpful

If I was able to help you with your case, please click the Thumb Icon and mark as Correct.

@Omkar Kumbhar  Below is the catalog I have, the first field is an alias field, I have created one alias "sample_alias", and when I select the alias I want to make a REST call onchange using the api key I have given in the alias while creation. Currently the data in the second field Building Name is static and i want to populate from the rest response , the challenge is that user can select any alias that he has created and the REST response is different for diffrent alias

For Example :-

Alias:- sample_alias

Build Name :- A,B,C

 

Alias:- sample_alias_new

Building Name:- 1,2,3

 

kiwi07_2-1674464104571.png

kiwi07_3-1674464310080.png

kiwi07_4-1674464551020.png

kiwi07_5-1674464613183.png