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

Hi @Ajay Singh2 

if it onchange then use onchange method as per your requirement and try below code

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

if(newValue !=''){ //newValue is onchange of your field

 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); 
}  
}
 
Script Include: 
var myScriptUtils = Class.create();
myScriptUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
nameOfInstance:function() { 

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

return instanceName; 

} ,

type: 'myScriptUtils'
});

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 !!