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

@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