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

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


tried it in addInfoMessage to check whether its working, gonna apply this for some other scenario



thanks Sergiu.