Remove Option on category not working in Onload client script

dagarson
Tera Guru

Hello I am trying to remove some category options when a case is assigned to a specific group. I was able to get a script working with a ui policy for on change but I need the same to work when you load the form. So I am an onload client script but it doesnt seem to remove the category options. All the alerts are running so I know the script is hitting but the options arent being removed. Here is the script

 

function onLoad() {
    var targetGroup = 'b5ca7f751b70529009c7202ce54bcb78'; 
    var currentGroup = g_form.getValue('assignment_group');

    // Proceed only if the current group matches the target group
    if (currentGroup === targetGroup) {
        alert("Target group matched.");

        // Alert current category value
        var currentCategoryValue = g_form.getValue('category');
        alert("Current category value: " + currentCategoryValue);

        // Remove categories one by one
        g_form.removeOption('category', '1000');
        g_form.removeOption('category', '1001');
        g_form.removeOption('category', '120000000000000');
        g_form.removeOption('category', '12311111');
        g_form.removeOption('category', '1233333332');
        g_form.removeOption('category', '21111111');
        g_form.removeOption('category', '211111111112');
        g_form.removeOption('category', '2222221111');
        g_form.removeOption('category', '222222211111114');
        g_form.removeOption('category', '222222221111');
        g_form.removeOption('category', '22222224');
        g_form.removeOption('category', '4030942586');
        g_form.removeOption('category', '4222211');
        g_form.removeOption('category', '43222222');
        g_form.removeOption('category', '455555621');
        g_form.removeOption('category', '54411225');
        g_form.removeOption('category', '544444444');
        g_form.removeOption('category', '563842034');
        g_form.removeOption('category', '57391921111');
        g_form.removeOption('category', '600');
        g_form.removeOption('category', '601');
        g_form.removeOption('category', '602');
        g_form.removeOption('category', '603');
        g_form.removeOption('category', '604');
        g_form.removeOption('category', '605');
        g_form.removeOption('category', '6051');
        g_form.removeOption('category', '606');
        g_form.removeOption('category', '607');
        g_form.removeOption('category', '608');
        g_form.removeOption('category', '609');
        g_form.removeOption('category', '6091');
        g_form.removeOption('category', '610');
        g_form.removeOption('category', '611');
        g_form.removeOption('category', '6111');
        g_form.removeOption('category', '6112');
        g_form.removeOption('category', '6113');
        g_form.removeOption('category', '612');
        g_form.removeOption('category', '613');
        g_form.removeOption('category', '614');
        g_form.removeOption('category', '615');
        g_form.removeOption('category', '616');
        g_form.removeOption('category', '617');
        g_form.removeOption('category', '618');
        g_form.removeOption('category', '619');
        g_form.removeOption('category', '620');
        g_form.removeOption('category', '621');
        g_form.removeOption('category', '622');
        g_form.removeOption('category', '623');
        g_form.removeOption('category', '624');
        g_form.removeOption('category', '625');
        g_form.removeOption('category', '626');
        g_form.removeOption('category', '627');
        g_form.removeOption('category', '644443333');
        g_form.removeOption('category', '69045032');
        g_form.removeOption('category', '701');
        g_form.removeOption('category', '702');
        g_form.removeOption('category', '703');
        g_form.removeOption('category', '704');
        g_form.removeOption('category', '705');
        g_form.removeOption('category', '706');
        g_form.removeOption('category', '707');
        g_form.removeOption('category', '708');
        g_form.removeOption('category', '73222111');
        g_form.removeOption('category', '7435945');
        g_form.removeOption('category', '750');
        g_form.removeOption('category', '751');
        g_form.removeOption('category', '752');
        g_form.removeOption('category', '753');
        g_form.removeOption('category', '754');
        g_form.removeOption('category', '755');
        g_form.removeOption('category', '756');
        g_form.removeOption('category', '757');
        g_form.removeOption('category', '758');
        g_form.removeOption('category', '759');
        g_form.removeOption('category', '7845555');
        g_form.removeOption('category', '785555555');
        g_form.removeOption('category', '78888888');
        g_form.removeOption('category', '7950320324');
        g_form.removeOption('category', '8655766');
        g_form.removeOption('category', '877776677');
        g_form.removeOption('category', '9322222');

        alert("Categories have been removed.");
    }
}

Any assistance would be helpful.

1 ACCEPTED SOLUTION

dagarson
Tera Guru

I was able to get this resolved. by using the setTimeout function along with clearOption I was able to clear every option and only add the ones I wanted.

View solution in original post

7 REPLIES 7

Sai Shravan
Mega Sage

Hello @dagarson ,

 

Can you give a try with below script 

 

function onLoad() {
    var targetGroup = 'b5ca7f751b70529009c7202ce54bcb78'; 
    var currentGroup = g_form.getValue('assignment_group');

    // Proceed only if the current group matches the target group
    if (currentGroup === targetGroup) {
        alert("Target group matched.");

        // Function to remove categories
        removeCategories();
    }
}

function removeCategories() {
    var categoriesToRemove = [
        '1000', '1001', '120000000000000', '12311111', '1233333332',
        '21111111', '211111111112', '2222221111', '222222211111114',
        '222222221111', '22222224', '4030942586', '4222211', 
        '43222222', '455555621', '54411225', '544444444', 
        '563842034', '57391921111', '600', '601', '602', 
        '603', '604', '605', '6051', '606', '607', 
        '608', '609', '6091', '610', '611', '6111', 
        '6112', '6113', '612', '613', '614', '615', 
        '616', '617', '618', '619', '620', '621', 
        '622', '623', '624', '625', '626', '627', 
        '644443333', '69045032', '701', '702', '703', 
        '704', '705', '706', '707', '708', '73222111', 
        '7435945', '750', '751', '752', '753', '754', 
        '755', '756', '757', '758', '759', '7845555', 
        '785555555', '78888888', '7950320324', '8655766', 
        '877776677', '9322222'
    ];

    categoriesToRemove.forEach(function(category) {
        g_form.removeOption('category', category);
    });

    alert("Categories have been removed.");
}
Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

Thanks for reaching out. That still didnt work. All the alerts still hit but the category options arent removed.

Sandeep Rajput
Tera Patron
Tera Patron

@dagarson the second argument which you are passing in 

g_form.removeOption('category'

 

is it a choice value or choice label? 

the value.