Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

The array are not Returning values

Not applicable

Hello Team,

 

I was practicing the Array concept in ServiceNow.

I have written a script include where it will return the groups , a particular user belongs to.

Script:

   usergroup: function() {
        var arr = [];
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('sys_id', this.getParameter('sysparm1'));
        gr.query();
        while (gr.next()) {
            arr.push(gr.group.toString());
        }
        return arr;
    },

 

 

And in client script , whenever the caller changes the groups will be printed to the Description.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

    var ga = new GlideAjax('global.useremail');
    ga.addParam('sysparm_name','usergroup');
    ga.addParam('sysparm1',newValue);
    ga.getXMLAnswer(pop);
    function pop(response){
       
        g_form.setValue('description',response);
    }
   
}

But the Result is showing
sayanghosh0_0-1767512903521.png

 

Can you please help me what is wrong in this.
 
Best Regards

 

9 REPLIES 9

Ankur Bawiskar
Tera Patron

@Community Alums 

make these changes

Script Include

 usergroup: function() {
        var arr = [];
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('user', this.getParameter('sysparm1')); // use user column
        gr.query();
        while (gr.next()) {
            arr.push(gr.group.getDisplayValue()); // get group name
        }
        return arr.toString(); // return string
    },

💡 If my response helped, please mark it as correct ✅ and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Aditya_hublikar
Giga Sage

Hello @Community Alums ,

When you return an array from a Script Include, it must be returned as a string, because the callback function in a Client Script can only receive string values.

That’s why you need to convert the array into a string format before returning it.

You can do this by using:

return JSON.stringify(arrayName);

 

Parse it in client script callback function  :

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

 

    var ga = new GlideAjax('global.useremail');
    ga.addParam('sysparm_name','usergroup');
    ga.addParam('sysparm1',newValue);
    ga.getXMLAnswer(pop);
    function pop(response){
       

var response=response.responseXML.documentElement.getAttribute('answer')

var result = JSON.parse(res);

        g_form.setValue('description',result);
    }
   
}

 

 

you can refer following attachment.

 

adityahubli_0-1767587082049.png

 

 

If this helps you then mark it as helpful and accept as solution.

Regards,

Aditya,

Technical consultant

 

 

 

 

 

Hello @Community Alums ,

If my response helps you then mark it as helpful and accept as solution.

 

regards,

Aditya,

Technical Consultant

Rushi Savarkar
Mega Sage

Hello @Community Alums 

Please make the following changes in your script include and onChange client script:

Script Include: Client Callable true

usergroup: function() {
        var arr = [];
		var userID = this.getParameter("sysparm_callerID");
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('user', userID); // this will be your callerID
        gr.query();
        while (gr.next()) {
            arr.push(gr.group.getDisplayValue()); // get group name
        }
        return arr.join(", "); // return string
    },

OnChange Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (newValue == '') {
      return;
   }

  var ga = new GlideAjax("incidentCount");
  ga.addParam("sysparm_name", "usergroup");
  ga.addParam("sysparm_callerID", newValue);
  ga.getXML(callback);

  function callback(response){
	var answer = response.responseXML.documentElement.getAttribute("answer");
	alert(answer);
	g_form.setValue("description", answer);
  }
}

 Thank you!

If my response helped you, please accept the solution and mark it as helpful.
Thank You!

@Rushi Savarkar 

Any reason to share the same script again which was already shared by other members?

this leads to confusion to the person who asked question

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader