Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

How do I lookup choice list values via a script?

jbrassard
Kilo Contributor


I am creating a Web Import Set and putting a validation script in the Tranform Map to validate the incoming data before processing the SOAP request to create a new Incident. On our Incident form, we have 3 custom choice lists that are dependent on each other: Category 1, Category 2 (dependent on Category 1) and Category 3 (dependent on Category 2).

So for instance, Category 1 has entries of Hardware, Software, Facilities, etc. Under Hardware, Category 2 has Desktop and Laptop, Under Laptop, Category 3 has Windows and Mac.

In my script, I need to be able to take the Category 1 field being passed is valid and that the category 2 field is a valid value based on what is in category 1 and that the category 3 value coming in is a valid value based on the category 1 and 2 values. I haven't got a clue how to script this. Can anyone help?

4 REPLIES 4

Deepak Ingale1
Mega Sage

Hello Janet,



What I understand is that category 2 should be populated based on category 1 and category 3 should be populated based on category 2. If this is the case, then you should create a lookup rule just similar to impact, urgency and priority fields on incident form.



You will find the article on wiki for creating this.



Data Lookup and Record Matching Support - ServiceNow Wiki


When I did this is dev, our priorities no longer work as they did. I had seen the warning about it disabling customizations to the changePriority script - it did not appear we had any, but something has changed somewhere, so I'm not sure this will work. Is there not a way to get the values out of the choice lists via scripts over sys_choice?


lars_b_lund
Giga Contributor

I just made a recursive lookup. for the sample I havde used the properties from the standard choice list.


you should adjust to your values.


Not beautiful but is should work.



this should work


var gr1 = new GlideRecord("sys_choice");


var cat2_depend;


var cat3_depend;


var valid_call=0; // same value as when the 3 Categories exists, including relevant dependencies



gr1.addQuery("table", "your value");


gr1.addQuery("element", "your value");


gr1.addQuery("value", "your value category 1");


gr1.query();


if (gr1.next()) {


  // Catagory 1 exists


  cat2_depend=gr1.value ;



  var gr2 = new GlideRecord("sys_choice");


  gr2.addQuery("table", "your value");


  gr2.addQuery("element", "your value");


  gr2.addQuery("value", "your value category 2");


  // Category 2 dependent is value of category 1


  gr2.addQuery("Dependent_value", cat2_depend);


  gr2.query();


  if (gr2.next()){


  // if there are more than one the first will be selected


  // Catagory 2 exists


  cat3_depend=gr1.value ;


  var gr3 = new GlideRecord("sys_choice");


  gr3.addQuery("table", "your value");


  gr3.addQuery("element", "your value");


  gr3.addQuery("value", "your value category 2");


  // Category 3 dependent is value of category 2


  gr3.addQuery("Dependent_value", cat3_depend);


  gr3.query();


  if (gr3.next()) {


  // Catagory 3 exists


  // insert the code that should be executed here, or call a function


// or use the statement finalizing theis script


  valid_call = 0;


  }else{


  gs.log("inddata cat 3 is not valid");


  valid_call = 3;


  }


  }else


  {


  gs.log("inddata cat 2 is not valid");


  valid_call = 2;


  }


}


else


  {


  gs.log("inddata cat 1 is not valid");


  valid_call = 1;


}




if(valid_call==0){


  //Call your function


}else


{


  // send error as web service response


  gs.log("Category: " & valid_call & " has an invalid value " );


}


ckoss
Kilo Contributor

..long time ago this question..but me as newbie in scripted REST on SN...run into this issue also. In my case the choice-list is an 

operational_status choice..( e.g. "operational_status": "Operational",.. ). In SN those values are stored via its "value"
(e.g. <value>2</value> if it is <label>Non-Operational</label> )
goto system definitions / choice Lists
after we filtered the list to our choices from interest ..we right click on the list and get this XML:
<xml>
<sys_choice>
<dependent_value/>
<element>operational_status</element>
<hint/>
<inactive>false</inactive>
<label>DR Standby</label>
<language>en</language>
<name>cmdb_ci</name>
<sequence>4</sequence>
<sys_created_by>system</sys_created_by>
<sys_created_on>2019-05-17 09:43:31</sys_created_on>
<sys_domain>global</sys_domain>
<sys_domain_path>/</sys_domain_path>
<sys_id>7e435ad4db2133009041f27eaf961954</sys_id>
<sys_mod_count>0</sys_mod_count>
<sys_updated_by>system</sys_updated_by>
<sys_updated_on>2019-05-17 09:43:31</sys_updated_on>
<value>4</value>
</sys_choice>
Now we can query via JS as we like:

..if we need the value:

var gr = new GlideRecord('sys_choice');
var gr = new GlideRecord('sys_choice');
gr.addQuery('name','cmdb_ci');
gr.addQuery('element','operational_status');
gr.addQuery('label','Non-Operational');
gr.query();

if (gr.next()) {
var v =gr.value ;
gs.info("this i found for you: " + v);
}

var gr = new GlideRecord('sys_choice');
gr.addQuery('name','cmdb_ci');
gr.addQuery('element','operational_status');
gr.addQuery('value','2');
gr.query();

if (gr.next()) {
var v =gr.label ;
gs.info("this i found for you: " + v);
}

result:
 this i found for you: 2
this i found for you: Non-Operational
..no clue if this is best practice - but working for me..