Get Embedded List Row Count with Client Script

aaron_damyen
Kilo Expert

Does anyone have functioning code (Eureka or Fuji or later) for retrieving the current number of rows in an Embedded list with a Client Script?

We would like to perform an onSubmit check to ensure there are an appropriate number of entries in an Embedded list.   Querying the related table does not work because the newly entered rows in the Embedded list are not save until the form is saved.   This also prevents us from performing the change during [Before] Business Rules.   An [After] Business Rule is not appropriate as modifying the record which was just saved is not appropriate use of an [After] Business Rule (according to support), and it has inconsistent behavior.

I know that there is a totalRows and grandTotalRows property on the embedded list object (GlideList2, I think), but it always has the value of when the embedded list was loaded, not the current count.

Another possibility might be to force the saving of the Embedded List before saving the form, but I don't know how to do that either.   The Embedded List documentation doesn't show a save()-type functionality.

Thanks in advance,

Aaron

1 ACCEPTED SOLUTION

Addendum.   We've recently upgraded to Fuji patch 3 and found a bug with this particular implementation.



There is what appears to be a bug in that the first row entered into an embedded list will still have a class of 'list_add'.   The second entry, however, will not.   Our users needed to enter one additional entry in order for the check to finally pass.



I suspect this is not appropriate behavior, but since this is outside of supportable functionality, it probably won't get fixed.



My amended version of the script follows.   This version counts up the number of 'list_add' rows and subtracts this value minus 1 from the overall total.   Basically, allowing for only one 'list_add' to be removed from the count.   This intent for this rather cryptic implementation was to handle a situation where additional 'list_add' rows are improperly updated.   (If the bug happens once, why not twice?)   This implementation will also work properly when the previous functionality is restored.



function getCount(embeddedListTableName) {


  // bug in platform.   list_add remains on first row entered


  var listadds = $$('[record_class='+ embeddedListTableName + ']:not(.list_delete)');


  var additional = 0;


  if(listadds.length > 1) {


  // subtract off the one empty listadd row


  additional = listadds.length - 1;


  }


  var elements = $$('[record_class='+ embeddedListTableName + ']:not(.list_add):not(.list_delete)');


  return elements.length + additional;


}


View solution in original post

6 REPLIES 6

edwin_munoz
Mega Guru

Hello Aaron,



Are you looking for a way to to this validation from server-side?



On the client-side you can write an onSubmit script that counts the number of elements that have the attribute "record-class" and the value is equal to the name of your list.


Thanks for the quick response Edwin Munoz.



I'm looking to do this validation client-side.   My users have a habit of assuming everything is appropriate when the submit goes through.   So I have a lot of client-side scripts to do validations.



I have no experience with querying the DOM.   Is your suggestion something I can do with a script similar to this? (See Embedded list section http://www.servicenowguru.com/scripting/client-scripts-scripting/hiding-related-lists-embedded-lists... - Thank you Mark Stanger).



Aaron


Hello Aaron,



If you are certain that users are using modern browsers you can do it like this:



var embeddedListElements =   document.querySelectorAll("[record_class=nameOfList]");



if (embeddedListElements.length < 1){


//code to prevent submission here


}


Got it.   Thanks a million, Edwin Munoz



Here's my function for everyone else.   I suspect there are still some gaps in it (like checking that the list actually is embedded).   But, since it is a workaround, it'll probably break with the next release.




function getCount(embeddedListTableName) {


              var elements = $$('[record_class='+ embeddedListTableName + ']:not(.list_add)');


              return elements.length;


}



I added the exclusion of the list_add class because my lists are editable and the extra line was offsetting the value.   I remember reading somewhere that these lists can be read-only, so I would gather there wouldn't be a list_add row in that scenario.