I was trying to run the below code in JavaScript Executor. but as an output, it returns "undefined".

Ajay07
Tera Contributor

I was trying to run the below code in JavaScript Executor. but as an output, it returns "undefined". any idea what I need to do??

 

gname = g_form.getValue('assignment_group');
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('group',gname);
gr.query();
while(gr.next())
{
var user = gr.user.name;
alert(user);
}

6 REPLIES 6

AnubhavRitolia
Mega Sage
Mega Sage

Hi @Ajay07 

 

JavaScript Executor tests client side scripting. Refer link below:

https://www.servicenow.com/community/developer-blog/using-the-javascript-executor-to-test-client-scr....

 

And GlideRecord() is a server side API so it won't work on JavaScript Executor.

 

If you want to test it, you can use background Script with specific record number you can test GlideRecord.

 

Refer code below:

 

var inc = GlideRecord('incident');
inc.addQuery('number','INC0003424'); // put your Incident number for which you are trying to run javascript executor
inc.query();
if(inc.next())
{
gname = inc.assignment_group;
var grpMember = new GlideRecord('sys_user_grmember');
grpMember.addQuery('group',gname);
grpMember.query();
while(grpMember.next())
{
var user = grpMember.user.name;
gs.info(user); // gs.print(user) will also work in background script
}
}

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

Hi, @AnubhavRitolia  Thanks!!
I have one more doubt, First, I want to fetch all the users whoever is supporting the respective group and after that, I want to compare the current time with the From time and To time of the group member. if the current time is between From and To then it will set the member name as Assigned to. so I was writing this code in UI Action. it's not working. 

       gname = g_form.getValue('assignment_group');
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('group',gname);
        gr.query();
        while(gr.next())
        {
        var user = gr.user.name;
        var gr2=new GlideRecord('sys_user');
        gr2.addQuery('name',user);
        gr2.query();
            while(gr2.next()) {
            var gdt = new GlideDateTime();
            var gtime = new GlideTime();
            gtime.setValue("05:30:00"); //Converting GMT time to IST
            gdt.add(gtime);
            var from = gr2.getDisplayValue('u_glide_date_time_1'); //User Will Available From Time
            var to = gr2.getDisplayValue('u_glide_date_time_2');   //User Will Available Till Time
            var curr = gdt.toString();
            if( curr >= from && curr <= to)
            {
                g_form.setValue('assigned_to',user);
                g_form.save();
                g_form.addInfoMessage('Assigned Successfully');
            }
                else
                g_form.addErrorMessage('Something is wrong');
            }
        }

Hi @Ajay07 

 

As mentioned, you cannot use both client side and server side script together.

 

You are using g_form (client side) and GlideRecord() and GlideDateTime() server side APIs together.

 

What is your UI Action type? Client or Server.

Also try to add Logs or alerts to see where you are getting stuck.

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

Hi @Ajay07 

 

As I have solved your original query related to JavaScript Executor, can you please mark my solution as Correct Answer

 

For another query you can raise different question and we can have seperate tread to resolve it. Feel free to share the link of new question on private message also or tag me so I can help you with that also.

 

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023