calculating the number of selections in a related items category field

Comments

10 comments

  • William Willman

    figured this one out that i can use @all of voicemail.length but now I'm trying to do the same with @all of clients.length for another category i have but it doesn't show any available tokens...

    0
    Comment actions Permalink
  • William Willman

    actually i did not figure it out at all... @all of voicemail.length pointed to a separate field i created by accident trying something out. is there a way to have as stated in the original question?

    0
    Comment actions Permalink
  • Rainer Grabowski

    Hi William,

    I assume, lead titel field is a text field and type is a category field. If so, for each lead category you can use:

    var lead = @All of Your lead title field;   
    var type = @All of Your lead Type field; 
    
    var count = 0;
    for(var i = 0; i <  lead.length; i++){
    if(type[i] == "received"){
    count++
    }
    };
    

    Rainer

    0
    Comment actions Permalink
  • William Willman

    hey rainier! you seem to always be a big help around here (:
    I'm trying to use this but its saying returned undefined result. also one problem i might be able to forsee is that i do not have any text fields automatically being populated when a new lead is generated. (first name is a text field but obviously if its a voicemail i haven't spoke to them yet to get a name)

    var lead = @All of First Name;

    var type = @All of Lead Type;

    var count = 0;
    for(var i = 0; i < lead.length; i++){
    if(type[i] == "Voicemail"){
    count++
    }
    };

    0
    Comment actions Permalink
  • Rainer Grabowski

    Hi William,

    instead of @All of First Name; you can take every other field - it's only important that it is a field which always has a value. If e.g. @All of Lead Type always has a value, you only need this one var. Then you can use

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

    Please try this to avoid the error notification. Open an item in the app where the calculation field is in and which has relations to leads, click Modify Template, create a new calculation field, enter the code, and click Done.

    Hope that helps.

    0
    Comment actions Permalink
  • William Willman

    ok i have this but its still not givin er to me (undefined result). I'm taking a js course online to try to catch up so i really appreciate you're help!

    var type = @All of Lead Type;
    var count = 0;
    for(var i = 0; i < type.length; i++){
    if(type[i] == "Voicemail"){
    count++
    }
    };

    can you explain this line to me?

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

    (also how do you get your code in the box like that?)

    0
    Comment actions Permalink
  • William Willman

    so i now understand why this code should work (array loop and such) but i do not know why it does not... i have tried what you said of creating a new item then modify temp, I've done it from the main view in the app, as well as opening an existing item in the app and doing it.

    var type = @All of Lead Type;
    var count = 0;
    for(var i = 0; i <type.length; i++){
    if(type[i] == "Voicemail"){
    count++
    }
    };

    0
    Comment actions Permalink
  • William Willman

    nvm figured it out was missing else at the end:

    var type = @All of Lead Type;
    var count = 0;
    for(i = 0; i <type.length; i++){
    if(type[i] == "Voicemail"){
    count++
    }
    else{
    count
    }
    };

    0
    Comment actions Permalink
  • William Willman

    so now I'm feeling a bit more comfortable with js and I've come up with this and it goes in but all i get as an output is

    Voicemail = NaN%

    var type= @All of Lead Type;
    var leadTotal = type.length;

    var countR = 0;
    for(i = 0; i <type.length; i++){
    if(type[i] == "Received"){
    countR++
    }
    else{
    countR
    }
    };

    var countM = 0;
    for(i = 0; i <type.length; i++){
    if(type[i] == "Missed"){
    countM++
    }
    else{
    countM
    }
    };

    var countV = 0;
    for(i = 0; i <type.length; i++){
    if(type[i] == "Voicemail"){
    countV++
    }
    else{
    countV
    }
    };

    var percR= (countR.length / leadTotal * 100).toFixed(1);
    var percM= (countM.length / leadTotal * 100).toFixed(1);
    var percV= (countV.length / leadTotal * 100).toFixed(1);

    "Total: " + leadTotal + "Leads \n" +
    "Received: " + percR + "% \n" +
    "Missed: " + percM + "%"
    "Voicemail: " + percV + "%"

    0
    Comment actions Permalink
  • Rich Siegmund

    This post and the comments helped me, but there was missing operator that took me a few minutes to realize.

    This working code snippet was directly copied from a Contacts App with items that can have zero, one or more related Deals Apps that have a field named Stage.  Stage is a category field and therefore always exists.

    The variable 'stage' is an array of the different stages from related Deals. If there are 3 related Deals the array would have 3 elements

    The for loop checks each array element for being in either Unreached or Quoting stage

    The last if else else block sets the variable 'yesno' to based on the number of elements

    if 0, yesno equals ...
    if 1, yesno equals YES
    if >1, yesno equals Multiple

    Thanks for Rainer and William .... hope this helps!

    var stage = @All of Stage
    var count = 0;
    var yesno = "";

    for(var i = 0; i < stage.length; i++){
    if((stage[i] == "Quoting") || (stage[i] == "Unreached")){
    count++
    }
    else {
    count
    }
    };

    if (count == 0) { yesno = "..." }
    else { if (count == 1) { yesno = "YES" }
    else { yesno = "Multiple" }
    };
    yesno

    0
    Comment actions Permalink

Please sign in to leave a comment.

Powered by Zendesk