Set variable value and make variable read only

Michael Nash
Giga Expert

Hi,

 

I need some help with my script below.

 

I'm trying to set a catalogue item variable cost_centre to the cost centre "SSER Southern Europe" and make it read only if the value of the variable selected in location variable has a location source of "Galileo"

 

Location variable is a Reference to cmn_location

Cost Centre is a  Reference to cmn_cost_center

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
// Check if the field is being loaded or cleared
if (isLoading || newValue === '') {
return;
}

// Get the value of the location field
var locationSysId = g_form.getValue('location');

// Get the location record
var location = new GlideRecord('cmn_location');
location.get(locationSysId);

// Get the value of the location_source field
var locationSource = location.getValue('cmn_location_source');

// If the location_source is Galileo, set the name of the cost center variable to "SSER Southern Europe"
if (locationSource === 'Galileo') {
var costCentreName = "SSER Southern Europe";
g_form.setValue('cost_centre', costCentreName);
g_form.setReadOnly('cost_centre', true);
} else {
// If the location_source is not Galileo, clear the value of the cost_centre variable and make it editable
g_form.setValue('cost_centre', '');
g_form.setReadOnly('cost_centre', false);
}

1 ACCEPTED SOLUTION

@Michael Nash 

are you sure you are comparing choice value then?

update as this

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
// Check if the field is being loaded or cleared
if (isLoading || newValue === '') {
return;
}

// Get the value of the location field
var locationRef = g_form.getReference('location', callBackMethod);

}

function callBackMethod(locationRef){

// Get the value of the location_source field
var locationSource = locationRef.cmn_location_source;


// If the location_source is Galileo, set the name of the cost center variable to "SSER Southern Europe"
if (locationSource === 'Galileo') {
var costCentreName = "SSER Southern Europe";
g_form.setValue('cost_centre', costCentreName);
g_form.setReadOnly('cost_centre', true);
} else {
// If the location_source is not Galileo, clear the value of the cost_centre variable and make it editable
g_form.setValue('cost_centre', '');
g_form.setReadOnly('cost_centre', false);
}
}

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

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@Michael Nash 

it's not recommended to use GlideRecord in client script

I assume cmn_location_source field name is correct and holds the string value

try this

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	// Check if the field is being loaded or cleared
	if (isLoading || newValue === '') {
		return;
	}

	// Get the value of the location field
	var locationRef = g_form.getReference('location', callBackMethod);

}

function callBackMethod(locationRef){

	// Get the value of the location_source field
	var locationSource = locationRef.getValue('cmn_location_source');

	// If the location_source is Galileo, set the name of the cost center variable to "SSER Southern Europe"
	if (locationSource === 'Galileo') {
		var costCentreName = "SSER Southern Europe";
		g_form.setValue('cost_centre', costCentreName);
		g_form.setReadOnly('cost_centre', true);
	} else {
		// If the location_source is not Galileo, clear the value of the cost_centre variable and make it editable
		g_form.setValue('cost_centre', '');
		g_form.setReadOnly('cost_centre', false);
	}
}

 If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Ankur Bawiskar 

 

I tried his but it is still not updating the cost centre variable.

 

MichaelNash_1-1680778686264.png

 

 

 

 

@Michael Nash 

so it's not a string field. It's a reference field I guess.

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

the variable is a refrence field, but location source is a String field on the location table.

 

MichaelNash_0-1680780841674.png