- 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-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-29-2014 08:17 AM
Hi Travis,
Happy Monday! I did a slight change on your script and got it to work... Can you see below if I did it correct, because i don't want a fluke (Like what you pointed out in "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.")
These are the minor changes I did:
Line 11 - Added space after oracle
Line 12 - Added space after discoverer
Line 17 - Changed oracle to erp
Thanks so much for the feedback.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-29-2014 08:20 AM
All looks good! I love how after "fixing" my dumb error I made another one by not changing oracle to erp on line 17
Great catch!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-29-2014 08:23 AM
Thank you so much for doing much of the leg work for me. I have much to learn on Java Scripting.
Sincerely,
Darla
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-29-2014 08:29 AM
Not a problem, if any of the above is still confusing or if you would like to understand something more clearly just let me know. Or feel free to post other questions to the community, there are plenty willing to help