- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2022 12:48 PM
Here is the code I have that verifies the value is synced. I added the if and for statement because it work 90% of the time and the other 10% failed. I think it might be a timing issue so I added the for and if statements however now it hangs - Thoughts?
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var cc = g_form.getReference('cost_center', populateReqForDetails);
function populateReqForDetails(cc) {
for (i = 0; i < 5; i = i++) {
if (cc == 'u_cost_center_code') {
return;
} else {
g_form.setValue('u_cost_center_code', cc.code);
}
}
}
//Type appropriate comment here, and begin script below
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-01-2022 08:17 AM
From any Department record, select Configure > Form Layout
From there, click the Cost center [+] field, then select the top icon in the middle to expand the Cost center [+] to show all fields on the Cost Center form.
Select the Code field and add it to your form.
You probably should make the field read only with a UI Policy so users do not change the Cost center record by mistake:
- Make Code Read Only
Table: Department [cmn_department]
Short description: Make Code Read Only
Description: Makes the Code field from the Cost Center record read only
Order: 100
Global: true
Reverse if false: true/false
On load: true/false
Inherit: true/false
Create a UI Policy Action and expand the Cost center field to get at the Code field
Make the UI Policy Action for the Code field to be Read only
Let me know if you still need assistance,

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2022 08:54 AM
Based upon what has been mentioned before, it sounds like you want to be able to fill in a variable in a catalog item form based upon the change of another variable. If this is the case, we can use Cost center [cost_center] as the reference variable that changes and Cost center code [cc_code] as the variable you set. You can change the variable names and labels to what you have built.
In order to use our AJAX call, we will need to create a Script Include that is client callable. For our example we will create a Script Include called reqUtilsAJAX:
Name: reqUtilsAJAX
Api Name: global.reqUtilsAJAX
Client callable: false
Application: Global
Accessible from: All application scopes
Description: Custom Include containing Catalog Request related methods.
Script:
var reqUtilsAJAX = Class.create();
reqUtilsAJAX.prototype = Object.extendsObject(AbstractAjaxProcessor, {
/**
* Gets the corresponding Cost center code from selected Cost center
* @param {string} sysparm_cid The sys_id of the Cost center
* @return {string} The corresponding Cost center code
*/
getgetCostCenterCode: function(sysparm_cid) {
var ccID = this.getParameter('sysparm_ccid');
var ccGr = new GlideRecord('cmn_cost_center');
ccGr.get(ccID);
var result = ccGr.u_cost_center_code;
return result;
},
type: 'reqUtilsAJAX'
});
Using this approach can also allow you to have other functions that can be used within the same Script Include.
The other part of this solution is to have a Catalog Client script that sets the cc_code variable called: Set Cost center code onChange
Name: Set Cost center code onChange
UI Type: All
Type: onChange
Variable name: cost_center
Applies on a Catalog Item view: true
Applies on Requested Items: false
Applies on Catalog Tasks: false
Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if (newValue == '') {
g_form.setValue('cc_code', '');
return;
}
if(newValue != '') {
var ga = new GlideAjax('getgetCostCenterCode');
ga.addParam('sysparm_name', 'getUsrDetails');
ga.addParam('sysparm_usr', newValue);
ga.getXML(setVariable);
}
}
function setVariable(serverResponse) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('cc_code', answer);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2022 12:54 PM
Additionally, the for loop will iterate through 5 loops so fast that it will not solve any timing issues.
You could try:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var cc = g_form.getReference('cost_center', populateReqForDetails);
function populateReqForDetails(cc) {
if(cc){
if (cc == 'u_cost_center_code') {
return;
} else {
g_form.setValue('u_cost_center_code', cc.code);
}
}
//Type appropriate comment here, and begin script below
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2022 12:57 PM
Hi
can you elaborate what you would like to achieve here? It seems both fields are on the same form (cost_center and u_cost_center_code) hence you don't need to use getReference at all.
getReference (well, rather AJAX) is used in case you want to get other parameter from different table
eg. an email of a caller
var usr = g_form.getReference('caller_id');
gs.info('User's email is ' + usr.email);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2022 01:14 PM
This is getting a reference from another table and putting in a user defined field as that is the only way I can seem to get this value in a list view on the cnm_department table

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-24-2022 12:52 PM
Have you thought of creating a new field that just dot-walks through the reference field on the user table?