Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Suggested answer

Re: SetFilter for data type text needs to be converted to Date.

(1) ShareShare
ReportReport
Posted on by 4
Hi,
I used SetFilter for this code to convert a data type from Text to Date. I have tried the methods below

For Examples:
 
trigger OnAfterAfterGetRecord()
var
 glentry: Record "G/L Entry";

Begin
if GLDateFilter2 <> '' then begin
  glentry.SetFilter("Posting Date", '<%1', GLDateFilter2);
 
End;
 
This is a global variable for
GLDateFilter2  Data Type Text 
 
This field is showing an error (GLDateFilter2 : This is the Error Message  cannot convert from 'Text' to the type of Argument 1 'Date') 
 
 
Thank you 
  • Suggested answer
    Mohamed Amine Mahmoudi Profile Picture
    Mohamed Amine Mahmoudi 9,818 Super User 2024 Season 2 on at
    Re: SetFilter for data type text needs to be converted to Date.
    Hi,
     
    You must use Evaluate method to convert text to date.
     
    for e.g.
    Procedure Text2Date(DateTxt: Text): Date
    var
       Date: MyDate;
    Begin
       Evaluate(myDate, DateTxt);
    End;
    BR,
    Mohamed Amine MAHMOUDI
  • Suggested answer
    Saif Ali Sabri Profile Picture
    Saif Ali Sabri 370 on at
    Re: SetFilter for data type text needs to be converted to Date.

    My response was crafted with AI assistance, tailored to provide detailed and actionable guidance for your query.

    The issue arises because the SetFilter function expects a Date type for the "Posting Date" field, but you're providing a Text value (GLDateFilter2). To resolve this, you need to explicitly convert the Text value to a Date using the Evaluate function, which converts text to the appropriate data type if the format is valid. 

    Updated Code:

    al 
    trigger OnAfterAfterGetRecord()
    var
    glentry: Record "G/L Entry";
    FilterDate: Date; // Temporary variable to hold the converted Date value
    begin
    if GLDateFilter2 <> '' then begin
    // Convert Text to Date
    if Evaluate(FilterDate, GLDateFilter2) then begin
    // Apply the filter using the converted Date
    glentry.SetFilter("Posting Date", '<%1', FilterDate);
    end else
    Error('Invalid date format. Please provide a valid date.');
    end;
    end;

    Explanation:

    1. Evaluate Function:

      • Evaluate(FilterDate, GLDateFilter2) attempts to convert the Text value (GLDateFilter2) into a Date value (FilterDate).
      • If the conversion is successful, Evaluate returns true, and you can safely use FilterDate in the SetFilter function.
      • If the conversion fails (e.g., if the text is not in a valid date format), Evaluate returns false.
    2. Error Handling:

      • If the conversion fails, an error message is shown ('Invalid date format. Please provide a valid date.').
      • This ensures the user is aware of the issue and can correct their input.
    3. Using the Converted Date:

      • Once the Text value is successfully converted into a Date, it is used in the SetFilter function to filter the "Posting Date" field.

    Additional Notes:

    • Ensure that GLDateFilter2 contains a valid date format that aligns with the user's regional settings or your application logic. For example, if the user enters 10-12-2024, it must conform to the expected format (e.g., MM-DD-YYYY or DD-MM-YYYY).
    • If there’s a chance of mixed formats, you might need to validate the format before attempting the conversion.

    With this approach, your code will handle text-to-date conversion dynamically and apply the filter correctly.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,391 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,445 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans