Catalog client script (onLoad) GlideRecord lookup acts weird (Istanbul/ServicePortal)

Vespertinus
Tera Expert

Hello SN-Community,

i need your help because this drives me crazy. I "simply" wanted to add a "Catalog client script" for a catalog item populated in service portal to preload some data into a list collector field.

First I figured out following strange behaviour.

"normal" gr.query() does not work in Catalog client scripts and i have to use following code:

gr.query(function(gr) {

while(gr.next())

{

        ..

});

why? This seems to run the whole script in incorrect "order" and i cannot add another lookup in while loop.

Here is my usecase:

Get users skills from (sys_user_has_skill) and take sys_id in "skill" field, so i have the skills related to logged in user

Get skill display value from this skill

Populate these into list collector variable -> pseudocode: myListCollector.addItem(sys_user_has_skill.skill,sys_user_has_skill.skill.name);

-> got this running so far for sys_id only but could not get display value of the referenced skill field

-> I tried to do a gliderecord lookup for display value but this ended up in code running in "incorrect" order (i think this is caused by function inside while loop)

I am little frustraded trying this "simple" thing for couple of hours and did not get it working. Why theese catalog client scripts are so uncomfortable to use compared to normal client scripts? Also methods that run pretty fine for client scripts simply "crash" the client script without any message in frontend. In log i can see a strange:" java.lang.IllegalArgumentException: Group undefined does not exist.: " error and no reference to the line that caused the error. I can post my actual code, but do not want to influence another solution. Seems that i am stuck in a dead end..

Big thx in advance

Vesp

1 ACCEPTED SOLUTION

https://developer.servicenow.com/app.do#!/api_doc?v=istanbul&id=c_GlideAjaxAPI



It's not that advanced.   You pass a parameter back to the server, have the server process some logic based on that, then return the results to your client.


View solution in original post

4 REPLIES 4

Mike Allen
Mega Sage

Have you tried GlideAjax.   Generally, it is bad practice to put GlideRecord in Client-side code, and GlideAjax should be used instead.


I have not tried GlideAjax so far.. is there any good introduction for this? Is it actually really mandatory for having my usecase realized? I am little frustrated/disappointed why such a "simple" requirement causes the need for a lot of code lines with even having need for script includes libraries created and combined with GlideAjax. I attended the ServiceNow scripting course and this was the last module in it commented by the instructor as the most advanced and complex module... 😕


https://developer.servicenow.com/app.do#!/api_doc?v=istanbul&id=c_GlideAjaxAPI



It's not that advanced.   You pass a parameter back to the server, have the server process some logic based on that, then return the results to your client.


Vespertinus
Tera Expert

Thanks Mike for fast replies, i figured out that best way to realize my requirement was to directly add all script into default value field of list collector:



javascript:


var userhasskills = new GlideRecord('sys_user_has_skill');


userhasskills.addQuery('user',gs.getUserID());


userhasskills.query();


var answer = "";


while(userhasskills.next()){


  answer = answer +userhasskills.skill.toString()+",";


}


answer = answer .slice(0, -1);



Really easy and "short" script and prefills the collector with correct value. My conclusion is to avoid complex lookups in catalog client scripts. Better way would be to use script includes or direct JS in default value field.



Thx and br


Vesp