Need to display all incident caller id's incident number, username and email

aladpereira
Mega Expert

Hi All,

Need to display all active incident's - incident number, username and email. But couldn't get the expected output. can anyone tell whats wrong with the code.

function onBefore(current, previous) {

      //This function will be automatically called when this rule is processed.

      //gs.addInfoMessage("first");

      var inc = new GlideRecord('incident');

      inc.addQuery('active',true);

      inc.query();

      while (inc.next()) {

              gs.addInfoMessage(inc.number);

              var grpMbr = new GlideRecord('sys_user');

              grpMbr.addQuery('user_id', inc.caller_id);

              grpMbr.orderByDesc('sys_created_on');

              grpMbr.query();

              while (grpMbr.next()) {

                      var emailid = grpMbr.email;

                      var userid = grpMbr.user_name;

                      gs.addInfoMessage(userid +" = "+emailid);

                     

                     

              }

      }

}

1 ACCEPTED SOLUTION

Hi Anto,



Running this script in Background Scripts produces the result you want:



var inc = new GlideRecord('incident');


inc.addQuery('active',true);


inc.query();


while (inc.next()) {


            gs.print(inc.number);


            gs.print(inc.caller_id.getDisplayValue());


              var grpMbr = new GlideRecord('sys_user');


              grpMbr.addQuery('sys_id', inc.caller_id);


              grpMbr.orderByDesc('sys_created_on');


              grpMbr.query();


              while (grpMbr.next()) {


                      var emailid = grpMbr.email;


                      gs.print(emailid);


          }


}



I don't understand why you would use this in a business rule with gs.addInfoMessage, especially if output is large.


Second, in the sys_user GlideRecord call, don't use user_id in addQuery, but sys_id, as inc.caller_id is actually sys_id of sys_user record.



Does this helps? Otherwise please explain a bit more into details what you want to achieve.



Regards,


Sergiu


View solution in original post

6 REPLIES 6

sergiu_panaite
ServiceNow Employee
ServiceNow Employee

Hi Anto,



How are you applying this script?



Regards,


Sergiu


using Business rule in incident table


Hi Anto,



This works for me as a business rule on display with a condition:



Condition:


current.active == true



Script:



function onDisplay(current, g_scratchpad) {


    //This function will be automatically called when this rule is processed.


      gs.addInfoMessage(current.number);


      gs.addInfoMessage(current.caller_id.getDisplayValue());


      var grpMbr = new GlideRecord('sys_user');


              grpMbr.addQuery('sys_id', current.caller_id);


              grpMbr.query();


              while (grpMbr.next()) {


                      var emailid = grpMbr.email;


                        gs.addInfoMessage(emailid);


              }


}



Screenshot:



Screen Shot 2015-10-21 at 10.52.29 AM.JPG



Regards,


Sergiu


Sergiu thanks for the reply, I need all the active records of incidents , not only for the current.caller_id and for all the caller_id for active incidents.