What makes this loop code went wrong?!

jemmy
Member Posts: 247
Folks,
I just passed my C/Side Intro exam.
Need to go further C/Side solution dev.
The loop only occurs once, which part of this code goes wrong?
Please advice, and thanks as always.
Jemmy
I just passed my C/Side Intro exam.

Need to go further C/Side solution dev.
Customer.SETCURRENTKEY ("Credit Limit (LCY)"); Customer.SETFILTER ("Credit Limit (LCY)", '=0'); Customer.FIND ('-'); REPEAT Customer.CALCFIELDS (Balance); IF Customer.Balance <> 0 THEN Customer."Credit Limit (LCY)" := Customer.Balance ELSE Customer.”Credit Limit (LCY)" := 10000; Customer.modify; UNTIL Customer.NEXT = 0;
The loop only occurs once, which part of this code goes wrong?
Please advice, and thanks as always.
Jemmy
0
Comments
-
Do your modifications on a different Customer record variable, as the modified Customer in your example no longer matches the filter criteria after modification:
Customer.CALCFIELDS (Balance); Customer2 := Customer; IF Customer.Balance <> 0 THEN Customer2."Credit Limit (LCY)" := Customer.Balance ELSE Customer2.”Credit Limit (LCY)" := 10000; Customer2.modify;
Kind regards,
Jan Hoek
Product Developer
Mprise Products B.V.0 -
Customer.SETCURRENTKEY ("Credit Limit (LCY)"); Customer.SETFILTER ("Credit Limit (LCY)", '=0'); WHILE Customer.FIND ('-') DO BEGIN Customer.CALCFIELDS (Balance); IF Customer.Balance <> 0 THEN Customer."Credit Limit (LCY)" := Customer.Balance ELSE Customer.”Credit Limit (LCY)" := 10000; Customer.modify; END;
Stamen Delikostov
Navision Solution Developer
Intelligent Systems Bulgaria0 -
Strange! :shock:
Where are the other posted message from Steve0?
To s.delikostov, thank you for your enlighments!WHILE Customer.FIND ('-')
Jemmy0 -
It always take first record in the filter.
When you modify Credit Limit with value different from 0, this record will be excluded form the filter set.Stamen Delikostov
Navision Solution Developer
Intelligent Systems Bulgaria0 -
If you wish to pass the C/Side solution dev exam as well you should stop doing like this:
Customer.SETFILTER ("Credit Limit (LCY)", '=0');
Only use SETFILTER when you wish to set a filter that can't be set using SETRANGE. :-)Regards
Peter0 -
pdj wrote:If you wish to pass the C/Side solution dev exam as well you should stop doing like this:
Customer.SETFILTER ("Credit Limit (LCY)", '=0');
Only use SETFILTER when you wish to set a filter that can't be set using SETRANGE. :-)0 -
There's nothing wrong with that filter, why should anyone stop filtering that way?
Because the style guide says so (even some of the standard code was reworked in recent versions to comply to these guidelines, Denmark gets it wrong sometimes too) :?
SETRANGE appears to me to be a little bit faster than SETFILTER, noticeably on non-key fields in tables with millions of records .Kai Kowalewski0 -
Kowa wrote:Because the style guide says so
Secondly I have seen a lot of developers setting filters like this: SETFILTER("Some Field","Some Value") and this causes strange effects when "Some Value" suddenly contains &, * or |. So I always say that you should be careful using SETFILTER and only use it when it can't be done using SETRANGE.
I even do like this: SETRANGE("Some Date Field",TODAY,31129999D) eventhough I know that's not common in the standard application.Regards
Peter0 -
There is another point which I hate to bring up, but it is necessary...
I always use a constant value while filtering "Some Field"... I haven't written a plenty of applications, but I always use "constant" in the "Some Value".
I realized that SETFILTER is not the easiest way in Navision to get along with... yeah better SETRANGE takes a higher priority rather than SETFILTER. (I also don't know their speed)
Jemmy0 -
Correct codes:
Customer.SETCURRENTKEY ("Credit Limit (LCY)"); Customer.SETRANGE("Credit Limit (LCY)", 0); if Customer.FIND ('-') then repeat Customer.CALCFIELDS (Balance); OldLimit := Customer."Credit Limit (LCY)"; IF Customer.Balance <> 0 THEN Customer."Credit Limit (LCY)" := Customer.Balance ELSE Customer.”Credit Limit (LCY)" := 10000; Customer.modify; Customer.”Credit Limit (LCY)" := OldLimit; //return old value without modify until Customer.NEXT=0;
Customer.SETCURRENTKEY ("Credit Limit (LCY)"); Customer.SETRANGE("Credit Limit (LCY)", 0); if Customer.FIND ('-') then repeat Customer.CALCFIELDS (Balance); Customer2 := Customer; IF Customer.Balance <> 0 THEN Customer2."Credit Limit (LCY)" := Customer.Balance ELSE Customer2.”Credit Limit (LCY)" := 10000; Customer2.modify; modify until Customer.NEXT=0;
Other code are not "clean"...0 -
pdj wrote:Kowa wrote:Because the style guide says so
Secondly I have seen a lot of developers setting filters like this: SETFILTER("Some Field","Some Value") and this causes strange effects when "Some Value" suddenly contains &, * or |. So I always say that you should be careful using SETFILTER and only use it when it can't be done using SETRANGE.
I even do like this: SETRANGE("Some Date Field",TODAY,31129999D) eventhough I know that's not common in the standard application.
SETFILTER("Some Field","Some Value") is perfectly legitimate, only you should do it like SETFILTER("Some Field",'=%1',"Some Value"), and use the FORMAT statement as it is appropriate. That way you could also do SETFILTER("Some Field",'=*%1*',"Some Value"), and your wildcards are taken care of right there.
I don't like the use of hard coded artificial end dates. If I need to set a filter on a date I use SETFILTER("Some Date Field",'>=%1',"Some Date Value"). The speed of the query depends on you setting the right key with SETCURRENTKEY.0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions