- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-26-2014 12:31 PM
Hello,
I am new in scripting and need to create a script onChange when a CI is chosen it should assign it to a specific category. Below is a script I have been playing around with but when I un-choose the CI it does remove the current category.
//Set Change Category depending on CI
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var oracle = g_form.getValue('cmdb_ci').indexOf("tg france oracle") > -1;
if (oracle == true){
category == "ERP Software";
}
else{
category == '';
return;
}
}
Thanks for the enlightenment
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-29-2014 06:07 AM
Hi Darlene,
So sorry, I made a really dumb error. I have set up a scenario in one of my instances to test this one out and the following script should work for you:
//Set Change Category depending on CI
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
return;
}
g_form.getReference('cmdb_ci', setCategory);
function setCategory(ci) {
// ci contains the GlideRecord on the cmdb_ci table
var erp = (ci.name + '').toLowerCase().indexOf("tg france oracle") > -1;
erp = erp || ((ci.name + '').toLowerCase().indexOf("tg france discover") > -1);
var applSoftware = (ci.name + '').toLowerCase().indexOf("tg france windchill") > -1;
applSoftware = applSoftware || ((ci.name + '').toLowerCase().indexOf("tg france adp") > -1);
if (oracle == true){
g_form.setValue("category", "ERP Software");
}
else if (applSoftware == true) {
g_form.setValue("category", "Applications Software");
}
else{
g_form.setValue("category", "");
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-26-2014 12:55 PM
Hi Darlene,
Did you mean to say that it "doesn't remove the current category"? I imagine that should be the case. If you want the category to be cleared when the CI is cleared, use the following script:
//Set Change Category depending on CI
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
return;
}
var oracle = g_form.getValue('cmdb_ci').indexOf("tg france oracle") > -1;
if (oracle == true){
category == "ERP Software";
}
else{
category == '';
return;
}
}
You will note that I made a change to line 3 by removing newValue =="". The variable newValue holds the value that you just selected in the CI field. So if you cleared the field, newValue is empty which in this case is represented by the empty string "".
On line 3, the onChange script will run as follows when you clear the CI Field:
1. LINE 3: If the form is loading OR if newValue is an empty string (newValue IS an empty string when you clear the field, so the if block gets executed starting with line 4
2. LINE 4: return from the function
Nothing else gets executed because you have returned from the function, thus the category field never gets reset.
I hope this helps. Kind regards,
Travis
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-26-2014 01:24 PM
Thanks for the feedback Travis. I apologize if my query what somewhat confusing.
In the Change form, my intention is:
- if a user chooses a CI that has TG France Oracle or TG France Discoverer it will automatically change the category to "ERP Software";
- if a user chooses a CI that has TG France Windchill or TG France ADP it will automatically change the category to "Applications Software"
- if the user chooses neither of the options (or chose on on top but changed it to neither of the options) it will change the category to "-- Choose --" (What I set as the NULL override)
Originally, I created the script which is working:
But I wanted to go the extra step as there are alot of CIs that begin with TG France Oracle and I want the category to reset if the user changes the CI to something else so I created the script below:
I tried the script you recommended but it did not work too.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-26-2014 01:55 PM
Hi Darlene,
Thank you, I think I see the direction you are trying to go here. The problem you are encountering is that newValue on a reference field (like the CI) contains the sys_id of the record and not the human readable display value. To get around this, we need to use getReference:
//Set Change Category depending on CI
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
return;
}
g_form.getReference('cmdb_ci', setCategory);
function setCategory(ci) {
// ci contains the GlideRecord on the cmdb_ci table
var oracle = (ci.name + '').toLowerCase().indexOf("tg france oracle") > -1;
if (oracle == true){
category == "ERP Software";
}
else{
category == '';
return;
}
}
}
How it Works:
The getReference call on line 7 gets the CI record from the server and passes the setCategory function as its callback. When the server returns the CI GlideRecord, the callback setCategory is run and the GlideRecord is passed into the ci variable. At that point everything else runs as you would expect it to do so.
If your original script contained only a single equals sign as in the screenshot, then it actually worked accidentally. cmdb_ci = 'TG France Oracle' will always return true because its using the assignment operator and not the logical equivalence operator.
I hope this has helped. Please let me know if you need additional information or assistance.
Kind regards,
Travis
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-26-2014 02:04 PM
Thanks for looking into my dilemma. I tried your script but it still did not work as expected. It's five o' clock here. I suppose I can kill some dragons for a few hours and revisit this.