Get Contact with highest No.of Matching Strings?

navvy
Member Posts: 79
Hi,
In the table 5085 "Contact Duplicate" I try to get the Contact which has:
1. a specified Contact No
2. the highest No. of Matching Strings
Example: Table Contact Duplicate
I want to get the Contact "44444444" which has the highest No. of Matching Strings (with the value "2" in this example).
First, I have to set the "Contact No." - filter on the ""Contact Duplicate" record:
In the table 5085 "Contact Duplicate" I try to get the Contact which has:
1. a specified Contact No
2. the highest No. of Matching Strings
Example: Table Contact Duplicate
I want to get the Contact "44444444" which has the highest No. of Matching Strings (with the value "2" in this example).
First, I have to set the "Contact No." - filter on the ""Contact Duplicate" record:
Duplicates.SETFILTER(Duplicates."Contact No.", '44444444');And then, how can I now set an additional filter to get the appropriate record with the highest "No. of Matching Strings" ? #-o
navvy
Freelance Developer
Freelance Developer
0
Answers
-
There are a number of ways you could do this. This is just one:
Add a new key to the Contact Duplicate table:
No. of Matching Strings
Then,ContactDuplicate.SETRANGE("Contact No.",'44444444'); ContactDuplicate.SETCURRENTKEY("No. of Matching Strings"); IF ContactDuplicate.FINDLAST THEN .. ..
Of course, you need to bear in mind that there could be more than one record with the same number of matching strings, so you will need to decide how you want to handle that.0 -
you have two options.
first: Make an new key in the table with No. of Matching Strings and than use setcurrentkey("No. of Matching Strings")
second: You create in your function an loop and go throw the records in filter and select there the rec with the highest No. of Matching Strings
But it's possible, that you have more than one record with the same No. of Matching Strings,
RegardsDo you make it right, it works too!0 -
thanks for the fast response
I've tried it with the following code...Duplicates.SETRANGE("Contact No.", '44444444'); Duplicates.SETCURRENTKEY(Duplicates."No. of Matching Strings"); IF Duplicates.FINDLAST THEN BEGIN
... and received the following error-message: Message :-knavvy
Freelance Developer0 -
ok, that means I have to add a new field (key) to the table "Contact Duplicate" (open the table over the object designer and change) ?
In this case, I can't use this solution (can't change the table) ....navvy
Freelance Developer0 -
navvy wrote:ok, that means I have to add a new field (key) to the table "Contact Duplicate" (open the table over the object designer and change) ?
In this case, I can't use this solution (can't change the table) ....
You have a developers license, no??0 -
I have a developer license, but that's not the problem: I can't change any tables, because it should not be necessary to make changes to NAV to run the codeunit (which includes the search contact duplicate procedure).navvy
Freelance Developer0 -
navvy wrote:I have a developer license, but that's not the problem: I can't change any tables, because it should not be necessary to make changes to NAV to run the codeunit (which includes the search contact duplicate procedure).
Ok, then you will need to go for a solution using a loop.
Something along these lines (code untested):ContactDuplicate.SETRANGE("Contact No.",'44444444'); IF ContactDuplicate.FINDFIRST THEN REPEAT IF ContactDuplicate."No. of Matching Strings" > MaxNoOfMatchingStrings THEN MaxNoOfMatchingStrings := ContactDuplicate."No. of Matching Strings"; UNTIL ContactDuplicate.NEXT = 0; ContactDuplicate.SETRANGE("No. of Matching Strings",MaxNoOfMatchingStrings); IF ContactDuplicate.FINDFIRST THEN BEGIN .. .. .. END;
The first section determines what the maximum No. of Matching Strings value is.
The second section sets a range on this value (remember there could be more than one record).
Hope this helps0 -
I would go for something like:
ContactDuplicate.SETRANGE("Contact No.",'44444444'); IF ContactDuplicate.FINDFIRST THEN BEGIN rectmpMaxContactDuplicate := ContactDuplicate; REPEAT IF ContactDuplicate."No. of Matching Strings" > rectmpMaxContactDuplicate."No. of Matching Strings" THEN rectmpMaxContactDuplicate := ContactDuplicate; UNTIL ContactDuplicate.NEXT = 0; END;
After the loop, you've got your record in the temp variable.
This way, you avoid to send a query with a filtering on a field you don't have an index on... . And it's less traffic.
Just my two cents... too bad you're not allowed to create a key on the field. ](*,)0 -
Waldo wrote:I would go for something like:
ContactDuplicate.SETRANGE("Contact No.",'44444444'); IF ContactDuplicate.FINDFIRST THEN BEGIN rectmpMaxContactDuplicate := ContactDuplicate; REPEAT IF ContactDuplicate."No. of Matching Strings" > rectmpMaxContactDuplicate."No. of Matching Strings" THEN rectmpMaxContactDuplicate := ContactDuplicate; UNTIL ContactDuplicate.NEXT = 0; END;
After the loop, you've got your record in the temp variable.
This way, you avoid to send a query with a filtering on a field you don't have an index on... . And it's less traffic.
Just my two cents... too bad you're not allowed to create a key on the field. ](*,)
Nicebut.... this will only ever return 1 record.
There could be >1 record with the same No. of matching Strings
This is why you need to find out what the maximum number of matching strings is before you do anything else.
@ Navvy - Adding a key to the table is the lowest impact way to go though.0 -
-
Waldo wrote:I though he only neede one :-k (cfr. his FINDLAST statement).
Furthermore, filling a temp table in background is also a way to go. If you filter then on the "No of Matching Strings", it will be done on the client, not on the server... .
If he only want the first or last then this method is ok.
The fact remains, there is still the possibility of more than one record with the same No. of Matching Strings. This being the case, without using the additional key on the table, you have to find the maximum value somehow before you can do anything else.
I think a little more info on the exact requirements may be needed
[/i]0 -
Hehe, 14 relies for this little questionDo you make it right, it works too!0
-
Thanks once again for all the great replies
I need only 1 record, so this solution will be perfect:ContactDuplicate.SETRANGE("Contact No.",'44444444'); IF ContactDuplicate.FINDFIRST THEN BEGIN rectmpMaxContactDuplicate := ContactDuplicate; REPEAT IF ContactDuplicate."No. of Matching Strings" > rectmpMaxContactDuplicate."No. of Matching Strings" THEN rectmpMaxContactDuplicate := ContactDuplicate; UNTIL ContactDuplicate.NEXT = 0; END;
Revolution1210 wrote:I think a little more info on the exact requirements may be needed
I'm developing an external add-on for NAV (screenshot.jpg). If there are more than one record (= possible contact duplicates), the user has to go into NAV and find the correct contact.
If the user doesn't go to NAV and search the correct contact manually, or this process is automated, NAV should take the contact with the highest "No. of Matching Strings".
I hope, this information helps you to understand my situation 8)navvy
Freelance Developer0
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