Autopopulate variables based on selection made in another variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-08-2025 12:12 PM - edited ‎01-08-2025 01:05 PM
I have a custom app with a catalog item. For one of my use cases, I am trying to populate the first and last name variables based on the selection of the email address in a variable that references the custom table. In the attached screenshot the variable Contractor Email Lookup is the referenced field and based on this value I need to populate First Name and Last Name.
I have created a Catalog Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '' || newValue === oldValue) {
return;
}
var EMAIL_FIELD = 'new_end_term_date_contractor';
var FIRST_NAME_FIELD = 'first_name';
var LAST_NAME_FIELD = 'last_name';
var email = g_form.getValue(EMAIL_FIELD);
if (!email || !email.includes('@')) {
g_form.showFieldMsg(EMAIL_FIELD, 'Invalid email address.', 'error');
return;
}
g_form.showFieldMsg(EMAIL_FIELD, 'Loading contractor details...', 'info');
var ga = new GlideAjax('GetContractorDetails');
ga.addParam('sysparm_name', 'getDetails');
ga.addParam('sysparm_email', email);
ga.getXMLAnswer(function(response) {
g_form.clearMessages();
try {
var details = JSON.parse(response);
if (details) {
g_form.setValue(FIRST_NAME_FIELD, details.first_name || '');
g_form.setValue(LAST_NAME_FIELD, details.last_name || '');
} else {
g_form.showFieldMsg(EMAIL_FIELD, 'No contractor details found.', 'error');
}
} catch (error) {
g_form.showFieldMsg(EMAIL_FIELD, 'Error fetching contractor details.', 'error');
console.error('Error parsing response:', error);
}
});
}
var GetContractorDetails = Class.create();
GetContractorDetails.prototype = {
initialize: function() {},
getDetails: function() {
var result = {};
// Get the email parameter
var email = this.getParameter('sysparm_email');
if (!email) {
result.error = "Email parameter is missing.";
return JSON.stringify(result);
}
try {
// Query the custom table for the contractor details
var gr = new GlideRecord('x_unchc_unch_con_0_contractor_badge_requests');
gr.addQuery('email_address', email);
gr.query();
if (gr.next()) {
result.first_name = gr.getValue('first_name');
result.last_name = gr.getValue('last_name');
} else {
result.error = "No records found for the given email.";
}
} catch (e) {
gs.error("Error in GetContractorDetails Script Include: " + e.message);
result.error = "An unexpected error occurred while retrieving contractor details.";
}
return JSON.stringify(result); // Return the data as JSON
},
type: 'GetContractorDetails'
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-09-2025 08:21 AM
Thank you for the replies. After reading @jcmings reply I had an aha moment, I realized that because the contractor information is in the same table and not in a separate contractor data table [like the sys_user table], my script approach as it currently stood wasn't going to work. Then I read @Jon23 reply and had another aha moment, I had previously considered the auto-populate, dot-walk approach but because of my uses cases didn't think it would work, but the aha moment was that I could probably get it to work in combination with a UI Policy. I have done some config and need to do some more testing, but so far it seems to be working. I'll provide another update later.
I do really appreciate you both providing some valuable guidance. I am relatively new at this development stuff, hopefully some day in the future [when I have more experience] and can repay the favor and help you out.
Thank you!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-09-2025 08:28 AM
I love those 'aha' moments. Happy we could help, and thanks for replying with an update.