The CreatorCon Call for Content is officially open! Get started here.

Displaying "Last logged into" or "Primary" customer/caller machine name on incident form.

jared_wentzel
Kilo Contributor

I am looking to create a field on the incident form that will auto populate,
after the customer is pulled up, with that customers primary CI or preferably
their most recently logged into CI (Machine Name) to make it easier for the
helpdesk to provide remote assistance. Basically I am asking, what is the most
accurate way of displaying the machine name that the person calling is on, at
the at the time of the call, on the incident form and how would I go about
implementing that. Has anyone attempted this? Any thoughts or suggestions?

On the "Last logged into CI" side of things...

We are looking to maybe implement the SCCM pluggin (Microsoft
SMS/SCCM Integration 2.0 - ServiceNow Wiki
) and have SCCM query the last
logged into machine name for the given customer. Even if we get that setup I
still don't know how I would have a field populate with that information with
the triggering event being the customer selection. But that is at a standstill
for the moment.

In the mean time I am trying to create a field that will auto populate when
the customer is selected with that persons assigned machine. So it would search
for all CI's assigned to that user where the class is equal to "computer". And I guess if
that customer is assigned multiple computers then the ServiceNow user would
just have to ask the customer which one they are calling about and then select
it.

It doesn't seem hard conceptually based on other systems I've worked with
but I'm not sure how to accomplish this with ServiceNow.

1 ACCEPTED SOLUTION

Use whatever makes sense in your instance.



For us our 'Incident For' field is 'caller_id' (Screenshot: caller_id).



I may have been tired yesterday because I notice that I didn't use g_form.setValue() to actually set the value on the form!


Try this instead:



function onChange(control, oldValue, newValue) {


  var caller = g_form.getValue('caller_id');//get the value of the caller/Incident For person


  g_form.setValue('u_default_ci', getMachine(caller));//set the variable: 'u_default_ci' on the form to


                                                                                                          //the return value of the getMachine function.


 


  function getMachine(c) {


      var gr = new GlideRecord('cmdb_ci_computer');//going out to the computer table


              gr.addQuery('assigned_to', c)//where cmdb_ci.compter.assigned_to is our caller from above.


              gr.query();


              if(gr.next()) {//if the record is found


                  return gr.name;//return the computer name of the caller


              }              


  }




I hope this one makes more sense!



When I return 'gr.name', I am actually returning the asset name from cmdb_ci_computer:


find_real_file.png


View solution in original post

10 REPLIES 10

You can wrap the whole thing in a NOT 'isLoading' check.   Try:


        function onChange(control, oldValue, newValue, isLoading) {  
          if (!isLoading) {
          var caller = g_form.getValue('caller_id');//get the value of the caller/Incident For person  
          g_form.setValue('u_default_ci', getMachine(caller));//set the variable: 'u_default_ci' on the form to  
                                                                                                                  //the return value of the getMachine function.  

          function getMachine(c) {  
              var gr = new GlideRecord('cmdb_ci_computer');//going out to the computer table  
                      gr.addQuery('assigned_to', c)//where cmdb_ci.compter.assigned_to is our caller from above.  
                      gr.query();  
                      if(gr.next()) {//if the record is found  
                          return gr.name;//return the computer name of the caller  
                      }
          }                      
      }









jonathanurie1
Mega Contributor

I think that populating a field on the Incident table is a probably the simplest way to go about doing what you're trying to do.   Personally, I think you should use something like a field message on the caller_id field or something like that so you don't have unnecessary fields cluttering your incident table (field messages are awesome. Here's the wiki article on how to use them: GlideForm (g form) - ServiceNow Wiki).



I would make a couple of changes to the suggested client script:





function onChange(control, oldValue, newValue) {  


 


        //var caller = g_form.getValue('caller_id');   //Since this script is attached the caller_id field, you don't need to ask for the value again


        //var u_newest_incident_field = getMachine(caller);   //This isn't actually setting anything.   You need to call g_form.setValue()


       


        g_form.setValue('u_newest_incident_field', getMachine(newValue));


     


        function getMachine(c) {  


                  var gr = new GlideRecord('cmdb_ci_computer');//going out to the cmdb computer table  


                  gr.addQuery('assigned_to', c);//where the assigned_to field on cmdb computer is the caller_id on incident  


                  //What are you ordering by?


                  //You mentioned perhaps by the last login or something like that.  


                  //What field will get you that information?


                  //gr.orderBy('somefield_that_will_order_correctly');


                  gr.query();  


                  if(gr.next()) {//if record found  


                            //returning 1st result back to field:


                            //return gr.assigned_to; // We know who this is assigned to (it's the caller if your query worked properly)


                            return gr.getDisplayValue(); //This should be the display value of the CI in question.


              }  


  }  


 


}




If you wanted to use a field message, I would change the client script so it would look like this:



function onChange(control, oldValue, newValue) {  


        g_form.hideFieldMsg('caller_id', true);//Hide any field messages that may be out there already


        var gr = new GlideRecord('cmdb_ci_computer');//going out to the cmdb computer table  


        gr.addQuery('assigned_to', newValue);


        gr.query();  


        if(gr.next()) {


       


                  g_form.showFieldMsg('caller_id','CI:'+gr.getDisplayValue(),'info');


        }  


}      



u_newest_incident_field




With both of these suggestions, though, the gliderecord query will introduce some latency to your form.   There will be a tiny delay from the time that you enter a new caller_id to when the field gets filled in or to when the field message is shown.   A single script doesn't make a slow site, but a bunch of scripts will make it very slow.   Consider looking into an AJAX script include to look that info up for you. If you do that then the form will not lock up while the new information is being returned from the server.   A project for Memorial day, maybe?



As a disclaimer, I haven't tested this code, but it should get you going in the right direction.   Let me know if it works!




K


jared_wentzel
Kilo Contributor

Thank you both! I will take a look at it now and let you know how it goes


jared_wentzel
Kilo Contributor

Alright all finished... Finally! Thank you for everyones help! I finally had to recruit the help of my supervisor but this is the finished product if anyone else wants to use this as a template. It will even clear out a previous value if the customer is not only changed but if the field is blank as well.



function onChange(control, oldValue, newValue) {



if (g_form.getValue('caller_id') != "")


{


  var caller = g_form.getValue('caller_id');  


  g_form.setValue('u_default_ci', getMachine(caller));  


      }


else {


  g_form.setValue('u_default_ci', "");


}


}


function getMachine(c) {  


var gr = new GlideRecord('cmdb_ci_computer');


      gr.addQuery('assigned_to', c);//where the assigned_to field on cmdb computer is the caller_id on incident  


      gr.query();  


      if(gr.next()) {//if record found                        


    //returning 1st result back to field:  


      return gr.name;  


      }  


}


jared_wentzel
Kilo Contributor

Looks like we are moving forward with populating the "Last logged into CI" information into ServiceNow. I am working with a .csv file containing the UserID and last logged into machine name for that person. I was able to create a new table and import all the information into it and now I am wanting to try to automate this process to run every 15 minutes and update the "last logged into machine name" where the UserIDs match and am needing some assistance.



after I have this process scheduled, I can then modify the script above to pull in the customer selected last logged into machine name on the incident form.