OnChange Client script not working on SC_TASK table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2025 02:49 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2025 03:13 PM
I see you are checking for state = 3 if the first "if" which is 'Closed Complete', the "else" logic applies to all others. But there are also "Closed Incomplete" and "Closed Skipped". Maybe that is intentional. Also, the above doesn't address requiring an attachment that I can see. Anyway, I would add "alert()' statements in the script to see items of interest to help you debug.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2025 05:29 PM
Hi @anerivirani19 ,
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
var shortDesc = g_form.getValue('short_description');
var isTargetShortDesc = shortDesc && shortDesc.startsWith("Add to Inventory Service and Review attached Health Check");
if (newValue == '3' && isTargetShortDesc) {
g_form.setMandatory('n_act_Initial_compliance_validation_on_configuration_item_question', true);
if (g_form.getValue('n_act_Initial_compliance_validation_on_configuration_item_question') == 'yes') {
g_form.setMandatory('n_act_evidence_Initial_compliance_validation_on', true);
} else if (g_form.getValue('n_act_Initial_compliance_validation_on_configuration_item_question') == 'no') {
g_form.setMandatory('n_act_no_reason_Initial_compliance_validation_on', true);
}
g_form.setMandatory('n_act_configuration_item_in_appropriate_security_inventory_databases_question', true);
if (g_form.getValue('n_act_configuration_item_in_appropriate_security_inventory_databases_question') == 'yes') {
g_form.setMandatory('n_act_evidence_configuration_item_in_appropriate_security_inventory_databases', true);
} else if (g_form.getValue('n_act_configuration_item_in_appropriate_security_inventory_databases_question') == 'no') {
g_form.setMandatory('n_act_no_reason_configuration_item_in_appropriate_security_inventory_databases', true);
}
var mandatoryVars = [
'n_act_lifecycle_status',
'n_act_business_need_required',
'n_act_userid_process_required',
'n_act_employment_verification_required',
'n_act_health_check_required',
'n_act_manual_health_check_required',
'n_act_non_privileged_revalidation_required',
'n_act_privileged_revalidation_required',
'n_act_software_collection_required',
'n_act_opaque_id',
'n_act_bac_id',
'n_act_bam_id',
'n_act_compliance_reporting',
'n_act_parent_system_type',
'n_act_operating_system_release',
'n_act_security_managed_by'
];
mandatoryVars.forEach(function(v) {
g_form.setMandatory(v, true);
});
} else if (isTargetShortDesc) {
// Reset all to non-mandatory
var allVars = [
'n_act_Initial_compliance_validation_on_configuration_item_question',
'n_act_evidence_Initial_compliance_validation_on',
'n_act_no_reason_Initial_compliance_validation_on',
'n_act_configuration_item_in_appropriate_security_inventory_databases_question',
'n_act_evidence_configuration_item_in_appropriate_security_inventory_databases',
'n_act_no_reason_configuration_item_in_appropriate_security_inventory_databases',
'n_act_lifecycle_status',
'n_act_business_need_required',
'n_act_userid_process_required',
'n_act_employment_verification_required',
'n_act_health_check_required',
'n_act_manual_health_check_required',
'n_act_non_privileged_revalidation_required',
'n_act_privileged_revalidation_required',
'n_act_software_collection_required',
'n_act_opaque_id',
'n_act_bac_id',
'n_act_bam_id',
'n_act_compliance_reporting',
'n_act_parent_system_type',
'n_act_operating_system_release',
'n_act_security_managed_by'
];
allVars.forEach(function(v) {
g_form.setMandatory(v, false);
});
}
}
try this script and adjust it if required
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-13-2025 11:50 AM
how does re-structuring the code that results in the same outcome help?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-12-2025 06:38 PM
Hi @anerivirani19,
Why Not Use UI Policies Instead of Client Scripts?
Instead of using Client Scripts with onChange, you should really consider using a UI Policy for this.
A UI Policy lets you define which fields will be mandatory or not, and the best part is, you won't need to write any script! A UI Policy is generally a much better fit for this kind of scenario.