Pulling My Hair Out

I have been trying to figure out a scenario that splits an order into two orders and divides the lineitems between the orders based on which line items are checked in the line item portal.

I have a portal on the form for the line items, when a box from lineitems table is checked, the field value = yes.

I want to send the checked items to the new order and keep the unchecked lineitmes with the original order.

I created the following code to create the split the order and line items, then loop through the line items per order and delete the appropriate line items based on the checked box value argument (‘Yes’ or Null).

The orders and line items are successfully split, but the line item deletions are inconsistent, and I can’t figure out the trend, it’s as if the “if” statement is not correctly evaluating. I guess it seems like an indexing issue possibly?

This method is run on a FID form.

// Capture Current PSF Number
globals.Text12 = product_sales_order_number;

// Save Checked Items to memory
var lineitems = psforders_to_psforderlineitems;

// Capture the number of records
var count = psforders_to_psforderlineitems.getSize()

//Duplicates and selects a new master record
controller.duplicateRecord();

//loop on previously saved detail records
for (var i = 1; i <= count; i++)
{
//Duplicates the line Items
lineitems.duplicateRecord(i,false,true);

//Assigns the new master primary
lineitems.product_sales_order_number = product_sales_order_number;
}
controller.saveData();

// Capture new PSF Number
globals.Text13 = product_sales_order_number;

// Return to old PSF
controller.find()
product_sales_order_number = globals.Text12;
controller.search()

//Delete Checked Items from original
for (var i = 1; i <= psforders_to_psforderlineitems.getSize(); i++)
{
elements.PSFOrderLineItems.setSelectedIndex(i)
if(psforders_to_psforderlineitems.split_order_check_box == ‘Yes’);
{
psforders_to_psforderlineitems.deleteRecord();
}
}

//Return To New Record
controller.find()
product_sales_order_number = globals.Text13;
controller.search()

//Delete Un-Checked Items from New Order
for (var i = 1; i <= psforders_to_psforderlineitems.getSize(); i++)
{
elements.PSFOrderLineItems.setSelectedIndex(i)
if(psforders_to_psforderlineitems.split_order_check_box == null);
{
psforders_to_psforderlineitems.deleteRecord();
}
}

If anyone sees anything obvious, I could use a push in the right direction, this is driving me nuts.

Thanks,
Erich

is the following line:

if(psforders_to_psforderlineitems.split_order_check_box == null);

really null? maybe it is empty?
you can try this:

if(psforders_to_psforderlineitems.split_order_check_box);

this checks empty AND null

Hope this helps.

Try else the debugger, and do step by step.

I would check this loop closely.
You are deleting records in the recordset that you are looping through,
which may cause your counter “i” to go out of sync

//Delete Checked Items from original
for (var i = 1; i <= psforders_to_psforderlineitems.getSize(); i++)
{
elements.PSFOrderLineItems.setSelectedIndex(i)
if(psforders_to_psforderlineitems.split_order_check_box == 'Yes');
{
psforders_to_psforderlineitems.deleteRecord();
}
}

Excellent, thank you both, I did use debugger but the deletions where not being evaluated correctly.
How can I set the index correctly if I am deleting records?

quick and dirty way: decrease “i” after each deletion;

{
psforders_to_psforderlineitems.deleteRecord();
i--;
}

You the man!

Thanks much, that was a huge help!!!

Ok, I have tried every index option I could thing of, via relationships, via elements. etc.. viewing in debugger, the index count is right.

for (var i = 1; i <= psforders_to_psforderlineitems.getSize(); i++)

{
elements.PSFOrderLineItems.setSelectedIndex(i);
if(psforders_to_psforderlineitems.split_order_check_box == ‘Yes’);
{
elements.PSFOrderLineItems.deleteRecord();
}
}

I did did not check any items, yet no matter how many Items I have in the portal, it deletes every other line starting at line 2.

Servoy Developer
Version R2 2.2-build 328
Java version 1.4.2_04-b05 (Windows XP)

Thanks for any help, I am sure I am missing something simple, but I have never had so much trouble with a simple loop.

Isn’t this your problem:

  • you go to record #1 and delete it
  • the cursor automatically jumps to the next record
  • then you go to record 2 by setting the index

this way you always skip one record.

I see several ways how you can do this:

  • create a relationship that sees all those records with split_order_check_box “Yes” and use deleteAllRecords (the safest, fastest and best ways in my eyes)
  • loop backwards (i–)
  • use a find in the form that shows those lineitems and delete the result

Hope this helps

Patrick

Patrick,

Thank you for your suggestions, those are some good options, I already have a global relationship set up for the checked items, I will go that route.

I guess I am just a bit confused as to why I can’t just loop through the records and delete certian ones based on an argument. I tried looping backwards

{
psforders_to_psforderlineitems.deleteRecord();
i–;
}

but this did not work either. I will just try your suggestion by deleting the global related records.

Thanks for your help.

The other thing that confused me was:

Isn’t this your problem:

  • you go to record #1 and delete it

None of the records should have been deleted, because the argument was false (I did not check any line items).

Anyway, Thanks again

I may be missing a trick here (not unusual) but notice that in maarten’s code he uses:

psforders_to_psforderlineitems.deleteRecord();

whereas in your code you appear to be using a Form element:

elements.PSFOrderLineItems.deleteRecord();

Could this be part of the problem.

Graham Greensall
Worxinfo Ltd

Yes I have tried using both relationships and elements as well. I just happen to post the last attempt, but did try it as he posted.

Thanks though

I guess I am just a bit confused as to why I can’t just loop through the records and delete certian ones based on an argument. I tried looping backwards

How about splitting the operation into two?

Loop through the records and just set a flag on the ones to be deleted - then after the loop finishes do a Search and Delete/Tansfer the flagged records.

Graham Greensall
Worxinfo Ltd

My 1.5 cents:

Why duplicate and delete the old? Why not set the new order key on the existing line items, and uncheck them? They would then “move” to the new order, wouldn’t they? Then you wouldn’t be deleting anything in your loop.

WARNING: This is a suggestion based on the kind of logic I learned in “FileMaker think”. I cannot suggest a single line of Servoy code on this topic at this time.

however, perhaps this logic will be helpful nonetheless, so I’ll go out on a limb here:

Create a compound relationship matching a global “yes” value to the checkbox in lineitems, as well as the order_id to lineitems.order_id. Use that relationship to delete all child records.

OK, stop laughing, was I way off?

kazar

well, I am still in Filemaker world as well, so it does not seem too cryptic. I wish I had more time to devote to Java, HTML and SQL. Anyway, thats what I am doing, but still working out some new issues. Thanks for the help though.

Hi eBrant, sounds to me that you need to do the following:

  1. You need a form based on your line items, you may want to check “Use separate found set”. You will be resposible for loading all of the line items of your order to split into this form.
  2. Create a method which takes one parameter, the id of the new order. Loop through the records of line items and set the new id for any ticked line items. (No deletions, copying or anything needed, just a simple loop setting the one field where needed).
  3. Create a method on your order form which loads the line items of your order into the form mentioned in 1) and fires the method 2). You may need to do SaveData() at the end of this method to make sure everything updates.

By the way, this is how I would do this in FileMaker too!
Hope this helps,