How to auto populate the instance name in catalog form variable

Ajay Singh2
Tera Contributor

How to auto populate the instance name in catalog form variable like if its dev than display Dev when QA than display QA and when prod it should display PROD

1 ACCEPTED SOLUTION

SANDEEP28
Mega Sage

@Ajay Singh2 You can use below client script and script include

 

Script include : instanceDetails

 

SANDEEP28_0-1698916685180.png

 

In below script you can replace the actual keyword present in your instance name like dev, qa & prod under indexOf function

 

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

    getInstanceName:function(){
		var instanceName = gs.getProperty('instance_name');

		if (instanceName.indexOf('dev') > -1)
		return "Dev";

		if (instanceName.indexOf('qa')  -1)
		return "QA";

		if (instanceName.indexOf('prod') > -1)
		return "Prod";

	},
	

    type: 'instanceDetails'
});

 

Client script 

 

SANDEEP28_1-1698916798290.png

 

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

    var getInstanceDetails = new GlideAjax('global.instanceDetails');
    getInstanceDetails.addParam('sysparm_name', 'getInstanceName');
    getInstanceDetails.getXML(InstanceName);

    function InstanceName(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer")
        g_form.setValue('instance_name', answer); // where you want to set the instance name set in that field
    }
    

}

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

View solution in original post

6 REPLIES 6

Harsh_Deep
Giga Sage
Giga Sage

Hello @Ajay Singh2 

 

You can use this -

var instanceName = gs.getProperty('instance_name');

 

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.

yes i am using but issue is how to use in client script it is showing you can not use gs in client script because its server side. So please advise what to do in this case.

yaswanthi2
Giga Sage

Hi @Ajay Singh2 

If it auto populate when the form loads then you can try glide Ajax

 

Client Script

function onLoad() {
 var ga = new GlideAjax('myScriptUtils');
ga.addParam('sysparm_name', 'nameOfInstance');
 
ga.getXML(InstanceName);
 
function InstanceName(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('short_description', answer);  // where you want to set the instance name set in that field
}  
}
 
Script Include: 
var myScriptUtils = Class.create();
myScriptUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
nameOfInstance:function() { 
var instanceName = gs.getProperty('instance_name');
return instanceName; 
} ,
type: 'myScriptUtils'
});

I am using on change. Please see attached screenshot.

 

AjaySingh2_0-1698914503449.png