Displaying an email in mail preview pane by message ID

I want to find an email in the Mail app by its Message-ID and display it in the preview pane, but I haven’t been able to select the correct message.

It always selects the next message instead of the intended one. Why is this happening?

        tell application "Mail"
            activate
            set targetMessage to missing value
            set targetMailbox to missing value
            
            set MessageID to "id"
            
            repeat with anAccount in accounts
                repeat with aBox in mailboxes of anAccount
                    try
                        set msgList to (messages of aBox whose message id is MessageID)
                        if (count of msgList) > 0 then
                            set latestMessage to item 1 of msgList
                            repeat with msg in msgList
                        if date received of msg > date received of latestMessage then
                                    set latestMessage to msg
                                end if
                            end repeat
                            
                            set targetMessage to latestMessage
                            set targetMailbox to aBox
                            exit repeat
                        end if
                    end try
                    if targetMessage is not missing value then exit repeat
                end repeat
                if targetMessage is not missing value then exit repeat
            end repeat
            
            if targetMessage is not missing value then
                set viewerCount to count of message viewers
                if viewerCount > 0 then
                    set selected mailboxes of message viewer 1 to {targetMailbox}
                    delay 1
                    set selected messages of message viewer 1 to {targetMessage}
                    delay 1
                    activate
                    return "Found"
                else
                    set newViewer to make new viewer with properties {selected mailboxes:{targetMailbox}}
                    delay 1
                    set selected messages of newViewer to {targetMessage}
                    delay 1
                    activate
                    return "Found"
                end if
            else
                return "Not Found"
            end if
        end tell

I followed a strict path approach and optimized the search by date. However, the highlight still focus to the email below the targetMessage. :confused: The mail application probably moves on to the next email because the message was not read.

tell application "Mail"
    activate
    
    set targetAccount to missing value
    repeat with anAccount in every account
        if name of anAccount is "AccountName" then
            set targetAccount to anAccount
            exit repeat
        end if
    end repeat
    
    if targetAccount is not missing value then
        set targetBox to missing value
        repeat with aBox in mailboxes of targetAccount
            if name of aBox is "MailboxName" then
                set targetBox to aBox
                exit repeat
            end if
        end repeat
        
        if targetBox is not missing value then
            set targetMessage to missing value
            set oneWeekAgo to (current date) - (7 * days)
            set filteredMessages to (every message of targetBox whose date received ≥ oneWeekAgo)
            
            repeat with aMessage in filteredMessages
                try
                    if message id of aMessage is "MessageID" then
                        set targetMessage to aMessage
                        exit repeat
                    end if
                end try
            end repeat
            
            if targetMessage is not missing value then
                if (count of message viewers) > 0 then
                    set mailViewer to message viewer 1
                else
                    set mailViewer to make new message viewer
                end if
                
                tell mailViewer
                    set selected mailboxes to {targetBox}
                    delay 0.2
                    set selected messages to {targetMessage}
                end tell
                
                return "Found"
            else
                return "Message Not found"
            end if
        else
            return "Folder Not found"
        end if
    else
        return "Account Not found"
    end if
end tell

I’ve found the problem. This behavior occurs if the organize by conversation option is enabled.