- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-25-2015 04:55 AM
Im sorry to ask again but need help and also need to learn how to script this demand.
I can find way how to populate service on incident form if SD user select last appllication on the image below - 3
I havent luck to buil onchange client script - Im able to build script which returns me only direct relation ( from 3 to 2 ) but not what we need ( from 3 to X)
would somebody give me a lesson here please ? Please note that I need onchange solution, so service is prefilled always when suer change CI
My system fields are
cmdb_ci -- for apps
u_service_ci -- for service
thank you
/Petr
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-01-2015 07:22 AM
Hi Petr,
Nice work on the GlideAjax script. I do have a few recommendations that can improve your script and hopefully help you learn a little more about GlideAjax in the process (you can also check out my infographic on GlideAjax for additional info: Demystifying GlideAjax).
So first, the scripts (please bear in mind I haven't fully tested these yet so they may need some tweaks):
Client Script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var ooo = g_form.getValue('cmdb_ci'); // this is the sys id of CI entered by user
var ga = new GlideAjax('BusinessServiceFinder');
ga.addParam('sysparm_name', 'getServiceForCI');
ga.addParam('sysparm_ci_sys_id', ooo); // passing CI sys_id to script include
ga.getXML(setBusinessService);
function setBusinessService(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
g_form.setValue('u_service_ci', answer); // get answer from Script Include and set Service CI field
alert(answer);
}
}
Script Include
var BusinessServiceFinder = Class.create();
BusinessServiceFinder.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getServiceForCI: function() {
var this_is_variable_from_form = this.getParameter('sysparm_ci_sys_id') + '';
return this._getParentRelationship(this_is_variable_from_form);
},
_getParentRelationship: function(ciSysId) {
var o = new GlideRecord('cmdb_rel_ci');
o.addQuery('child', ciSysId);
o.query();
if (o.next()) {
if (o.parent.sys_class_name == 'cmdb_ci_service') {
return o.getValue('parent'); // return the CI's sys_id
}
else {
return this._getParentRelationship(o.getValue('parent')); // recursively execute the function for the parent's sys_id
}
}
else {
return 'There is no direct relation to service';
}
},
type: 'BusinessServiceFinder'
})
Changes
1. You will notice that I changed the names of the Script Include and its function. You can change these names as you desire, but with GlideAjax there is a convention that you must follow:
- The name of the Script Include (BusinessServiceFinder) must be included inside the parenthesis of the GlideAjax call as shown on line 7 of the Client Script. This tells GlideAjax which Script Include to use.
- The name of the Script Include's function (getServiceForCI) must be included in the Client Script by setting it as the sysparm_name parameter as shown on line 8 of the Client Script.
2. You will also notice that I added a private function _getParentRelationship and changed the getServiceForCI function to use it. The _getParentRelationship is a recursive function that calls itself. This eliminates the need for the nested if statements and reduces the duplicated code. Basically, the _getParentRelationship function will keep calling itself by passing the parent CI in the relationship until it finds a Business Service or is unable to find a relationship. The advantage in this script is that it will work for any sized relationship tree depth. If you were to at some point in the future link relationships 6 nodes from the Business Service, this script will still work.
I hope this helps. And thank you deepak.ingale for looping me in!
Kind regards,
Travis
EDIT: A few typos, missing punctuation, wrong punctuation, and other miscellaneous errors later, this script has now been tested. Thanks guys!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-25-2015 05:07 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-30-2015 09:25 PM
Hello, looks that I have solved it. maybe there exist better way how to write code for script include below, but it works for us and it doesnt slow down form.
If any idea how to improve script include code below, please share it with us so we and others can learn, thank you
PS: I have learned from script include on wiki, therefore i kept the same script include name - GlideAjax - ServiceNow Wiki
/Petr
Client script
Script Include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-01-2015 04:21 AM
Hi Petr,
Script looks good, you will have to change the name though to something relevant.
Also, I am not expert in Ajax calls but its looks good.
Travis Toulson is an expert in this area, I had seen his community blog only to learn GlideAjax calls.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-01-2015 07:22 AM
Hi Petr,
Nice work on the GlideAjax script. I do have a few recommendations that can improve your script and hopefully help you learn a little more about GlideAjax in the process (you can also check out my infographic on GlideAjax for additional info: Demystifying GlideAjax).
So first, the scripts (please bear in mind I haven't fully tested these yet so they may need some tweaks):
Client Script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var ooo = g_form.getValue('cmdb_ci'); // this is the sys id of CI entered by user
var ga = new GlideAjax('BusinessServiceFinder');
ga.addParam('sysparm_name', 'getServiceForCI');
ga.addParam('sysparm_ci_sys_id', ooo); // passing CI sys_id to script include
ga.getXML(setBusinessService);
function setBusinessService(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
g_form.setValue('u_service_ci', answer); // get answer from Script Include and set Service CI field
alert(answer);
}
}
Script Include
var BusinessServiceFinder = Class.create();
BusinessServiceFinder.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getServiceForCI: function() {
var this_is_variable_from_form = this.getParameter('sysparm_ci_sys_id') + '';
return this._getParentRelationship(this_is_variable_from_form);
},
_getParentRelationship: function(ciSysId) {
var o = new GlideRecord('cmdb_rel_ci');
o.addQuery('child', ciSysId);
o.query();
if (o.next()) {
if (o.parent.sys_class_name == 'cmdb_ci_service') {
return o.getValue('parent'); // return the CI's sys_id
}
else {
return this._getParentRelationship(o.getValue('parent')); // recursively execute the function for the parent's sys_id
}
}
else {
return 'There is no direct relation to service';
}
},
type: 'BusinessServiceFinder'
})
Changes
1. You will notice that I changed the names of the Script Include and its function. You can change these names as you desire, but with GlideAjax there is a convention that you must follow:
- The name of the Script Include (BusinessServiceFinder) must be included inside the parenthesis of the GlideAjax call as shown on line 7 of the Client Script. This tells GlideAjax which Script Include to use.
- The name of the Script Include's function (getServiceForCI) must be included in the Client Script by setting it as the sysparm_name parameter as shown on line 8 of the Client Script.
2. You will also notice that I added a private function _getParentRelationship and changed the getServiceForCI function to use it. The _getParentRelationship is a recursive function that calls itself. This eliminates the need for the nested if statements and reduces the duplicated code. Basically, the _getParentRelationship function will keep calling itself by passing the parent CI in the relationship until it finds a Business Service or is unable to find a relationship. The advantage in this script is that it will work for any sized relationship tree depth. If you were to at some point in the future link relationships 6 nodes from the Business Service, this script will still work.
I hope this helps. And thank you deepak.ingale for looping me in!
Kind regards,
Travis
EDIT: A few typos, missing punctuation, wrong punctuation, and other miscellaneous errors later, this script has now been tested. Thanks guys!