We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Team is there any way to avoid repetition of code in onchange client script

Priya Singh 2 2
Tera Contributor

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 

6 REPLIES 6

Ankur Bawiskar
Tera Patron

@Priya Singh 2 2 

share your current script

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

On Change client script:

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {

        return;

    }

 

    if (newValue == 'others_specify') {

 

        g_form.clearValue('site_url');

        g_form.clearValue('site_owner_admin');

        g_form.clearValue('description');

        g_form.clearValue('permission');

        g_form.clearValue('user_s_requesting_access');

 

 

        g_form.setMandatory('site_url', true);

        g_form.setDisplay('site_url', true);

 

        g_form.setMandatory('others_please_specify_in_detail', false);

        g_form.setDisplay('others_please_specify_in_detail', true);

 

        g_form.setMandatory('site_owner_admin', false);

        g_form.setDisplay('site_owner_admin', true);

 

        g_form.setMandatory('description', true);

        g_form.setDisplay('description', true);

 

        g_form.setMandatory('permission', true);

        g_form.setDisplay('permission', true);

 

        g_form.setMandatory('business_justification', false);

        g_form.setDisplay('business_justification', false);

 

        g_form.setMandatory('user_s_requesting_access', true);

        g_form.setDisplay('user_s_requesting_access', true);

 

        g_form.setMandatory('start_date', false);

        g_form.setDisplay('start_date', false);

 

        g_form.setMandatory('end_date', false);

        g_form.setDisplay('end_date', false);

 

        g_form.setMandatory('access_level', true);

        g_form.setDisplay('access_level', true);

 

        g_form.setMandatory('source_url', false);

        g_form.setDisplay('source_url', false);

 

        g_form.setMandatory('destination_url', false);

        g_form.setDisplay('destination_url', false);

 

        g_form.setMandatory('site_owner_source_site', false);

        g_form.setDisplay('end_datesite_owner_source_site', false);

 

        g_form.setMandatory('site_owner_destination_site', false);

        g_form.setDisplay('site_owner_destination_site', false);

 

        g_form.setMandatory('preferred_date_and_time_of_migration', false);

        g_form.setDisplay('preferred_date_and_time_of_migration', false);

 

    } else if (newValue == 'Read' || newValue == 'Contribute' || newValue == 'Edit' || newValue == 'Full Control' || newValue == 'Site Collection Administration') {

 

        g_form.clearValue('site_url');

        g_form.clearValue('site_owner_admin');

        g_form.clearValue('description');

        g_form.clearValue('permission');

        g_form.clearValue('user_s_requesting_access');

 

 

        g_form.setMandatory('site_url', true);

        g_form.setDisplay('site_url', true);

 

        g_form.setMandatory('others_please_specify_in_detail', false);

        g_form.setDisplay('others_please_specify_in_detail', false);

 

        g_form.setMandatory('site_owner_admin', false);

        g_form.setDisplay('site_owner_admin', true);

 

        g_form.setMandatory('description', true);

        g_form.setDisplay('description', true);

 

        g_form.setMandatory('permission', true);

        g_form.setDisplay('permission', true);

 

        g_form.setMandatory('access_level', true);

        g_form.setDisplay('access_level', true);

 

        g_form.setMandatory('business_justification', false);

        g_form.setDisplay('business_justification', false);

 

        g_form.setMandatory('user_s_requesting_access', true);

        g_form.setDisplay('user_s_requesting_access', true);

 

        g_form.setMandatory('start_date', false);

        g_form.setDisplay('start_date', false);

 

        g_form.setMandatory('end_date', false);

        g_form.setDisplay('end_date', false);

 

        g_form.setMandatory('source_url', false);

        g_form.setDisplay('source_url', false);

 

        g_form.setMandatory('destination_url', false);

        g_form.setDisplay('destination_url', false);

 

        g_form.setMandatory('site_owner_source_site', false);

        g_form.setDisplay('end_datesite_owner_source_site', false);

 

        g_form.setMandatory('site_owner_destination_site', false);

        g_form.setDisplay('site_owner_destination_site', false);

 

        g_form.setMandatory('preferred_date_and_time_of_migration', false);

        g_form.setDisplay('preferred_date_and_time_of_migration', false);

 

    }

}

 

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue == '') {

        return;

    }

 

    if (newValue == 'others_specify') {

 

        g_form.clearValue('site_url');

        g_form.clearValue('site_owner_admin');

        g_form.clearValue('description');

        g_form.clearValue('permission');

        g_form.clearValue('user_s_requesting_access');

 

 

        g_form.setMandatory('site_url', true);

        g_form.setDisplay('site_url', true);

 

        g_form.setMandatory('others_please_specify_in_detail', false);

        g_form.setDisplay('others_please_specify_in_detail', true);

 

        g_form.setMandatory('site_owner_admin', false);

        g_form.setDisplay('site_owner_admin', true);

 

        g_form.setMandatory('description', true);

        g_form.setDisplay('description', true);

 

        g_form.setMandatory('permission', true);

        g_form.setDisplay('permission', true);

 

        g_form.setMandatory('business_justification', false);

        g_form.setDisplay('business_justification', false);

 

        g_form.setMandatory('user_s_requesting_access', true);

        g_form.setDisplay('user_s_requesting_access', true);

 

        g_form.setMandatory('start_date', false);

        g_form.setDisplay('start_date', false);

 

        g_form.setMandatory('end_date', false);

        g_form.setDisplay('end_date', false);

 

        g_form.setMandatory('access_level', true);

        g_form.setDisplay('access_level', true);

 

        g_form.setMandatory('source_url', false);

        g_form.setDisplay('source_url', false);

 

        g_form.setMandatory('destination_url', false);

        g_form.setDisplay('destination_url', false);

 

        g_form.setMandatory('site_owner_source_site', false);

        g_form.setDisplay('end_datesite_owner_source_site', false);

 

        g_form.setMandatory('site_owner_destination_site', false);

        g_form.setDisplay('site_owner_destination_site', false);

 

        g_form.setMandatory('preferred_date_and_time_of_migration', false);

        g_form.setDisplay('preferred_date_and_time_of_migration', false);

 

    } else if (newValue == 'Read' || newValue == 'Contribute' || newValue == 'Edit' || newValue == 'Full Control' || newValue == 'Site Collection Administration') {

 

        g_form.clearValue('site_url');

        g_form.clearValue('site_owner_admin');

        g_form.clearValue('description');

        g_form.clearValue('permission');

        g_form.clearValue('user_s_requesting_access');

 

 

        g_form.setMandatory('site_url', true);

        g_form.setDisplay('site_url', true);

 

        g_form.setMandatory('others_please_specify_in_detail', false);

        g_form.setDisplay('others_please_specify_in_detail', false);

 

        g_form.setMandatory('site_owner_admin', false);

        g_form.setDisplay('site_owner_admin', true);

 

        g_form.setMandatory('description', true);

        g_form.setDisplay('description', true);

 

        g_form.setMandatory('permission', true);

        g_form.setDisplay('permission', true);

 

        g_form.setMandatory('access_level', true);

        g_form.setDisplay('access_level', true);

 

        g_form.setMandatory('business_justification', false);

        g_form.setDisplay('business_justification', false);

 

        g_form.setMandatory('user_s_requesting_access', true);

        g_form.setDisplay('user_s_requesting_access', true);

 

        g_form.setMandatory('start_date', false);

        g_form.setDisplay('start_date', false);

 

        g_form.setMandatory('end_date', false);

        g_form.setDisplay('end_date', false);

 

        g_form.setMandatory('source_url', false);

        g_form.setDisplay('source_url', false);

 

        g_form.setMandatory('destination_url', false);

        g_form.setDisplay('destination_url', false);

 

        g_form.setMandatory('site_owner_source_site', false);

        g_form.setDisplay('end_datesite_owner_source_site', false);

 

        g_form.setMandatory('site_owner_destination_site', false);

        g_form.setDisplay('site_owner_destination_site', false);

 

        g_form.setMandatory('preferred_date_and_time_of_migration', false);

        g_form.setDisplay('preferred_date_and_time_of_migration', false);

 

    }

}

 

...............................................................................Variable Name - access_level................................................................

I am clearing same values again n again, how can I avoid repetition of code?