Sum of two fields populate in a third

Rob Sestito
Mega Sage

Hello SN Comm,

Looking for some scripting help (suggestions on which to use).

I was thinking either using the Calculated script within the third field OR I believe I have read that a Business Rule would be best.

Here is the story:

I need to create calculated fields to sum the scores into the two totals

For example sake this is what I have created so far within our Test Instance:

Drop-down field ALPHA: choices are:

  1. 1. "Red — 1 point"
  2. 2. "Blue — 3 points"
  3. 3. "Green — 5 points"

Drop-down field BETA: choices are:

  1. 1. "Leather — 5 points"
  2. 2. "Vinyl — 2 points"

  "Nylon — 1 point"

Calculated field GAMMA:   Sum the point values of the two fields.   If I pick Green Nylon, the result is 6

I have created a quick script however all it does is bring the two value numbers assigned to each color and put them together. So if I pick Green and Nylon, Gamma shows 51.

here is my current script: mind you, this script I am using as a calculation from the Gamma field..

current.u_gamma = current.u_alpha + current.u_beta;

Any help on best way to do this (again I believe it probably should be a business rule) - and help with how the script should be.


Thanks in advance!!

-Rob

1 ACCEPTED SOLUTION

adityaghosh
Tera Guru

The reason for that is it takes its as string,


try:-


current.u_gamma = parseInt(current.u_alpha) + parseInt(current.u_beta);



you may try the if condition for various combinations




var val =0;


if (current.u_alpha == '1' && current.u_beta == '5' )


      val = 6


if (current.u_alpha == '1' && current.u_beta == '2' )


      val = 3;


:


:


& so on...


View solution in original post

13 REPLIES 13

Thanks for the reply Shiladitya,



However, I think I am blanking on how it should be scripted for the calculated script.



Currently I have:


current.u_gamma = parseInt(current.u_alpha) + parseInt(current.u_beta);



Would it be something like this:


function parseCurrency( num ) {
   
return parseFloat( num.replace( /,/g, '') );
}

alert
( parseCurrency(u_alpha) + parseCurrency(u_beta) );




I feel like I am completely wrong - able to help on how this should actually be written?



thanks,


-Rob


adityaghosh
Tera Guru

        current.u_gamma = parseInt(current.u_alpha) + parseInt(current.u_beta);


        alert(current.u_gamma);



        var number1 = current.u_alpha;


        var number2 = current.u_beta;




      function parseCurrency( num ) {


                  return parseFloat( num.replace( /,/g, '') );


                }



        alert( parseCurrency(number1) + parseCurrency(number2)


Thanks for your reply Shiladitya,



Unfortunately, that script is not displaying any value at all within the Gamma field.



Not sure what I can move around from what you provided to try and get to work properly.



Thanks,


-Rob


adityaghosh
Tera Guru

what value does



current.u_alpha and urrent.u_beta



have ?


I have tested the following code in scripts-background



and its working...



var number1 = '12,33.90';


var number2 = '7,687.06';


var number = parseFloat(number1.replace(',','')) + parseFloat(number2.replace(',',''))


gs.print( number.toFixed(2)) ; //two decimal places



//////////////////


result -> *** Script: 8920.96


if the var has & then replace it e.g. number1.replace('$','');


and later add $ e.g. number = '$'+number;