Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Caller Field Auto-Population Issue

Not applicable

Hi, 

Here is a form that users need to fill out. Once the caller field is populated, the machine name and IP address fields should auto-populate, but its not happening. I'm not sure why. Any assistance would be appreciated.

 

Carol6_0-1707220046228.png

 

script include: 

    getIPandMachineName: function() {
        var note = "";
        //var machinename = '';
        var req = this.getParameter("sysparm_user");
        var ast = new GlideRecord("cmdb_ci_hardware");
        ast.addQuery("sys_class_name""6f2bacd8dbd19450655c5bd05b9619f9");
        ast.addQuery("assigned_to", req);
        ast.addQuery("install_status""1");
        //ast.addQuery("sys_id", srn);
        ast.query();
        if (!ast.hasNext()) {
            gs.addInfoMessage("No Asset assigned to user");
        } else {
            while (ast.next()) {
                var ipandmachinename = {
                    "machine_name": ast.name.getValue(),
                    "ip_address": ast.ip_address.getValue()
                };
            }
            return JSON.stringify(ipandmachinename);
        }
    },
 
client script: 
Type:OnChange
Variable name: caller
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var caller = g_form.getValue('caller_id');

  var ga = new GlideAjax("machineDetails");
    ga.addParam("sysparm_name""getIPandMachineName");
    ga.addParam("sysparm_user",newValue);
    ga.getXML(setalert);

function setalert(response){
    var answer = response.responseXML.documentElement.getAttribute("answer");
    alert(answer);
    g_form.setValue('machine_name', answer);
    g_form.setValue('ip_address', answer);
    }
}
 
1 ACCEPTED SOLUTION

Jyoti Jadhav9
Tera Guru

Hi @Community Alums ,

 

Please refer to the below script and make the change in query condition as per your requirement:

ScriptInclude:

var machineDetails = Class.create();
machineDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getIPandMachineName: function() {
        var note = "";
        //var machinename = '';
        var req = this.getParameter("sysparm_user");
        var ast = new GlideRecord("cmdb_ci_hardware");
       //ast.addQuery("sys_class_name", "6f2bacd8dbd19450655c5bd05b9619f9");
        //ast.addQuery("assigned_to", req);
        //ast.addQuery("install_status", "1");
        //ast.addQuery("sys_id", srn);
        ast.addEncodedQuery("sys_class_name=cmdb_ci_computer^assigned_to="+req+"^install_status=1");
        ast.query();
        if (!ast.hasNext()) {
            gs.addInfoMessage("No Asset assigned to user");
        } else {
            while (ast.next()) {
                var ipandmachinename = {};
                ipandmachinename.machine_name = ast.name.getValue();
                ipandmachinename.ip_address = ast.ip_address.getValue();                
            }
            return JSON.stringify(ipandmachinename);
        }
    },

    type: 'machineDetails'
});

=================================================================================

Client Script:

Type:OnChange
Variable name: caller
function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

   //Type appropriate comment here, and begin script below
   var caller = g_form.getValue('caller_id');

  var ga = new GlideAjax("machineDetails");
    ga.addParam("sysparm_name", "getIPandMachineName");
    ga.addParam("sysparm_user",newValue);
    ga.getXML(setalert);

function setalert(response){
    var answer = response.responseXML.documentElement.getAttribute("answer");
    var answer1 = JSON.parse(answer);
    g_form.setValue('machine_name', answer1.machine_name);
    g_form.setValue('ip_address', answer1.ip_address);
    }
}
 
O/P:
Jyoti4_0-1707229160453.png     Jyoti4_2-1707229245857.png

 

 

Please hit the like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.

 

Thanks & Regards

Jyoti Jadhav

 

View solution in original post

14 REPLIES 14

Hi @Community Alums whatz the alert you get?

Regards
Harish

Not applicable

I get "Null" 

Not applicable

@Harish KM actually this is what it says 

 

Carol6_2-1707227745361.png

 

Carol6_1-1707227719505.png

 

Hello @Community Alums ,

 

As informed by @Harish KM  have you to update the code like below

 

g_form.setValue('machine_name', answer.machine_name);
g_form.setValue('ip_address', answer.ip_address);

 

Please click on like icon if it helps

Not applicable

Yes updated as shown by Harish KM