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

Been struggling with the code tags myself.   Switch to the chevron menu -> Syntax Highlight



SyntaxHighlight.png


function onSubmit() {


    alert("Thanks!");


}


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


sid31
Mega Contributor

Ran into the same problem and this code was very helpful. I made a slight tweak to it that I thought I'd add here in-case someone else runs into the same thing.



The code above almost works for me except that if a user adds a row, and then later marks it for deletion using the 'X' button, they can still save the record with no rows because list.table.getElementsBySelector('tr.list_row') will still count that row.



So I modified the code as follows:



function onSubmit() {


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


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


  strClass;



  //iterate through the embedded list to get true number of rows added. We have to


  //exclude the blank 'insert row' and any rows that have been marked for deletion


  var taskCount = 0;


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


            strClass = rows[i].getAttribute('class');


            if (!strClass.includes('list_add') && !strClass.includes('list_delete')) { taskCount++; }


  }



  if (taskCount == 0){


            alert("Please add at least 1 task before saving");


            return false;


  } else {


            alert(taskCount + " tasks up in here!!");


  }


            return true;


}



Not sure if this is the best way to do this but it seems to work.


Uncle Rob
Kilo Patron

Looks like you can only mark two answers as helpful, but I just wanted you guys to know, y'all are beasts.