The CreatorCon Call for Content is officially open! Get started here.

How to set category based on CI

dwolf
Giga Expert

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

1 ACCEPTED SOLUTION

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", "");      


          }  


  }  


}  


View solution in original post

9 REPLIES 9

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", "");      


          }  


  }  


}  


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.")


nth script.PNG


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.


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!


Thank you so much for doing much of the leg work for me. I have much to learn on Java Scripting.



Sincerely,


Darla


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