cancel
Showing results for 
Search instead for 
Did you mean: 

nil can't be coerced into Float when trying to add 3 searches together

johnw
Executive Chef I
Executive Chef I

is there an easier way to do this?

 

I have 3 companies with 5 warehouses each

so I do 3 searches t find all the costs in each warehouses and then just add the data pills together and this worked fine.

but then I started seeing the Nil can't be coerced error so some warehouses will be blank. 

do I need to loop through each search and do a if .present to add them together? or is there a trick I'm missing like in SFDC where there is a way to treat NULL as 0

 

also, is there a way to do a loop through 3 searches? or do I need to create a list variable which I'm trying to do but can't seem to combine the 3 searches into a single list

 

sorry if these are basic questions very new

1 ACCEPTED SOLUTION

gary1
Executive Chef II
Executive Chef II

There are a lot of ways to do this, but it depends on how your data is formatted. If you can make an array of all of the costs, you can use [array].compact.sum, where compact will eliminate the nil values. Otherwise, if you want to add them up individually, you will need to handle nil values with .present? like you mentioned. Something like:

([wh1].present? ? [wh1] : 0) + ([wh2].present? ? [wh2] : 0) ... and so on

For your second question, you can loop through three searches, but first you need an input to determine how many times to loop. Here's the general idea:

  • You have 3 companies, and I'm assuming each company has an ID. Put the IDs in an array.
  • Make a repeat helper list with [array].size (resolves to 3)
  • Use the repeat helper list in a repeat action to loop 3 times
  • Create your action to run your searches (which I assume requires a company ID)
  • Parameterize the company ID in the search action by using [array][loop_index], which will return the ID in the array that corresponds to the index of the current loop
  • Add the cost from the response to a list 
  • Outside of the loop, pluck the costs from the list and sum them

Hope this helps 

 

View solution in original post

18 REPLIES 18

johnw
Executive Chef I
Executive Chef I

I got it working but its NOT pretty so have 3 search actions 1 for each company pulling 5 Cost fields each.

Then I created I List variable and created 15 Update List variable actions to add those 15 fields to the list.

then I looped through the list variable checking for nil and adding to my original variable to ensure it was a float.

this is working but like I said not pretty I was hoping to pass the searches datapil directly into a variable list which i suspect can be done but I tried everything I could think of.

tonight, this will run in sandbox and I'm curious how long it will take to crunch 2500 or so parts  

 

gary1
Executive Chef II
Executive Chef II

Glad you made some progress. If you can provide some examples of how the response is formatted, I'm sure there are more elegant and efficient ways to do this.

gary1
Executive Chef II
Executive Chef II

There are a lot of ways to do this, but it depends on how your data is formatted. If you can make an array of all of the costs, you can use [array].compact.sum, where compact will eliminate the nil values. Otherwise, if you want to add them up individually, you will need to handle nil values with .present? like you mentioned. Something like:

([wh1].present? ? [wh1] : 0) + ([wh2].present? ? [wh2] : 0) ... and so on

For your second question, you can loop through three searches, but first you need an input to determine how many times to loop. Here's the general idea:

  • You have 3 companies, and I'm assuming each company has an ID. Put the IDs in an array.
  • Make a repeat helper list with [array].size (resolves to 3)
  • Use the repeat helper list in a repeat action to loop 3 times
  • Create your action to run your searches (which I assume requires a company ID)
  • Parameterize the company ID in the search action by using [array][loop_index], which will return the ID in the array that corresponds to the index of the current loop
  • Add the cost from the response to a list 
  • Outside of the loop, pluck the costs from the list and sum them

Hope this helps 

 

moxiego
Deputy Chef II
Deputy Chef II

Gary,

Can you explain/give an example of step #3

"Use the repeat helper list in a repeat action to loop 3 times"

I have a repeat helper list, how do I iterate through it. or update the sequence?

Thanks