- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2025 10:33 AM
I created custom app off the Task table. For short description, I want to set the field to the value of 'Service', 'Category', and 'Subcategory'. I have created client script to capture that, but it is getting the sys_id of the reference field. Here is my script:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2025 11:33 AM
Alternative to getting display value instead of sys_id for a reference field in core UI could be to use getDisplayBox() function like so.
var category = g_form.getDispayBox('category').value
But as @Chaitanya ILCR mentioned, the better approach is to go with Before Business Rule if you want the change in Short Description to reflect on all 3 field's change of value.
Regards,
Sangeetha
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2025 07:47 PM
Hi @athavichith
You can make use of g_form.getDisplayBox('your_field_name').value method to get reference field value. Refer below example where I tried getting the value of the Caller field and pushing it to the Description Field.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var caller = g_form.getDisplayBox('caller_id').value;
g_form.setValue('description', caller);
}
Thanks and Regards
Amit Verma
Please mark this response as correct and helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2025 10:46 AM
Hi @athavichith
If the field is a reference field, then using g_form.getValue(fieldName) returns the sys_id of the corresponding reference record. If you want to get the actual value displayed, then use g_form.getDisplayValue(fieldName).
So, if your problem is to get the actual displayed value of that particular field instead of getting their sys_id, then replace occurences of g_form.getValue(..) with g_form.getDisplayValue(..).
If you find my post resolving your issue or if you find my post in any way useful, please mark it as helpful.
Thanks,
Sangeetha
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2025 11:02 AM
Hello @athavichith
I don't think there is any g_form.getDisplayValue() method that would work on normal records, it only works on "service portal". It doesn't works for normal records.
You may need to use GetReference method of g_form.
Additionally, you need to use g_form.getOption(fieldName, value).text method to get the display value of choice fields. Otherwise it will give you backend values. (If both the backend and frontend values are same that's fine)
Here's the modified script -
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var service = g_form.getValue('service'); // Reference field
var category = g_form.getValue('category'); // Choice field
var subcategory = g_form.getValue('subcategory'); // Choice field
// Get display values for choice fields
var categoryDisplay = category ? g_form.getOption('category', category).text : '';
var subcategoryDisplay = subcategory ? g_form.getOption('subcategory', subcategory).text : '';
function setShortDescription(serviceName) {
var shortDescription = '';
if (serviceName) {
shortDescription += 'Service: ' + serviceName;
}
if (categoryDisplay) {
shortDescription += (shortDescription ? ' / ' : '') + 'Category: ' + categoryDisplay;
}
if (subcategoryDisplay) {
shortDescription += (shortDescription ? ' / ' : '') + 'Subcategory: ' + subcategoryDisplay;
}
g_form.setValue('short_description', shortDescription);
}
if (service) {
g_form.getReference('service', function(ref) {
setShortDescription(ref ? ref.name : service);
});
} else {
setShortDescription('');
}
}
Please let me know if you face any issues.
Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket.
Regards,
Shivalika
My LinkedIn - https://www.linkedin.com/in/shivalika-gupta-540346194
My youtube - https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&si=0WynLcOwNeEISQCY
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2025 11:10 AM
I cannot make the category, subcategory into Choice field...cause I have 300+ options.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2025 11:18 AM
Hello @athavichith
I was assuming both of them to be choice field hence I gave that code. This is how we have it in OOB.
Let me give you a refreshed code, since now I have the clarity that all three are reference fields.
Below is the updated code -
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var service = g_form.getValue('service');
var category = g_form.getValue('category');
var subcategory = g_form.getValue('subcategory');
var shortDescription = [];
function fetchDisplayValue(field, callback) {
if (field) {
g_form.getReference(field, function(record) {
if (record && record.name) {
callback(record.name);
} else {
callback('');
}
});
} else {
callback('');
}
}
fetchDisplayValue('service', function(serviceName) {
if (serviceName) shortDescription.push('Service: ' + serviceName);
fetchDisplayValue('category', function(categoryName) {
if (categoryName) shortDescription.push('Category: ' + categoryName);
fetchDisplayValue('subcategory', function(subcategoryName) {
if (subcategoryName) shortDescription.push('Subcategory: ' + subcategoryName);
g_form.setValue('short_description', shortDescription.join(' / '));
});
});
});
}