Need help with rule that returns lowest and highest numbers

Recommended Posts

Hello,

Not sure if this is the correct place to ask this. Just wondering if someone could help me out with creating a rule that will return the lowest and highest numbers.

I am working on a project that has 9 fields for pricing. Each field can range from \$0 (or no number at all) to \$100. I need to create a rule that will populate the lowest number (which cannot be 0) and highest number into 2 different fields called min price and max price. Any help would be greatly appreciated.

Thank you,

Sean

Share on other sites

```// create array of all price fields
var prices = [price1,price2,price3,price4,price5,price6,price7,price8,price9];
// strip dollar sign if present
for (var d=0; d<prices.length; d++) {
prices[d] = prices[d].replace("\$","");
prices[d] = StringToNumber(prices[d]);
}
// sort lowest to highest
prices.sort(function (a,b) {return a-b;});
// choose lowest value greater than zero
for (var c=0; c<prices.length; c++) {
if (prices[c] && prices[c] > 0) {
var minPrice = prices[c];
break;
}
}
// choose highest value
var maxPrice = prices[prices.length-1];
// return values with preferred formatting
return "low = \$" + minPrice + " and high = \$" + maxPrice;```

I need to create a rule that will populate the lowest number (which cannot be 0) and highest number into 2 different fields called min price and max price.

If you need to use minPrice and maxPrice in separate fields, you could place the above code (minus the return) into an OnRecordStart callback rule and drop the "var " preceding those two values (to make them global). Then you could refer to them in separate text rules for placement on your template.

Edited by esmith
clarification
Share on other sites

Or, more succinctly (before Mr Korn can embarrass me):

```var prices = [price1,price2,price3,price4,price5,price6,price7,price8,price9].map(function(i){return StringToNumber(i.replace("\$",""));}).filter(function(n){return n>0;}).sort(function(a,b){return a-b});
var minPrice = FormatNumber("\$0.00", prices[0]);
var maxPrice = FormatNumber("\$0.00", prices[prices.length-1]);
return "low = " + minPrice + " and high = " + maxPrice;```

Share on other sites

Hi Eric,

This is much appreciated and exactly what i needed. And again thank you very much for your help.

- Sean

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×

• FusionPro.com

• MarcomCentral
×
• Create New...