How do I "Remove All" multirow variable set rows using a client script?

kevinharalson
Mega Expert

I need a client script for use in an onChange function to remove all rows from a multirow variable set.

Scenario

  1. A user will select a CI in a catalog item, and add maximum rows to a multirow variable set based on max_rows attribute.
  2. Then, the user realizes the wrong CI was selected, and changes the selected CI
  3. Upon selection of a new CI, I need a client script to remove all rows from the multirow variable set and enable the "Add" button.

I have tried the following without success

  • g_form.setValue(< mrvs name>, '');
  • g_form.setValue(< mrvs name>, '[]');
  • g_form.clearValue(< mrvs name>);

In all instances the information is removed, but the "Add" button remains disabled.

1 ACCEPTED SOLUTION
30 REPLIES 30

okay, backend i have to verify once. may be i checked only in SP. 

Willem
Giga Sage
Giga Sage

I got it working guys!

As it is a bit much I have written an article on it:

https://community.servicenow.com/community?id=community_article&sys_id=55a660441bdfdc90305fea89bd4bc...

Willem
Giga Sage
Giga Sage

@kevinharalson 

Hope you are doing well.

Is your question resolved? Or do we need to follow-up on this?

Please mark the answer as correct if it solves your question. This will help others who are looking for a similar solution. Also marking this answer as correct takes the post of the unsolved list.
Thanks.

Kind regards,

Willem

I was out of the office last week, but I had not found a resolution as of my departure.

I have not been able to review and test your lastest post from 10 days ago.

Thank you! The function worked for me, but I adjusted it a little to preserve and reuse the original max_rows value. This will allow the max_rows to be adjusted on the MRVS without having to revisit the client script.

function onChange(control, oldValue, newValue, isLoading) {
     if (isLoading || newValue == '') {
          return;
     }
     try { // try in service portal.
          var field = g_form.getField("variable_set_1");
          if (field != null) {
               var max_rows = field.max_rows_size;
               g_form.setValue('variable_set_1', JSON.stringify([]));
               field.max_rows_size = max_rows;
          }
     }
     catch(e){ //desktop
          function clearRows (sysIdMRWS) {
               TableVariableService.removeAllRows(sysIdMRWS);
          }
          clearRows("variable_set_id");
     }
}