How to display multiple items in an array from a list field Portal

Thomas Miles
Tera Contributor

I have a contact page in the portal and I am appending each  departments contact with multiple values into an array . If there is one contact it works fine. But if there are multiples then I'm getting an error message

 

Server Side

 

data.departments = {

 'HR':  [

 

]

 

Talent = {

    'name': gr.u_talent.first_name,

    'phone': gr.u_talent.phone.toString(),

    'email': gr.u_talent.email.toString()

}

data.departments.HR.push(Talent);

 

If there is one person listed under talent and this was not a list field it works fine. But now that that u_talent is a list field it is throwing me an error . how can i parse the different users in this list to display them in the portal via html

 

 

HTML

 

<div ng-repeat="(label,departments) in data.departments">
<div class="parent">
<div ng-repeat="contact in departments">
<div>{{contact.name}}</div>

<div>{{contact.email}}</div>

<div>{{contact.phone}}</div>

 

 

1 ACCEPTED SOLUTION

Claude DAmico
Kilo Sage

Get the value of the list field to get a list of sys_ids. I don't know off the top of my head if that ends up being a string or array so you may need to use .split() to turn it into an array. Once you have that list array, you can loop through it to generate the data.departments.HR object array. It would look something like this:

 

//Server Side
data.departments={
     'HR': []
};

var gr = new GlideRecord('table');
gr.addQuery('table','value');
gr.query();

while(gr.next()){
     var userList = gr.getValue('u_talent').split(',');
     for(var i = 0; i < userList.length(); i++){
          var gr2 = new GlideRecord('sys_user');
          gr2.get(userList[i]);
          Talent = {
               'name': gr2.first_name,
               'phone': gr2.phone.toString(),
               'email': gr2.email.toString()
          }
          data.departments.HR.push(Talent);
     }
}

 

Claude E. D'Amico, III - CSA

View solution in original post

4 REPLIES 4

Claude DAmico
Kilo Sage

Get the value of the list field to get a list of sys_ids. I don't know off the top of my head if that ends up being a string or array so you may need to use .split() to turn it into an array. Once you have that list array, you can loop through it to generate the data.departments.HR object array. It would look something like this:

 

//Server Side
data.departments={
     'HR': []
};

var gr = new GlideRecord('table');
gr.addQuery('table','value');
gr.query();

while(gr.next()){
     var userList = gr.getValue('u_talent').split(',');
     for(var i = 0; i < userList.length(); i++){
          var gr2 = new GlideRecord('sys_user');
          gr2.get(userList[i]);
          Talent = {
               'name': gr2.first_name,
               'phone': gr2.phone.toString(),
               'email': gr2.email.toString()
          }
          data.departments.HR.push(Talent);
     }
}

 

Claude E. D'Amico, III - CSA

Thank You

do you know how i can turn this into a function to accept a value?

The answer would be dependent on where the value is coming from. There are some great examples here:

https://docs.servicenow.com/bundle/tokyo-servicenow-platform/page/build/service-portal/concept/adv-w... 

Claude E. D'Amico, III - CSA