How populate a field on the sc_req_item table with a field from a catalog item?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 08:52 AM
I created an "activity" field on the sc_req_item table for reporting purposes. Most, not all, of our catalog items have a field called "Select Action Type" which I also want to be the default value of "activity" upon the user submitting the form. How do I do this?
I initially tried setting the default value of activity to javascript:current.select_action_type but that did not work I guess because value is only there onChange.
So I created this client script on the sc_req_item table. I want this to be a default function where I won't have to create a catalog client script for every catalog item it's on. This is my client script, why isn't it working?
function onSubmit() {
//Type appropriate comment here, and begin script below
var user = new GlideRecord('sc_req_item');
user.addQuery('select_action_type');
user.query();
var actionType = g_form.getValue('select_action_type');
g_form.setValue('u_activity', actionType);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 09:46 AM
This would work better as a before Insert Business Rule on the sc_req_item table. The script on the Advanced tab would look like this:
(function executeRule(current, previous /*null when async*/) {
current.u_activity = current.variables.select_action_type;
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2025 11:07 AM
@smahmud -
The above client script is not working because of few key reasons:
1. Client Script execution timing:
- If you are setting the value in an onLoad client script, the select_action_type field may not be populated, specially if it is dynamically set by a catalog item.
- If it's an onSubmit client script, it might not properly update the activity field before submission.
Scope of the Script:- Since select_action_type is part of catalog items and activity is on sc_req_item, ensure the script runs at the correct time and scope is important.
Solution:
ou can use an onSubmit Client Script (runs before the form submission) on the sc_req_item table:
Client Script:
- Table: sc_req_item
- Type: onSubmit
- Script:
(function executeRule(current, gForm, gSNC) {
// Check if 'select_action_type' exists before assigning it to 'activity'
var actionType = gForm.getValue('select_action_type');
if (actionType) {
gForm.setValue('activity', actionType);
}
})(current, gForm, gSNC);
Alternative Solution using Business Rule (Server-side)
If you want this to be enforced universally at the database level, create a Business Rule:
- Table: sc_req_item
- When: before insert
- Condition: current.select_action_type.changes()
- Script:
(function executeRule(current) {
if (!current.activity) {
current.activity = current.select_action_type;
}
})(current);
Why business rule is better at the database level than client script at the user end?
- It ensures activity is always set at database level, even if a form is submitted via API or external integrations.
- Client-side scripts only work when users interact through the UI, but Business Rules apply universally.
Kindly mark it as correct/helpful, if it resolves your issue. Please press the "like" button to vote for me.
With Regards,
Krishna Kumar M