Sum of 3 Values in Workflow

I am trying to populate the Sum of 3 values into a field. The 3 values are being calculated during the same workflow. Is this possible?

ie. case_fee + case_travbilled + case_perdiem = case_totalfee

where the first 3 factors are being calculated earlier in the execution as case_hours*100 results in case_fee.

I hope I am making sense.

Here is what I was trying:

tval=$('#case_case_bcsactualtime').val();
tseg=$('#_HIDDENcase_totalfee');
if (parseFloat(tval)<1){tseg.val('0')}
if (parseFloat(tval)>0){tseg.val(case_consultfee + case_travbilled + case_perdiem)}

  • 0

    Does the user need to see this total on the screen during data entry?

    If not, I would suggest writing a table script on Update that takes these 3 values, adds them , and sets the value of the total field.

  • 0

    Hi,

    Is your rule loading all of the fields into the workflow rule, so they are available on the screen, as below:

    If so, you can do something like this...

    I have 4 fields named:

    • case_one
    • case_two
    • case_three
    • case_fourth

    I want the total of case_one,case_two and case_three to be shown in case_fourth. I can add the following code to the on change of:

    • case_one
    • case_two
    • case_three

    case_fourth.value = (parseFloat(case_one.value) + parseFloat(case_two.value) + parseFloat(case_three.value));


    But this only works if you are loading and changing those fields on the workflow screen.

    If you are storing the values of case_one,case_two and case_three before you invoke the workflow rule, you have a couple of options to get the sum of them into case_fourth.

    Using javascript, I could add the following code to the create script of case_fourth:

    var one =CRM.GetContextInfo("case","case_one");
    var two = CRM.GetContextInfo("case","case_two");
    var three = CRM.GetContextInfo("case","case_three");
    defaultvalue = parseFloat(one)+parseFloat(two)+parseFloat(three);

    Using an execute SQL rule, you can pass the values in and have the sum of them written to the database:

    DECLARE @CaseId INT = #case_caseId#
    DECLARE @1 INT = (SELECT case_one FROM cases WHERE case_caseId = @CaseId)
    DECLARE @2 INT= (SELECT case_two FROM cases WHERE case_caseId = @CaseId)
    DECLARE @3 INT = (SELECT case_three FROM cases WHERE case_caseId = @CaseId)
    DECLARE @SUM INT = SUM(@1+@2+@3)

    UPDATE Cases
    SET case_fourth = @SUM
    WHERE case_caseId = @CaseId

    Didn't see Kieron's post as I was entering this, indeed a TLS could do this also.

  • 0

    Toby,

    The data for case_one, case_two, and case_three are all being calculated during the workflow based on numbers entered into other fields (case_five, case_six, case_seven). This may be my issue. Can it figure those and then find the total all in one workflow? I made certain to figure case_fourth after all other calculation based on workflow order.

    With that thought, I tried case_fourth.value = (parseFloat(case_five.value*100) + parseFloat(case_six.value*50) + parseFloat(case_seven.value*125));

    This did not work either. Am I just trying to do something that can't be done in the workflow? Maybe TLS would work best, but I am not certain what script I would use there either.

    Thank you for your help on this. I really appreciate it!

  • 0

    Hi Stephanie,

    I think I am following what you are doing, but just to run it by you:

    >>The data for case_one, case_two, and case_three are all being calculated during the workflow based on numbers entered into other fields (case_five, case_six, case_seven).

    So you have three fields:

    • case_five
    • case_six
    • case_seven

    where values are input manually, then you have:

    • case_one
    • case_two
    • case_three

    that are being populated by a calculation based on the values entered into case_five,case_six & case_seven.

    What you then want to do is write the total of all of these fields to another field, case_fourth.

    >>This may be my issue. Can it figure those and then find the total all in one workflow?


    This is the section I need to be clear on. Are you wanting to invoke one rule that would do all calculations, taking the data in:

    • case_five
    • case_six
    • case_seven

    Using that to calculate the values for:

    • case_one
    • case_two
    • case_three

    Then summing them all into case_fourth?

    Or are you calculating these at different steps?

  • 0

    You have it exactly right - I have the 3 fields that values are manually input, that then populate based on calculations already in place on the 3 input fields. This is working fine.

    What I then want to do is write the total of all of these fields to another field, case_fourth.

    I am not particular if these are all calculated in one script, but currently the first three are individual scripts executed OnChange for each case_five, case_six, and case_seven.

  • 0

    Cool,

    Well I think it is just making sure you have your values available to you.

    If:

    • case_one
    • case_two
    • case_three
    • case_five
    • case_six
    • case_seven

    All have their values stored in the database already, you can do this via code on a new rule, that sets case_fourth.

    I have mimicked this on my system:

    So I have values in all the fields except case_fourth, and I want case_fourth to be the sum of all the fields.

    In the workflow rule, add case_fourth, and add the following code to the 'CreateScript':

    var one = CRM.GetContextInfo("cases","case_one");
    var two = CRM.GetContextInfo("cases","case_two");
    var three = CRM.GetContextInfo("cases","case_three");
    var five = CRM.GetContextInfo("cases","case_five");
    var six = CRM.GetContextInfo("cases","case_six");
    var seven = CRM.GetContextInfo("cases","case_seven");

    var Pone = parseFloat(one);
    var Ptwo = parseFloat(two);
    var Pthree = parseFloat(three);
    var Pfive = parseFloat(five);
    var Psix = parseFloat(six);
    var Pteven = parseFloat(seven);

    defaultvalue = (Pone+Ptwo+Pthree+Pfive+Psix+Pteven);


    This will get all of the values, as they are already in the database and add them together, so that when the workflow rule is invoked, the value of case_fourth is the sum of them all:

  • 0

    Toby,

    Thank you for your help. I am still struggling with this, but it will have to take a back burner for a week, until I have more time to figure it out.

    I very much appreciate your time and assistance. I will get it eventually. :)

    Have a wonderful day!