web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

How to select between two fields without overlapping

(0) ShareShare
ReportReport
Posted on by 454

Hi,

I have one Table which is having two fields "From" and "To". I need to select between these fields. If i enter a new value in the table , if the entered value in the "From" overlaps between the previously entered values. It should not accept the value to be inserted.

For eg:   The 1 to 1500 already inserted, if i try to enter 10 to 1000 it should not be inserted. Help me on this. TIA 

From  TO
1 1500
10 1000

Regards,

Ram

I have the same question (0)
  • Suggested answer
    ergun sahin Profile Picture
    8,826 Moderator on at

    If I am not wrong you want not only "from" but "to" do must not between these fields. Maybe something like this;

    Table   tableInsert, tableCheck;
    
    tableInsert.From    = 10;
    tableInsert.To      = 120;
    
    select firstOnly tableCheck
        where(tableCheck.From  >= tableInsert.From
           && tableCheck.To    <= tableInsert.From)
           ||(tableCheck.From  >= tableInsert.To
           && tableCheck.To    <= tableInsert.To);
           
    if(tableCheck)
    {
        error("..");
    }

  • Verified answer
    Martin Dráb Profile Picture
    237,882 Most Valuable Professional on at

    What exactly is the problem?

    You don't know how to validate records on save? If so, use validateWrite() method and return false if the check fails.

    Or you don't know how to compare values? You have operators like >, <, >= and <=.

    Or you don't know what logic you need to find conflicting records? What about something like this:

    select firstOnly RecId from myTable
    where (myTable.From < this.From && myTable.To > this.From)
       || (myTable.From > this.To && myTable.To < this.To);

  • Ram Kumar Profile Picture
    454 on at

    Hi Ergun,

    I have to compare between two fields in only one table.

    Regards,

    Ram

  • Martin Dráb Profile Picture
    237,882 Most Valuable Professional on at

    It's the same table. But you need two buffers - the one that you're saving and the other for finding existing records.

  • ergun sahin Profile Picture
    8,826 Moderator on at

    Dont you have two record (an old and a new).

  • Ram Kumar Profile Picture
    454 on at

    Thanks Martin, Ergun . I have confused with the buffers sorry. It works

    Regards,

    Ram

  • Ram Kumar Profile Picture
    454 on at

    Hi Ergun,

    I have one condition which is the To value should be greater than From Value. How it should be written.

    regards,

    Ram

  • Martin Dráb Profile Picture
    237,882 Most Valuable Professional on at

    If you look at code samples above, you can see how to compare fields. What else do you need? Please elaborate.

  • Suggested answer
    ergun sahin Profile Picture
    8,826 Moderator on at

    These codes check whether the fields are in range individually. It doesn't matter which field is big. If either of them is in range, the old record will return.

    If you want to add a new condition (from not greater than to), simply compare the new record.

    if (tableInsert.From> tableInsert.To)

    If you don't have such a request, the above codes will do the trick.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 565 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 450 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 250 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans