Test a value against a filter

jorgito
Member Posts: 115
Hi all.
How can I check if a value complies with a filter? I would really like to have a function like
TESTFILTER(Filter, Value) that returns true/false. For example,
TESTFILTER('CA*', 'CA.00234') would return true and
TESTFILTER('CA*', 'CB.00234') would return false.
I have implemented data security on the customers table using a table called User Filters. The table contains 2 fields: User ID and Filter. For example there is a line User ID: USER1, Filter: CA*. So, when USER1 posts a document or journal, the system checks if the specific user can post for the specified customer.
I have put the code in Codeunit 11 (Gen. Jnl.-Check Line). Whenever a journal line is checked, I check if the Customer is in agreement with the User Filter. The code is:
The code works well when the user posts journal lines.
But it doesn't work when I post a Sales Invoice. The Journal Line is created by the Sales Invoice on the fly, so it doesn't actually exist. So the ISEMPTY function ALWAYS returns false, even if the customer is within the specified filter.
Any ideas?
Thanx
Jorgito
How can I check if a value complies with a filter? I would really like to have a function like
TESTFILTER(Filter, Value) that returns true/false. For example,
TESTFILTER('CA*', 'CA.00234') would return true and
TESTFILTER('CA*', 'CB.00234') would return false.
I have implemented data security on the customers table using a table called User Filters. The table contains 2 fields: User ID and Filter. For example there is a line User ID: USER1, Filter: CA*. So, when USER1 posts a document or journal, the system checks if the specific user can post for the specified customer.
I have put the code in Codeunit 11 (Gen. Jnl.-Check Line). Whenever a journal line is checked, I check if the Customer is in agreement with the User Filter. The code is:
IF "Account Type" = "Account Type"::Customer THEN BEGIN GenJnlLine3.RESET; GenJnlLine3.SETRANGE("Journal Template Name", "Journal Template Name"); GenJnlLine3.SETRANGE("Journal Batch Name", "Journal Batch Name"); GenJnlLine3.SETRANGE("Line No.", "Line No."); IF UserFilter.GET(USERID) THEN BEGIN GenJnlLine3.SETFILTER("Account No.",UserFilter.Value); IF GenJnlLine3.ISEMPTY THEN FIELDERROR("Account No."); GenJnlLine3.SETRANGE("Account No."); END; END;
The code works well when the user posts journal lines.
But it doesn't work when I post a Sales Invoice. The Journal Line is created by the Sales Invoice on the fly, so it doesn't actually exist. So the ISEMPTY function ALWAYS returns false, even if the customer is within the specified filter.
Any ideas?
Thanx
Jorgito
0
Answers
-
If you are filtering against a customer, why are you looking at the gen. journal line. Shouldn't you look at the customer table instead?0
-
Your code should look something like this.
IF "Account Type" = "Account Type"::Customer THEN BEGIN IF UserFilter.GET(USERID) THEN BEGIN MyCustomer.FILTERGROUP(0); MyCustomer.SETFILTER("No.",UserFilter.Value); MyCustomer.FILTERGROUP(1); MyCustomer.SETRANGE("No.","Account No."); IF MyCustomer.ISEMPTY THEN FIELDERROR("Account No."); END; END;
0 -
In generally, you can do that by using some temporary table, insert the code there, set filter - if table is empty, result is false, if you see the record, condition is true... ;-)0
-
is empty is faster than get, or is get faster than isempty?0
-
ara3n wrote:is empty is faster than get, or is get faster than isempty?
It depends if you will use get if the table is not empty and how big is chance that some record will be there... ;-)0 -
I couldn't' understand what you meant. could you clarify?0
-
it means, that if you have something like:
if not Rec.IsEmpty then begin Rec.GET(...); ... end;
it is better to do the GET without IsEmpty, if probability that the record exists is high and do GET in 100% where in 80% the record is found. If probability that the table Is Empty is higher, it is better to do IsEmpty in 80% of cases and in rest to do IsEmpty + GET. For me the IsEmpty and Get is on same level of performance (may be IsEmpty is for me little "easier" for the engine). But it is not based on measure, just on my personal feeling...0 -
ara3n,
thanx for the solution. I never thought of it this way until now.0 -
kine wrote:it means, that if you have something like:
if not Rec.IsEmpty then begin Rec.GET(...); ... end;
it is better to do the GET without IsEmpty, if probability that the record exists is high and do GET in 100% where in 80% the record is found. If probability that the table Is Empty is higher, it is better to do IsEmpty in 80% of cases and in rest to do IsEmpty + GET. For me the IsEmpty and Get is on same level of performance (may be IsEmpty is for me little "easier" for the engine). But it is not based on measure, just on my personal feeling...
May be my two cents...
Can't we just say: if you need the data of the record, go for GET, if you don't need data, but you just have to know if any record exist ... go for ISEMPTY?
ISEMPTY + GET are two roundtrips to the server ... which I would avoid in any case. (this is also only my personal opinion)
Anyway, if I'm not mistaken, the SQL Statements are something like:
ISEMPTY: SELECT TOP NULL FROM Table
GET: SELECT * FROM Table
So in general, GET is slower.0 -
jorgito wrote:ara3n,
thanx for the solution. I never thought of it this way until now.
No problem.0 -
Waldo wrote:kine wrote:it means, that if you have something like:
if not Rec.IsEmpty then begin Rec.GET(...); ... end;
it is better to do the GET without IsEmpty, if probability that the record exists is high and do GET in 100% where in 80% the record is found. If probability that the table Is Empty is higher, it is better to do IsEmpty in 80% of cases and in rest to do IsEmpty + GET. For me the IsEmpty and Get is on same level of performance (may be IsEmpty is for me little "easier" for the engine). But it is not based on measure, just on my personal feeling...
May be my two cents...
Can't we just say: if you need the data of the record, go for GET, if you don't need data, but you just have to know if any record exist ... go for ISEMPTY?
ISEMPTY + GET are two roundtrips to the server ... which I would avoid in any case. (this is also only my personal opinion)
Anyway, if I'm not mistaken, the SQL Statements are something like:
ISEMPTY: SELECT TOP NULL FROM Table
GET: SELECT * FROM Table
So in general, GET is slower.
In this case there is no need for the get, because we don't need any info.
But it is a good idea if you are in a loop and you are checking mulitple records, is to insert them into a temp and do the filter on it.0 -
ara3n wrote:In this case there is no need for the get, because we don't need any info.
But it is a good idea if you are in a loop and you are checking mulitple records, is to insert them into a temp and do the filter on it.
When using temp tables you avoid unnecessary roundtrips to the server ... which is usely a good idea0 -
Waldo wrote:ara3n wrote:In this case there is no need for the get, because we don't need any info.
But it is a good idea if you are in a loop and you are checking mulitple records, is to insert them into a temp and do the filter on it.
When using temp tables you avoid unnecessary roundtrips to the server ... which is usely a good idea
If the number of records are in 1000's the records will be cached anyways so even when you do a get twice on a record it doesn't go the server.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