I have put a boolean flag on the Ship to Address table called Principal Address
The business logic is that there can only be one Principal Address per customer account so when a user places a tick in tnis field the previous flag is set to false.
This is my code which does not work! Any help would be appreciated..
Principal Address - OnValidate()
recShipToAddress.RESET;
recShipToAddress.SETRANGE(recShipToAddress."Customer No.","Customer No.");
recShipToAddress.SETFILTER(recShipToAddress.Code,'<>%1',Code);
IF recShipToAddress.FINDSET THEN BEGIN
recShipToAddress.MODIFYALL(recShipToAddress."Principal Address",FALSE);
END;
0
Comments
At the moment, the code is also run, if the user unchecks the field.
I'm assuming, if you want to set a ship-to as primary and another ship-to is already the primary,
that you want your code to check the new account and go thru the others unchecking it?
is this what you're tring to do?
what's the point on filtering the code?
recShipToAddress.SETFILTER(recShipToAddress.Code,'<>%1',Code);
http://www.BiloBeauty.com
http://www.autismspeaks.org
that would give you a message and which code it finds.
"Primary Address Already exists for this customer: Code %1\
Do you want to change it?"
We have an account with 215 ship-to's, if we did do something like this I would like to know which one is changing.
http://www.BiloBeauty.com
http://www.autismspeaks.org
This is exactly what I am trying to do!
This does not work I am afraid! Thanks for trying
This is to exclude the record that I have checked from being unchecked.
The code of Luc is ok so there is something strange happening.
Do you have accidentally added a local variable called "Customer No."?
What happens if you add a message like this:
Object Manager
The message displays the appropriate Customer No and Code for the record selected!
I have prototyped Lucs code on a V5SP1 Cronus demo database. There are no local variables. I have placed the code on the on validate trigger.
Still does not work!!!
//To prevent execution when you validate TRUE on one that was already TRUE, I check the previous value as well
//Prevents extra updates from being sent to the database
IF (BooleanField) AND (NOT xrec.BooleanField) THEN BEGIN
localRec.SETRANGE("Customer No.", rec."Customer No."); //Filter 1 to get only this customer's addresses
localRec.SEFILTER(Code, '<>%1', rec.Code); //Filter 2 to eliminate this record that was just checked
localRec.SETRANGE(BooleanField, TRUE); //Filter 3, to find the one record that is already checked
localRec.MODIFYALL(BooleanField, FALSE);
//Or use this code, as there should only be one other box checked anyway so no need for a repeat and changing a value
//that has been filtered on (throws off the NEXT)
//IF localRec.FINDFIRST THEN BEGIN
// localRec.BooleanField := FALSE;
// localRec.MODIFY;
//END;
END;
My Blog - nav.education
When the code executes there are still other ship to records for this customer that are still true! I am executing the code at table level on the on validate trigger of the Primary Address field.
A big thanks to Mattrax
Here is my final code..
This code is executed on the on validate trigger of my custom "Principal Address" Field
IF ("Principal Address") AND (NOT xRec."Principal Address") THEN BEGIN
recShipToAddress.RESET;
recShipToAddress.SETRANGE(recShipToAddress."Customer No.","Customer No.");
recShipToAddress.SETFILTER(recShipToAddress.Code,'<>%1',Code);
recShipToAddress.SETRANGE(recShipToAddress."Principal Address",TRUE);
IF recShipToAddress.FINDSET THEN BEGIN
recShipToAddress.MODIFYALL(recShipToAddress."Principal Address",FALSE);
END;
END;
I added this field to the Ship-To Address List form and added..
Principal Address - OnAfterValidate()
CurrForm.UPDATE;
recShipToAddress.RESET;
recShipToAddress.SETRANGE("Customer No.","Customer No.");
recShipToAddress.SETFILTER(Code,'<>%1',Code);
recShipToAddress.SETRANGE("Principal Address",TRUE);
if recShipToAddress.findfirst then begin
recShipToAddress.locktable; //to say the sql server, now we will modify the rec
recShipToAddress."Principal Address" := FALSE;
recShipToAddress.modify;
end;
if you really want to use the modifyall statement, the use this:
recShipToAddress.RESET;
recShipToAddress.SETRANGE("Customer No.","Customer No.");
recShipToAddress.SETFILTER(Code,'<>%1',Code);
recShipToAddress.SETRANGE("Principal Address",TRUE);
if not recShipToAddress.isempty then
recShipToAddress.modifyall("Principal Address",FALSE);
Here a link to the blog of WALDO with a nice article.
http://dynamicsuser.net/blogs/waldo/arc ... teall.aspx
Regards
But there is still another problem, if one copies a principal adress, one will get two of them... - think about it...
I forgot to set a filter on "Principal Address". This code should work: