Team is there any way to avoid repetition of code in onchange client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Hello Team,
My question is that I am using one on change client script and on that client script I am clearing 10 values when CI is x again same values I am clearing when CI is y, and CI is C so code length is increasing, how can I avoid it within catalog client script or we don't have another way to do it
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Hi @Priya Singh 2 2 ,
try using the Arrays and Loops to keep the code to fewer lines
please share the code if you still need assistance
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
4 weeks ago
May you try some like this
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// List of fields to clear
var fieldsToClear = [
'field1',
'field2',
'field3',
'field4',
'field5',
'field6',
'field7',
'field8',
'field9',
'field10'
];
// If CI is one of the values
var ciValues = ['X', 'Y', 'C'];
if (ciValues.indexOf(newValue) > -1) {
fieldsToClear.forEach(function(field) {
g_form.clearValue(field);
});
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
This is the code refactored:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Always clear these fields
var clearFields = ['site_url', 'site_owner_admin', 'description', 'permission', 'user_s_requesting_access'];
clearFields.forEach(function (f) {
g_form.clearValue(f);
});
// Define field rules for each scenario
var config = {
"others_specify": {
site_url: { mandatory: true, display: true },
others_please_specify_in_detail: { mandatory: false, display: true },
site_owner_admin: { mandatory: false, display: true },
description: { mandatory: true, display: true },
permission: { mandatory: true, display: true },
user_s_requesting_access: { mandatory: true, display: true },
access_level: { mandatory: true, display: true },
business_justification: { mandatory: false, display: false },
start_date: { mandatory: false, display: false },
end_date: { mandatory: false, display: false },
source_url: { mandatory: false, display: false },
destination_url: { mandatory: false, display: false },
site_owner_source_site: { mandatory: false, display: false },
site_owner_destination_site: { mandatory: false, display: false },
preferred_date_and_time_of_migration: { mandatory: false, display: false }
},
"permissions": { // for Read, Contribute, Edit, etc.
site_url: { mandatory: true, display: true },
others_please_specify_in_detail: { mandatory: false, display: false },
site_owner_admin: { mandatory: false, display: true },
description: { mandatory: true, display: true },
permission: { mandatory: true, display: true },
user_s_requesting_access: { mandatory: true, display: true },
access_level: { mandatory: true, display: true },
business_justification: { mandatory: false, display: false },
start_date: { mandatory: false, display: false },
end_date: { mandatory: false, display: false },
source_url: { mandatory: false, display: false },
destination_url: { mandatory: false, display: false },
site_owner_source_site: { mandatory: false, display: false },
site_owner_destination_site: { mandatory: false, display: false },
preferred_date_and_time_of_migration: { mandatory: false, display: false }
}
};
// Permissions list
var permissionValues = ['Read', 'Contribute', 'Edit', 'Full Control', 'Site Collection Administration'];
// Decide which config to use
var activeConfig = null;
if (newValue == 'others_specify') {
activeConfig = config.others_specify;
} else if (permissionValues.indexOf(newValue) > -1) {
activeConfig = config.permissions;
}
// Apply the rules
if (activeConfig) {
Object.keys(activeConfig).forEach(function (field) {
g_form.setMandatory(field, activeConfig[field].mandatory);
g_form.setDisplay(field, activeConfig[field].display);
});
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 weeks ago
Do you want to clear the variables whenever the CI value changes? or When the CI is one of some n values?
On a high level, I would recommend to do the clear variables in a function and to call that function to clear your variables.
If its the first case then you can use a condition like (old_value != new_value) and then call the function.
Or
If you want to check if CI is one of some n values, then store the CI values in an array and loop over the array to check if your CI is one of the values and then clear options calling the function you created to clear the variables.
Please mark this response as correct or helpful if it assisted you with your question.
Thanks,
Rishi.