Forum Discussion

AprylStoldt's avatar
AprylStoldt
Qrew Cadet
6 years ago

javascript to get count of multi-select choices

I am needing a way, in javascript, to get a count of how many choices a multi-select field has. I don't need how many were selected. Below is what I have so far, I can get a list of the choices, but cant figure out a way to count them. 

"javascript: " &
URLRoot() & "db/" & [_DBID_REQUESTS] & "?a=API_GetFieldProperties&fid=9" & "&rdr=";
  • There might be a cleaner way of XML parsing - but you can do something like this to make the call using a $.get and then parse the XML response and dig a little to get the choice count: 

    var text field = URLRoot() & "db/" & [_DBID_REQUESTS] & "?a=API_GetFieldProperties&fid=9";

    "javascript: {" & 
      "$.get('" & $urlD & "',function(data,success) {" & 
            "var count = data.getElementsByTagName('field')[0].getElementsByTagName('choices')[0].children.length);" &
           "$.get('" & URLRoot() & "db/" & [_DBID_REQUESTS] & "?a=API_EditRecord&rid=" & [Record ID#] & "&_fid_10='" + count);"
      "});" &   
    "}"

    In this example - you would do an Edit Recod call to store the value in the number of field choices - I put fid 10 for demo

    Chayce Duncan | Director of Strategic Solutions
    (720) 739-1406 | chayceduncan@quandarycg.com
    Quandary Knowledge Base
  • Sorry - I copied some of that from a different example

    var text field = URLRoot() & "db/" & [_DBID_REQUESTS] & "?a=API_GetFieldProperties&fid=9";

    "javascript: {" & 
      "$.get('" & $field & "',function(data,success) {" & 
            "var count = data.getElementsByTagName('field')[0].getElementsByTagName('choices')[0].children.length);" &
           "$.get('" & URLRoot() & "db/" & [_DBID_REQUESTS] & "?a=API_EditRecord&rid=" & [Record ID#] & "&_fid_10='" + count);"
      "});" &   
    "}"

    In this example - you would do an Edit Recod call to store the value in the number of field choices - I put fid 10 for demo 

    Chayce Duncan | Director of Strategic Solutions
    (720) 739-1406 | chayceduncan@quandarycg.com
    Quandary Knowledge Base
  • Im having an issue where this doesnt work for people who are not admins.i get the error: Uncaught TypeError: Cannot read property 'getElementsByTagName' of undefined.
  • im creating a conditional multi-select field, the code clears field choices, adds field choices, then loads new record page. I have it working for me, but it doesn't work for non-admin users.
  • Where/how are you populating the values? Is it one set of options based on role or user specific? Or is it more dynamic like the day or the project?

    Your use case to use code likely won't work as the ability to clear out field values requires the ability to edit fields which is usually reserved only for admins and super users. 

    Based on my question above - would it be possible to use a table - where you populate all the options you want shown - and then assign each option a 'type' or 'Role' that it applies to. Use that value to limit by role which options a person can see using custom permissions - and then set up a relationship to this 'Dropdowns' table so that when someone clicks the dropdown they can only see the values / records that you've assigned.


    Chayce Duncan | Director of Strategic Solutions
    (720) 739-1406 | chayceduncan@quandarycg.com
    Quandary Knowledge Base