Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Hi

Madhan007
Tera Contributor

I am new to SNOW. I am trying to auto populate the description field of an incident form with location of the logged in user using getReference method in onload client script and I cannot get it right.

function onLoad() {
//Type appropriate comment here, and begin script below
var user=g_form.getReference('caller_id',callBack);
function callBack(user){
g_form.setValue('description',user.email);
}
}

2 ACCEPTED SOLUTIONS

Sonu Parab
Mega Sage

Hi @Madhan007   Use below Scripts to fulfill your requirement.

Step 1] Create use below script in onLoad Client script.

 

function onLoad() {
    //Type appropriate comment here, and begin script below
    
    var caller = g_form.getValue('caller_id');
    var ga = new GlideAjax('global.AG_based_on_caller');   //here global.AG_based_on_caller Script Include Name
    ga.addParam('sysparm_name', 'getRecord');  // here getRecord  function in Script Include
    ga.addParam('sysparm_user', caller);
    ga.getXML(callback);

    function callback(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
          
            g_form.setValue('description', answer);
       
    }
}

 


Step 2] Create one Client Callable Script Inlcude.

 

var AG_based_on_caller = Class.create();
AG_based_on_caller.prototype = Object.extendsObject(AbstractAjaxProcessor, {


    getRecord: function() {

        var user_sys = this.getParameter('sysparm_user');
        gs.info("user_sys-" + user_sys);
        var user = new GlideRecord("sys_user");
        user.addQuery('sys_id', user_sys);
        user.query();
        if (user.next()) {
            var loc = user.location; //Location field on User table.
            return loc;
        }
    },
    type: 'AG_based_on_caller'
});


If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!
Thank You

 

View solution in original post

Community Alums
Not applicable

Hi @Madhan007 ,

To auto populate the  logged in user details:

Add in default value to that variables : javascript:gs.getUserID()//name of logged in user.

javascript:gs.getUser().getRecord().getValue('phone_number');//phone number

javascript:gs.getUser().getRecord().getValue('location');//location

AS shown in Screen shot:

SandeepDutta_0-1674480917113.png

 

 

 

View solution in original post

3 REPLIES 3

Sonu Parab
Mega Sage

Hi @Madhan007 
Just wanted to confirm where is your location field. On Users[sys_user]  table?


Sonu Parab
Mega Sage

Hi @Madhan007   Use below Scripts to fulfill your requirement.

Step 1] Create use below script in onLoad Client script.

 

function onLoad() {
    //Type appropriate comment here, and begin script below
    
    var caller = g_form.getValue('caller_id');
    var ga = new GlideAjax('global.AG_based_on_caller');   //here global.AG_based_on_caller Script Include Name
    ga.addParam('sysparm_name', 'getRecord');  // here getRecord  function in Script Include
    ga.addParam('sysparm_user', caller);
    ga.getXML(callback);

    function callback(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
          
            g_form.setValue('description', answer);
       
    }
}

 


Step 2] Create one Client Callable Script Inlcude.

 

var AG_based_on_caller = Class.create();
AG_based_on_caller.prototype = Object.extendsObject(AbstractAjaxProcessor, {


    getRecord: function() {

        var user_sys = this.getParameter('sysparm_user');
        gs.info("user_sys-" + user_sys);
        var user = new GlideRecord("sys_user");
        user.addQuery('sys_id', user_sys);
        user.query();
        if (user.next()) {
            var loc = user.location; //Location field on User table.
            return loc;
        }
    },
    type: 'AG_based_on_caller'
});


If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!
Thank You

 

Community Alums
Not applicable

Hi @Madhan007 ,

To auto populate the  logged in user details:

Add in default value to that variables : javascript:gs.getUserID()//name of logged in user.

javascript:gs.getUser().getRecord().getValue('phone_number');//phone number

javascript:gs.getUser().getRecord().getValue('location');//location

AS shown in Screen shot:

SandeepDutta_0-1674480917113.png