Script Troubleshooting help

Script Troubleshooting help

I have form called “Add a Student” with a multiselect field called “Enrolled Classes”.

I have another form called “Classes” with a field called "Max number of students” and a field called "Number of Students Enrolled” which counts how many students are listed as enrolled in that class.

When I load my “Add a Student" form I only want to be able to select Classes that have spots available in them. Ie: "Max number of students” < "Number of Students Enrolled”. I have a workflow to do this:

classesAvailableList = Add_a_Class[Number_of_Students_Enrolled < Max_number_of_students].ID.getAll();
input.Enrolled_Classes:ui.add(classesAvailableList);

However the issue I am having is that if I select a student to edit, any classes that where previously selected are deselected. - https://www.loom.com/share/3db7108aefc4415980e0c9eb4ca705bb 

Any ideas how I could fix this?