How to Auto-Populate a Field.

Austine
Tera Contributor

Please I need help on the following requirements.

 

1. Whenever we make changes to the Requester field, we want to auto-populate the description field with more of the Requester's information i.e. Requester email, Requester number, Requester department)

 

2. WE want to make sure that the ‘Date’ field on the ‘MASCOTH’ table does not accept past dates (today’s date is allowed, but past dates are not allowed). That's, only allow record update if the ‘Date’ is a future date.

2 ACCEPTED SOLUTIONS

Bert_c1
Kilo Patron

Create a Client Script of type: 'onChange', table; (your table), field: 'requested_for'. And a script include to get the desired fields from the sys_user table. An example follows:

 

Client script function:

 

 

function onLoad() {
	// Call script include to get the user's cost center
//	var user_id = g_user.userID;  // this is the user accessing the form
	var user_id = g_form.getValue('u_assigned_to');
//	alert("user_id = " + user_id);
	var ga = new GlideAjax('userDetailsUtil');//name of script include
	ga.addParam('sysparm_name', 'getEmployeeDetails');//name of function on script include
	ga.addParam('sysparm_user_id', user_id);//name of field on form triggering call
	ga.getXML(EmployeeDetailsLookup); // Always try to use asynchronous call
}
 
// Callback function to process the response returned from the server
function EmployeeDetailsLookup(response) {
	var answer = response.responseXML.documentElement.getAttribute("answer");
	var result = JSON.parse(answer);
//	g_form.setValue('requested_for',result.name);
//	g_form.setValue('cost_center',result.cc);
	alert("User: " + result.name + " Cost Center = " + result.cc + '\nDepartment: ' + result.dept + ', email: ' + result.email);
}

 

 

Script include (with Client callable checked:

 

 

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

	getEmployeeDetails: function() {
        var userID = this.getParameter("sysparm_user_id");
 
		var result = { //Create an object to store the User data
		name: "Unknown",
		cc: "Unknown",
		dept: "Unknown",
		email: "Unknown",
		// number: "Unknown",
		};

		var user = new GlideRecord('sys_user');
		user.addQuery('sys_id', userID);
		user.query();
		gs.info("userDetailsUtil: Found " + user.getRowCount() + " user records for " + userID + ".");
		if (user.next()) {
			gs.info("userDetailsUtil: user cost_center: " + user.cost_center);
			result.name = user.name.toString();
			result.cc = user.cost_center.toString();
			result.dept = user.department.toString();
			result.email = user.email.toString();
//			result.number = user.number;	// number is not present on sys_user
		}
//		gs.info("userDetailsUtil: Returning: " + result);
		return JSON.stringify(result);
    },

    type: 'userDetailsUtil'
});

 

 

You can build the result string there containing your desired fields, and then parse that in the client script to set form fields.

 

You can use a similar approach for your #2, the script include can compare a DateTime value to the current DateTime and return true/false.

View solution in original post

Moin Kazi
Kilo Sage
Kilo Sage

HI @Austine ,

 

1. Whenever we make changes to the Requester field, we want to auto-populate the description field with more of the Requester's information i.e. Requester email, Requester number, Requester department)

 

Please follow below step -

 

Step 1 Write OnChange Client Script -

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var ga = new GlideAjax('RequestorDetails');
    ga.addParam('sysparm_name', 'getUserDetails');
    ga.addParam('sysparm_id', newValue);
    ga.getXMLAnswer(callback);

    function callback(response) {
        alert(response);
        var answer = JSON.parse(response);
        var answer_details = "Requestor Name: " + answer.name + "\n" + "Requestor Email: " + answer.email + "\n" + "Requestor Department: " + answer.department + "\n" + "Requestor Number: " + answer.phone;
        g_form.setValue('description', answer_details);
    }
}

 Here, I am updating info in Description field based on Caller(requestor) change.

 

Step 2 Write Client Callable Script Include -

var RequestorDetails = Class.create();
RequestorDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getUserDetails: function() {
        var obj = {};
        var sys_id = this.getParameter('sysparm_id');
        var userGr = new GlideRecord('sys_user');
        userGr.get(sys_id);
        obj.name = userGr.getValue('name');
        obj.email = userGr.getValue('email');
        obj.department = userGr.department.getDisplayValue();
        obj.phone = userGr.getValue('phone') ? userGr.getValue('phone') : '';

        return JSON.stringify(obj);
    },
    type: 'RequestorDetails'
});

 

Output -

MoinKazi_0-1728581799331.png

 

2. WE want to make sure that the ‘Date’ field on the ‘MASCOTH’ table does not accept past dates (today’s date is allowed, but past dates are not allowed). That's, only allow record update if the ‘Date’ is a future date.

 

Write UI Policy - See Below

MoinKazi_1-1728582540541.png

MoinKazi_2-1728582572998.png

 

Output -

MoinKazi_3-1728582609039.png

MoinKazi_4-1728582638746.png

 

 

Mark MoinKazi_5-1728582678671.pngCorrect if this solves your issue and also mark MoinKazi_6-1728582678670.pngHelpful if you find my response worthy based on the impact.

 

Regards

Moin

 

View solution in original post

4 REPLIES 4

Bert_c1
Kilo Patron

Create a Client Script of type: 'onChange', table; (your table), field: 'requested_for'. And a script include to get the desired fields from the sys_user table. An example follows:

 

Client script function:

 

 

function onLoad() {
	// Call script include to get the user's cost center
//	var user_id = g_user.userID;  // this is the user accessing the form
	var user_id = g_form.getValue('u_assigned_to');
//	alert("user_id = " + user_id);
	var ga = new GlideAjax('userDetailsUtil');//name of script include
	ga.addParam('sysparm_name', 'getEmployeeDetails');//name of function on script include
	ga.addParam('sysparm_user_id', user_id);//name of field on form triggering call
	ga.getXML(EmployeeDetailsLookup); // Always try to use asynchronous call
}
 
// Callback function to process the response returned from the server
function EmployeeDetailsLookup(response) {
	var answer = response.responseXML.documentElement.getAttribute("answer");
	var result = JSON.parse(answer);
//	g_form.setValue('requested_for',result.name);
//	g_form.setValue('cost_center',result.cc);
	alert("User: " + result.name + " Cost Center = " + result.cc + '\nDepartment: ' + result.dept + ', email: ' + result.email);
}

 

 

Script include (with Client callable checked:

 

 

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

	getEmployeeDetails: function() {
        var userID = this.getParameter("sysparm_user_id");
 
		var result = { //Create an object to store the User data
		name: "Unknown",
		cc: "Unknown",
		dept: "Unknown",
		email: "Unknown",
		// number: "Unknown",
		};

		var user = new GlideRecord('sys_user');
		user.addQuery('sys_id', userID);
		user.query();
		gs.info("userDetailsUtil: Found " + user.getRowCount() + " user records for " + userID + ".");
		if (user.next()) {
			gs.info("userDetailsUtil: user cost_center: " + user.cost_center);
			result.name = user.name.toString();
			result.cc = user.cost_center.toString();
			result.dept = user.department.toString();
			result.email = user.email.toString();
//			result.number = user.number;	// number is not present on sys_user
		}
//		gs.info("userDetailsUtil: Returning: " + result);
		return JSON.stringify(result);
    },

    type: 'userDetailsUtil'
});

 

 

You can build the result string there containing your desired fields, and then parse that in the client script to set form fields.

 

You can use a similar approach for your #2, the script include can compare a DateTime value to the current DateTime and return true/false.

Austine
Tera Contributor

@Bert_c1 Thank you for your response. But please where will I use requester_email, requester_number, and requester_department in the script? because those are the information we want to populate in the description field whenever changes are made to the requester's field.

Moin Kazi
Kilo Sage
Kilo Sage

HI @Austine ,

 

1. Whenever we make changes to the Requester field, we want to auto-populate the description field with more of the Requester's information i.e. Requester email, Requester number, Requester department)

 

Please follow below step -

 

Step 1 Write OnChange Client Script -

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var ga = new GlideAjax('RequestorDetails');
    ga.addParam('sysparm_name', 'getUserDetails');
    ga.addParam('sysparm_id', newValue);
    ga.getXMLAnswer(callback);

    function callback(response) {
        alert(response);
        var answer = JSON.parse(response);
        var answer_details = "Requestor Name: " + answer.name + "\n" + "Requestor Email: " + answer.email + "\n" + "Requestor Department: " + answer.department + "\n" + "Requestor Number: " + answer.phone;
        g_form.setValue('description', answer_details);
    }
}

 Here, I am updating info in Description field based on Caller(requestor) change.

 

Step 2 Write Client Callable Script Include -

var RequestorDetails = Class.create();
RequestorDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getUserDetails: function() {
        var obj = {};
        var sys_id = this.getParameter('sysparm_id');
        var userGr = new GlideRecord('sys_user');
        userGr.get(sys_id);
        obj.name = userGr.getValue('name');
        obj.email = userGr.getValue('email');
        obj.department = userGr.department.getDisplayValue();
        obj.phone = userGr.getValue('phone') ? userGr.getValue('phone') : '';

        return JSON.stringify(obj);
    },
    type: 'RequestorDetails'
});

 

Output -

MoinKazi_0-1728581799331.png

 

2. WE want to make sure that the ‘Date’ field on the ‘MASCOTH’ table does not accept past dates (today’s date is allowed, but past dates are not allowed). That's, only allow record update if the ‘Date’ is a future date.

 

Write UI Policy - See Below

MoinKazi_1-1728582540541.png

MoinKazi_2-1728582572998.png

 

Output -

MoinKazi_3-1728582609039.png

MoinKazi_4-1728582638746.png

 

 

Mark MoinKazi_5-1728582678671.pngCorrect if this solves your issue and also mark MoinKazi_6-1728582678670.pngHelpful if you find my response worthy based on the impact.

 

Regards

Moin

 

@Moin Kazi Thank you so much. It works!