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

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...


Thanks Shiladity,



I had seen something similar to what you shown me here for the calculation script for Gamma.. but did not implement as I was unsure. Tried your top option and it worked just fine!



thank you!


-Rob


Hey Shiladity,



Not sure if we can go a little more from what I have shown you to what I needed, and what you have given me. If I want to change all of this to a currency value. Can I still use the calculated condition? So for example, i changed the value of the colors and material, and gave them dollar value. what more would I need to do or change in order to have the GAMMA (SUM) show the dollar value? As of now after changing the values, GAMMA does show the dollar value before the decimal. So if I have Red as 100.25 and Leather as 100.25.. it only shows 200 rather than 200.50...



are you able to speak to this on how to change?



Thanks in advance!


-Rob


adityaghosh
Tera Guru

if the variable holds the currency symbol like $ then youll have to remove it like e.g.



var t1 = g_form.getValue('var1'); var1 => $100.00


t1 = t1.replace('$',''); => t1=> 100.00



now use parseFloat and fillany append the $


e.g.


t1 = '$' + t1;



e.g.



var number1 = '12,000.00';
var number2 = '12,000.00';

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

alert
( parseCurrency(number1) + parseCurrency(number2) );



hope it helps...