- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2024 09:19 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2024 10:03 AM - edited 10-11-2024 08:03 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2024 10:54 AM
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 -
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
Output -
Mark Correct if this solves your issue and also mark
Helpful if you find my response worthy based on the impact.
Regards
Moin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2024 10:03 AM - edited 10-11-2024 08:03 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2024 10:16 AM - edited 10-10-2024 10:17 AM
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2024 10:54 AM
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 -
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
Output -
Mark Correct if this solves your issue and also mark
Helpful if you find my response worthy based on the impact.
Regards
Moin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2024 11:43 AM
@Moin Kazi Thank you so much. It works!