snehasaraf
Mega Contributor

For anyone who's trying to auto-populate a reference variable on their form, I have a solution.

I was searching for a way to auto-populate my user variable "Computer Name" (which is a reference variable) for my catalog item, but found it difficult at first to find what I was looking for. One discussion I found on here helped me get started: Auto populate computer name catalog item variable based on caller.

But I needed more than that to get exactly the result I wanted, so I tinkered around with the scripts until I came to this solution. When creating the client script to auto-populate the field, it helps to have a script include similar to this (replacing the function names and some of the parameters here with your own, of course):

var GetComputer = Class.create();  

GetComputer.prototype = Object.extendsObject(AbstractAjaxProcessor, {  

  getComputer: function() {  

    var req = this.getParameter('sysparm_user_id');

    var gr = new GlideRecord('cmdb_ci_computer');  

    gr.addQuery('assigned_to', req);  

    gr.query();

   

    if(gr.next())  

    {  

    return gr.sys_id; // returning the sys_id ensures that all fields in the reference are retrieved

    }

  },  

 

  _privateFunction: function() { // this function is not client callable  

 

  }  

 

});

And here is my onChange client script:

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {

          return;

    }

    // Client script for auto-populating computer name on change

  var req = g_form.getValue('requested_for');

  var ga = new GlideAjax('GetComputer');  

  ga.addParam('sysparm_name','getComputer');  

  ga.addParam('sysparm_user_id',req);  

  ga.getXML(GetComputerParse);  

 

  function GetComputerParse(response) {  

      var answer = response.responseXML.documentElement.getAttribute("answer");  

  g_form.setValue('computer_name', answer);

  }  

}

Hope this helps for future reference!

Comments
Service Manager
Kilo Guru

Hi,

I tried your steps but unfortunately my another variable didn't get auto-populated.

Could you tell me what is the type of another variable(which gets auto-populated).

Thanks

 

Version history
Last update:
‎06-07-2017 10:29 AM
Updated by: