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

Select distinct record using x++

(0) ShareShare
ReportReport
Posted on by 390

Hey team,

I'm trying to select some files from the DocuRef table but there seems to been multiple copies of the file in the DocuRef table and I want to select the distinct records. How can I do that. Can you please help me modify my select query to select only one file out of the duplicates

My select statement:

  while select * from docuref where docuref.RefTableId == 17136  join docuvalue
                where docuvalue.recid == docuref.ValueRecId
        {
        //my code
        }

Example of multiple files in my table:

Screenshot-_2800_154_2900_.png

Screenshot-_2800_155_2900_.png

Screenshot-_2800_156_2900_.png

I have the same question (0)
  • Blue Wang Profile Picture
    on at

    Hi Skd,

    You can add Group by in the select statement which field you want the distinct records.

  • Suggested answer
    Sergei Minozhenko Profile Picture
    23,093 on at

    Hi skd,

    If you have several copies and you don't need them, you need to delete them from document management.

    Also, I recommend using DocuRefSearch api instead of direct queries

    DocuRefSearch           docuRefSearch;
    DocuRef                 docuRef;
    
    docuRefSearch = DocuRefSearch::newCommon(record_where_documents_are_attached);
    
    if (docuRefSearch.next())
    {
        docuRef = docuRefSearch.docuRef();
    }

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    As mentioned above, if someone added the same file three times, you should remove the duplicates. The system can't tell that they are "distinct", we can see that each of them points to different DocumentId.

    The only way how you could tell that they are distinct is to fetch the file contents from the storage and compare the actual files in x++. But I would not recommend that, instead make sure that you don't upload same files many times.

  • skd Profile Picture
    390 on at

    Hey Blue Wang,

    There is some issue with my code. It gives me an error saying "Stack Trace: Company does not exist" .Please help me with the issue.

    while select Name, maxof(ModifiedDateTime) from docuref
                group by docuref.Name, ModifiedDateTime
                where docuref.RefTableId == 17136
                join docuvalue
              where docuvalue.RecId == docuref.ValueRecId
    
    
            {
                
                XmlDocument xmlDoc = XmlDocument::newFromStream(DocumentManagement::getAttachmentStream(docuRef));
                xmlNode = xmlDoc.getNamedElement('FormRunConfigurationSerializer').getNamedElement('OwnerValue');
                _owner = xmlNode.text();
                info(strFmt("Owner Name: %1", _owner));
    
    
                
                ttsbegin;
                    formRunConfiguration.Owner = _owner;
                    formRunConfiguration.Scope = FormRunConfigurationScope::User;
                    formRunConfiguration.User = "Nigel.Farage";
                    formRunConfiguration.IsDefault = NoYes::Yes ;
                    formRunConfiguration.Company = curExt();
                    formRunConfiguration.Name = "Default";
                    formRunConfiguration.Version = 1;
                    formRunConfiguration.FormViewOptionType = FormRunConfigurationFormViewOptionType::Default;
                    formRunConfiguration.insert();
               
    
    
                formRunConfigurationOptions.clear();
                formRunConfigurationOptions.User            = "Nigel.Farage";
                formRunConfigurationOptions.Owner           = _owner;
                formRunConfigurationOptions.Enabled         = NoYes::Yes;
                formRunConfigurationOptions.EnabledExplicitPersonalization = UnknownNoYes::Yes;
                formRunConfigurationOptions.insert();
                ttscommit;
    
    
                
            }

    Also when I change my select query to this it works okay. Just the issue is it stops on the duplicate files. (Duplicate files cannot be deleted by me due to some version changes as specified by my company)

      while select * from docuref where docuref.RefTableId == 17136 join docuvalue
        where docuvalue.recid == docuref.ValueRecId
    
            {
                XmlDocument xmlDoc = XmlDocument::newFromStream(DocumentManagement::getAttachmentStream(docuRef));
                xmlNode = xmlDoc.getNamedElement('FormRunConfigurationSerializer').getNamedElement('OwnerValue');
              
                _owner = xmlNode.text();
                info(strFmt("Owner Name: %1", _owner));
    
                ttsbegin;
                    formRunConfiguration.Owner = _owner;
                    formRunConfiguration.Scope = FormRunConfigurationScope::User;
                    formRunConfiguration.User = "Nigel.Farage";
                    formRunConfiguration.IsDefault = NoYes::Yes ;
                    formRunConfiguration.Company = curExt();
                    formRunConfiguration.Name = "Default";
                    formRunConfiguration.Version = 1;
                    formRunConfiguration.FormViewOptionType = FormRunConfigurationFormViewOptionType::Default;
                    formRunConfiguration.insert();
               
    
    
                formRunConfigurationOptions.clear();
                formRunConfigurationOptions.User            = "Nigel.Farage";
                formRunConfigurationOptions.Owner           = _owner;
                formRunConfigurationOptions.Enabled         = NoYes::Yes;
                formRunConfigurationOptions.EnabledExplicitPersonalization = UnknownNoYes::Yes;
                formRunConfigurationOptions.insert();
                ttscommit;
    
    
                
            }

  • nmaenpaa Profile Picture
    101,160 Moderator on at

    Hi sdk,

    when you get exceptions, the first thing to do is to debug it to understand which line of code throws the exception. Could you do that and let us know? Thanks!

  • nmaenpaa Profile Picture
    101,160 Moderator on at

    By the way, you should never hard code table ids in your code. Table ids can be different across different systems.

    Use tableNum(MyTable) instead.

  • skd Profile Picture
    390 on at

    Hey Nikolaos Mäenpää,

    I did debug and I get an exception at this line first:

     XmlDocument xmlDoc = XmlDocument::newFromStream(DocumentManagement::getAttachmentStream(docuRef));

    Screenshot-_2800_159_2900_.png

    Also, I added a watch on the select query which gave me this error:

    Screenshot-_2800_158_2900_.png

  • skd Profile Picture
    390 on at

    Yes I will, just till I get my output I'm using hard coded values.

  • Suggested answer
    Sergei Minozhenko Profile Picture
    23,093 on at

    Hi skd,

    The issue is that you are using grouping in the initial query and it doesn't populate all fields for DocuRef that are needed in getAttachmentStream method. You should use firstonly sort query by ModifiedDateTime in reverse order to get the latest record

    select firstonly docuref
        order by ModifiedDateTime desc
        where docuref.RefTableId == 17136
    join docuvalue
        where docuvalue.RecId == docuref.ValueRecId;
    
    if (docuRef.RecId != 0)
    {
        ...
    }

  • skd Profile Picture
    390 on at

    Hey Sergei Minozhenko,

    But this will only select one file right? There are multiple files I want to select, so how can I do that?

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 672 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 289 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans