Count Values from Multiple Fields

Forums General discussion Count Values from Multiple Fields

This topic contains 4 replies, has 2 voices, and was last updated by  Alexander Bautz 2 months, 3 weeks ago.

  • Author
    Posts
  • #23888

    Kim West
    Participant

    Hi Alexander,

    I hope you can help. My form has 12 fields that all have the same choice options: N/A, Submitted, In Progress, Approved, Declined. I have 2 number fields, RequestedCount and ReceivedCount.

    Any field status with a value of Submitted or In Progress needs to be added in the RequestedCount
    Any field status with a value of Approved or Declined needs to be added in the ReceivedCount

    Once the 2 counts are equal, that would trigger a rule to send an email notification that all responses have been received. I have tried scripting this different ways, but haven’t been able to get anything to work. Thank you in advance for any help/assistance you can provide!

  • #23893

    Kim West
    Participant

    The RequestCount cones from the new form, so on the edit form, it would already be there. As I get the responses I need some way to know how many fields need an approval/decline so that when all are received I can fire another rule.

    Thanks again!

  • #23903

    Alexander Bautz
    Keymaster

    In you NewForm Custom JS you must add this snippet (merge it with the existing function if you have used the same function name already):

    function dffs_PreSaveAction(){
        var arrOfFields = ["Status1","Status2","Status3"], count = 0;
        jQuery.each(arrOfFields,function(i,fin){
            var v = getFieldValue(fin);
            if(v === "Submitted" || v === "In Progress"){
                count += 1;
            }
        });
        setFieldValue("RequestedCount",count);
        return true;
    }

    Then add this to your EditForm Custom JS:

    function dffs_PreSaveAction(){
        var arrOfFields = ["Status1","Status2","Status3"], count = 0;
        jQuery.each(arrOfFields,function(i,fin){
            var v = getFieldValue(fin);
            if(v === "Approved" || v === "Declined"){
                count += 1;
            }
        });
        setFieldValue("ReceivedCount",count);
        if(getFieldValue("RequestedCount") === String(count)){
            // Same number - trigger email configured in E-Mail and Print tab by ID:
            spjs.dffs.processEmailTemplate("testMail");
        }
        return true;
    }

    Change the fields “Status1”, “Status2” etc. to match your field names.

    Let me know how this works out.

    Alexander

  • #23922

    Kim West
    Participant

    Alexander, thank you so much! This is awesome! I did have to make one minor change, for some reason it wasn’t saving the form, so I just changed the function name, and set up a rule to call the function when the form is saved.

  • #23924

    Alexander Bautz
    Keymaster

    I’m glad you got it running – and thank you for your donation!

    Best regards,
    Alexander

You must be logged in to reply to this topic.