Query embedded list contents before submission?

Uncle Rob
Kilo Patron

So I'm working on what I thought might be a simple feature:   Require a user to add Change Tasks before a Change Request can be submitted for approval (via a Request For Approval UI Action).

I have Change Tasks as an embedded list on my Change Request form and my users love it.   So I'm wondering if I could key the UI Action visibility or at least have a client script check onSubmit (of the change request) if data has been populated in that embedded related list.   Everything I see on the GlideList2 wiki page seems to be altering the properties of the embedded related list, and not actually interacting with its data.

Am I barking up the wrong tree here?  

1 ACCEPTED SOLUTION

Uncle Rob
Kilo Patron

Ok, with the insights Travis Toulson and Cory Seering gave me, I cracked the case.




function onSubmit() {


  var list = GlideList2.getByName('change_request.change_task.change_request'),


  rows = list.table.getElementsBySelector('tr.list_row'),


  sys_id;



  // - 1 to exclude the insert row which is the last in the list of rows retrieved above


  if (rows.length - 1 == 0){


  alert("No Change Tasks");


  } else {


  alert(rows.length + " up in here.");


  }


}




Now all that's left to do is integrate this into my Request Approval button and I'm off to the races.   Which leaves one more order of business for Travis and Chris:



Cookie.png


View solution in original post

15 REPLIES 15

DOH!   Got this with the revised code.



onSubmit script error: TypeError: Cannot read property 'grandTotalRows' of null:


You will likely need to verify the name of your Embedded List.   I was able to replicate this error by using a name of a non-existing list.   Here is one way to verify the name of the list (one advantage of the earlier script is it relies on the name of the table, not figuring out ServiceNow's list name):



1.   Navigate to the Change Request page


2.   Open the Web Inspector Console


3.   Type gsft_main.GlideLists2 into the console and hit enter


4.   Look through the object tree outputted in the console.   Each object in the list is one of the GlideLists.   Embedded lists are identified by sys_id while Related Lists are a bit more human readable.


5.   Find the list for your embedded list and lookup the listName property



See the attached image for what you may see in the browser:



Screen Shot 2015-06-10 at 12.42.55 PM.png


tltoulson
Kilo Sage

To parse the records in the tree, you can use the following:



var list = GlideList2.getByName('change_request.change_task.parent'),


      rows = list.table.getElementsBySelector('tr.list_row'),


      sys_id;




// - 1 to exclude the insert row which is the last in the list of rows retrieved above


for (var i = 0; i < rows.length - 1; i++) {


      sys_id = rows[i].getAttribute('sys_id');


      console.log(list.getCell(sys_id, 'short_description').innerText); // getCell takes the sys_id of the record and the field name and returns the <td> DOM element containing the value.   The innerText property returns the contained table value.


}


And with that, you should be able to do just about anything you need with the embedded GlideList.


Hmmm... I'm still getting Type Errors...



onSubmit script error: TypeError: Cannot read property 'table' of null:


function onSubmit() {


var list = GlideList2.getByName('change_request.change_task.parent'),


rows = list.table.getElementsBySelector('tr.list_row'),


sys_id;




for (var i = 0; i < rows.length - 1; i++) {


  sys_id = rows[i].getAttribute('sys_id');


console.log(list.getCell(sys_id, 'short_description').innerText);


}


}


By default, the related list for change tasks appears to be named "change_request.change_task.change_request" (change_request is the form, change_task is the table the list queries from, change_request is the field on change_task which relates to the current record being viewed).



On a demo instance, for a change with a single related change task, I have run this code:




function onSubmit() {


    var list = GlideList2.getByName('change_request.change_task.change_request'),


    rows = list.table.getElementsBySelector('tr.list_row'),


    sys_id;



    for (var i = 0; i < rows.length; i++) {


          sys_id = rows[i].getAttribute('sys_id');


          console.log(list.getCell(sys_id, 'short_description').innerText);


    }


}




My output:


This is a change task short description!