- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2021 10:24 AM
I have been working on several on-change client scripts to make this work, but have so far been unsuccessful. The task to make two fields that share the same table populate with their respective values, based-on the value of another field from the same table.
Below, is my script include.
How do I go about writing an on-change CS to make this work?
Thank you!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2021 03:41 PM
This is what I have defined in my PDI:
HHave added the fields to the Change Request form:
After flushing the cache, when I change System, Analyst and Owner automatically update.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2021 03:15 PM
The field can be named anything, don't have to be the same as the referenced table at all. Of course form a maintenance/development perspective it is expected that a field that is a reference to table Owner to be called Owner, but that's all. From a functionality perspective there is absolutely no relation or dependency between a reference field's name and the table referenced. E.g. on User table the field manager points to "itself", is a reference to User, but it is called, well, Manager.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2021 05:20 PM
Thanks so much! This is making better sense. I am still getting a "response is null" on System change, even after verifying all field names are correct. I am going to step away for a few and approach this again with a clear brain... Hopefully that helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2021 05:24 PM
In the mean time you could post your code, maybe the fault will be spotted.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2021 07:21 PM
Here it is...
CS:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('GetSysName'); // Script Include Name
ga.addParam('sysparm_name', 'getName'); // SI Method
ga.addParam('sysparm_system_unique_value', newValue); // Parameter for SI
ga.getXMLAnswer(function (response) {
alert('response is ' + response);
var answer = JSON.parse(response);
g_form.setValue('u_system_owner', answer.owner.uniqueValue, answer.owner.displayValue);
g_form.setValue('u_analyst', answer.analyst.uniqueValue, answer.analyst.displayValue);
});
}
SI:
GetSysName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getName: function () {
var gn = {};
var sn = this.getParameter('sysparm_system_unique_value');
var rec = new GlideRecord('cmdb_application_product_model');
if (rec.get(sn)) {
gn.owner = {
'uniqueValue': '' + rec.owner,
'displayValue': rec.owner.getDisplayValue(),
};
gn.analyst = {
'uniqueValue': '' + rec.u_analyst,
'displayValue': rec.u_analyst.getDisplayValue(),
};
}
return JSON.stringify(gn);
},
type: 'GetSysName'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-01-2021 12:24 AM
Couple of things to verify:
- the name of the script include is the same as the name in code (GetSysName
)
- the script include is marked as "Client callable"
- the script include begins with line
var GetSysName = Class.create();
A very big problem is that the method is called getName
. In fact the following names should not be (re)used in Script Includes that are extending AbstractAjaxProcessor
: CALLABLE_PREFIX
, gc
, getChars
, getDocument
, getName
, getParameter
, getRootElement
, getType
, getValue
, newItem
, process
, request
, responseXML
, setAnswer
, setError
. This would be the 1st thing I'd correct. Creating functions with any of those names will render the Ajax handler Script Include non-functional.
Another potential problem could be field System on Change Request or rather how it is defined. It should be a reference to table cmdb_application_product_model
- is it? In your very 1st screen-shot it is not. Have you modified that?