In order to keep the post a decent length I won’t be reiterating the content in Part 1, so if you found yourself here please read it first!
Demo
What’s different?
Counting object with requirements is different than Part 1 in many ways, most notably:
- The Object and Object Array variables now have a new attribute,
Requirements met?
- A new table that holds the requirements, and logic to check against the requirements.
- A new simple array to track if all requirements have been met.
This requires just over double the number of triggers to accomplish:
Note: It may be technically possible to reduce the number of triggers by one (maybe 2?) but I intentionally did it this way not to obfuscate the logic.
The Nitty-Gritty Details
New or Modified Variables
This variable is used to “collect” the Boolean Yes | No
values from the Record Object Array
.
This variable is used to know the total quantity of a particular animal including both the count of ones already added and the count of the new request so we can know if the new request would exceed the required quantity or not.
This variable counts the total number of items that have been added to the Record Object Array
.
As mentioned previously, this object variable has gained a new attribute, Requirements met?
.
Putting it all together
Now let’s break down each trigger:
Load record if available
Here we check to see if the requested item is in the table, and if so, load the record. I do have an Else If
(not pictured) to just show an error message and transition back to the same step if the item isn’t found.
Check requested quantity
This trigger is just a high level check to make sure the requested quantity is not more than the required quantity.
Check for Existing Items
I cheated the screenshot a bit to get it in one but once again, we can only check to see if an array is empty so the first If
clause is a no-op
if that’s the case. Otherwise we pull all the items from the existing Record Object Array
and store them in a simple array to be used in a subsequent trigger.
Check if item exists & calculate total
In the first half of this trigger we check to see if the item being requested has already been added, if so we pull the object out of the array so it can be modified and calculate the new quantity for that item.
Otherwise we treat it as a new item and prep a new object to be pushed onto the object array.
Note: The last part increments the Count of Items
variable which is used to know if all of the required items have been added. If you’re on Release 277 or higher you can get the length of the array instead.
Check requirements and merge
Here we check to see if the requested item has already met the requirements, and if so we show an error, push the Working Object
back on to the Record Object Array
and exit the trigger sequence by going back to the step.
Now we check to see if if the total quantity exceeds the required quantity and if so do the same as the previous conditional.
If the current quantity meets requirements then we change Requirements met?
to Yes
and push the object onto the array.
Lastly, if none of the previous are true, then it means we can add the request to the object array but it has not met the requirements.
Check all requirements met
Almost there I promise! This trigger simply copies the Requirements met?
attribute of each object into a simple array so we can perform the final check in the next trigger.
All requirements met?
Remember that variable Count of Items
I mentioned earlier? Here’s when we use it. It’s possible and even likely that the All Requirements Array
may only contain Yes
and yet we’re still missing item requirements.
This first check it to see if either of the requirements fail.
If neither of the previous checks fail then total item count and item quantities must match the requirements.