Scripting / Inventory Control Subtract
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
The second Inventory Script, subtracts inventory as you sell each product from an invoice. But before we start notice that I placed a value of 100 on each product so it's easy to test our script. So if we go through here you'll see that we have 100 Inventory on every single record. That's because when we run our Inventory Subtract, or inventory remove script when we print an invoice we want to be able to easily check that it worked and this will help us do that. So we're going to go over to Invoices and we're going to, every time we click on this Print Invoice, or only the first time we click on it for a particular invoice record we're going to go a head and remove, in this case, three Colombian and two Mountain Mist. So let's go into ScriptMaker, let's make a brand New Script. We're going to call it Inventory, we'll call it Remove to differentiate it from the Add Subtract one, and what we're going to do is we're going to say, If is empty, we're going to go up to Date Printed; now we don't have the Date Printed field. This is the field that's going to allow us to determine if we've already subtracted from our Inventory. So we're going to make Date underscore printed, that'll be a Date field and it'll be blank until we run the script, we'll see how we populate it, so but if we haven't printed from it, then we'll have a blank and it'll be empty. So we'll come in here and double-click on Date Printed, close parentheses, click OK. If it's empty then what we want to do is Set field, and we're going to set our Date Printed field to Get current date. Simple as that. So once we've run through it once then it will never get into this IF statement again because we'll have the current date in there. It only gets through this part of the script once because we're going to add the script into our Print Script. So this is our failsafe method. It gets in once, never gets in again because it's going to set that field. The next thing we're going to do is Freeze the Window because what we're going to do after this is go into a Loop. We want to loop through all of our records and subtract from inventory. Now remember, if we add this script, and we'll move this in, we'll come in here and look at Ð still called New Script because we haven't saved it Ð but if we look at our Print Invoice and edit that, we're going to enter it right here, Perform script, and we'll even choose it right now, New Script. It's going to be right in there, and so imagine where we're going to be; we did this Go to related record, so we're in Line Items right now until we go back to the Original Layout, so we're going to be looping through the Line Items that we found, remember we did a Match current record only that made a Found set of all of the Line Items that were on that invoice, so we'll Save that. Come back here, go into our Loop, and now we have our Loop. Now we're going to do a Set Field. The first thing we're going to do is start on the Set Field. Remember, after we've done a Go to related record we're always on the first record, so we're going to Set Field, and that'll be on the first record, and what we're going to do is Set our Inventory field, that's in Products. So we're going to choose Inventory here. Now, remember, if we're inside of Lines, looping through all the Line items here, they can see the products, so we can go from Set Field. Set Field can look across this relationship just like a related field and say, OK, this Line item matches up with this Product. So that's how we can Specify this Inventory field even though our starting point, we're actually in the Line items on a layout based on, or showing records from Line items, that's how we can Specify, or cross-relationship, our products in our Inventory field. And it'll hit the right product every time because one Line item equals one Product. It's one Product that equals many Line items that we might have trouble with, but from Lines over the Products, no problem hitting that exact product and taking that quantity off. So we're going to say, take that, Products Ð what we have in it right now, so that's our Inventory Level, we'll double-click on that, and subtract from Lines our quantity. There we go, right there. Double-click on it, click OK. So, see that this field, our target, is referenced inside the calculation. That means that whatever's in Inventory will then be subtracted from, our quantity will be subtracted from it, and then put over what's in there, so we're not going to replace it with just the Lines quantity, we're going to replace it with the subtraction of these so, and so that will just reduce it by two or three or however many of those we sold. And then we're going to go with Go to record request, and choose Next. And very importantly check Exit after last. That means Exit the Loop after the last record. So if we have three Line items, in other words our Go to related record found three records, and we go to the first one, the second one Ð as soon as we get to the third one and then once we've processed it and then try to go to the fourth one we go, Oop. There's none there. So I'm going to exit the loop. It's like Exit Loop If but it's based on what records are on, so it allows you to walk through all those records and exit after the last one. So, this will loop through all the records setting each one of the products to the correct Inventory Level and then End If. So we can close that, Save it, go back to our Print Invoice, we'll uncheck that actually, go back here and we can look in here, Perform the Script Inventory Remove. If it goes in there it may not actually do anything. It may hit that IF statement and just come right back, but if it doesn't have a Date Printed it'll actually go ahead and do it. So let's try it out. Remember we have Colombian, 3, Mountain Mist, 2. So we have to hit Print Invoice, Continue. I don't have to actually print it; it's going to actually do that for me right now, subtract those values, and we come back over here and walk through these records and see if we can find it. Let's go back to the first record. I don't know exactly where there Ð there's Colombian, it's down to 97, and let's see if we can find the Mountain Mist. It's probably the last record, and that's down to 98. So let's go back to Invoices and let's print that exact same invoice again. This time it will not Subtract from Inventory. You can see Mountain Mist is still at 98 and if we go back and find the other one, there's the 97, they haven't subtracted. So it only does it once because of that Date Printed field. So using a script to subtract from an inventory is much better. It takes all that processing that you have to do every single time if you used a Sum Function in a Calculation field and puts it down and spreads it out across all the invoices, and so once the value's written in here it's stored. A Calculation field will be unstored, meaning every time you switch records you'd have to recalculate. It would make navigating really painful through here, so it's much better to use a script in this particular situation just because you get a ton of records and you want to do the processing once, not over and over and over again every time you want to view that data.
Tutorial Information
| Course: | FileMaker Pro 10: Intermediate |
| Author: | John Mark Osborne |
| SKU: | 33926 |
| ISBN: | 1-935320-19-X |
| Release Date: | 2009-01-05 |
| Duration: | 15 hrs / 177 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 