Autopopulate a field on the incident form from the user table

DEESN
Tera Contributor

I've watched countless YouTube videos, read numerous Community articles and still I cant seem to populate a field from the User table on the incident form based on the caller_id. I created a dictionary entry called T-ID and made it a reference and then created various scripts including Client scripts and script includes and cant seem to get it to work. I even tried to use the user's email in case we accidentally set up the dictionary entry wrong and I cant get that to populate either (this was previously configured for us). 

 

Trying to understand if I missed a step in the process. Any help would be appreciated! Here is a very rudimentary outline of what I did:

 

Step 1. Created a dictionary entry for T-ID (reference to the sys_user table)

Step 2: Added the field to the incident form 

Step 3: Created a client script (tried so many versions at this point that writing them out seems unreasonable) to pull caller_id (field name for us is contact) and return the T-ID (u_t_id)

Step 4: Create a Script Includes 

 

18 REPLIES 18

SanjivMeher
Kilo Patron
Kilo Patron

Do you want to show that field in incident form?

If so, you can just do it via form layout, by expanding the caller_id field and search the field T-ID and then add it to the incident form.

If you want to use it to derive something, you need to create a client script and you can do a getReference call.

 

var gr = g_form.getReference('caller_id');
var caller = g_form.getReference('caller_id', doAlert); // doAlert is our callback function
function doAlert(caller) { //reference is passed into callback as first arguments
alert('Caller Email is: ' + caller.u_t_id);
}

 

Another option is making a GlideAjax call from client script to a Client callable script include.


Please mark this response as correct or helpful if it assisted you with your question.

I have the Field on the incident form already  We would like the T-ID to autopopulate onchange when contact is selected. Right now when I click the search function next to the T-ID field only Name, First name, Last name and email columns populate and this is where I cant tell if it's the code I wrote or if the dictionary entry is configured incorrectly or both. In a perfect world I wouldnt even have to click the search button and the T-ID would just populate. 

DEESN_0-1704805008925.png

This is the latest code I've attempted:

 

Client Script

DEESN_1-1704805266524.png

Script Include

DEESN_2-1704805322822.png

 

 

Our normal developer is out on extended leave so I am managing my best with limited javascript/servicenow knowledge and just trying to learn as much as quickly as possible (without breaking anything!) so any supporting explanations/ supplemental documentation would be beneficial to my learning and understanding!

Hi @DEESN 

It's because the line 7 in your script include => ga.log. Remove this line should do the trick.

Timi_0-1704810323712.png


Plus the point from @Danish Bhairag2 , the script include name is incorrect in your client script. 😁

You can try my adjustment below for your requirement.

#Client Scrip

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading) {
        return;
    }

	if (newValue === ''){
		g_form.clearValue('u_t_id');
		g_form.clearValue('u_user_s_email');
	}

    var ga = new GlideAjax('AutoPopulate_CallerInfo');
    ga.addParam('sysparm_name', 'getCallerInfo');
    ga.addParam('sysparm_caller_id', newValue);
	ga.getXMLAnswer(function(answer){
		var user = JSON.parse(answer);
		g_form.setValue('u_t_id', user.t_id);
		g_form.setValue('u_user_s_email', user.email);
	});
	
}

 

#Script Include

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

    getCallerInfo: function() {
        var callerSysId = this.getParameter('sysparm_caller_id');
		var result = {};
        var grUser = new GlideRecord('sys_user');
		if(grUser.get(callerSysId)){
			result.t_id = grUser.getValue('u_t_id');
			result.email = grUser.getValue('email');
		}
		return JSON.stringify(result);
    },

    type: 'AutoPopulate_CallerInfo'
});

 

Cheers,

Tai Vu

DEESN
Tera Contributor

@Tai Vu Unfortunately, even with the changes and trying the suggested code. I was unable to autopopulate the fields. I dont know if this signifies an issue with the dictionary entries?