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

Setting a field in one table to a count from another table.

(1) ShareShare
ReportReport
Posted on by 26

I have two tables, one with just all unique account numbers, and another with lines for each sale an account has made. 

I want a field in table 1 that count for each account how many sales they have done.

SQL wise it is easy:

select count(RECID) from Accounts GROUP BY AccountId;

This gives me the number of sales for each account. But how do I assign this number to the field in the right account row in table 1?

I've tried several solutions, but I cant get it to work. I am quite new to x++ and dynamics.  

Table1 tab1;

Table2 tab2;

ttsbegin;

while select forUpdate table1{

table1.sales = (select count(RecID) from table2 where table1.AccountId = table2.AccountId group by AccountId;

}

ttscommit;

I have the same question (0)
  • Verified answer
    Gunjan Bhattachayya Profile Picture
    35,423 on at

    Hi Dushatar,

    Assuming that field "Sales" in Table1 is an integer field, you can try something like this statement

    Table1 tab1;
    
    Table2 tab2;
    
    ttsbegin;
    
    while select forUpdate tab1
    {
        select count(RecID) from tab2 where tab2.AccountId = tab1.AccountId;
        
        tab1.sales = tab2.RecId;
    }
    
    ttsCommit;

  • Dushatar Profile Picture
    26 on at

    Doesnt seem to work, this sets them all to 0.

  • Dushatar Profile Picture
    26 on at

    Nevermind I (we?) forgot tab1.update();

    After I added that it worked. Thank you =)

  • Verified answer
    Gunjan Bhattachayya Profile Picture
    35,423 on at

    I was just about to reply asking you to check the DataAreaId and I forgot the update as well. Once you are comfortable with this, you can try using an update_recordeset statement for this requirement which will be faster.

    Table1 tab1;
    Table2 tab2;
    
    update_recordset tab1
        setting Sales = tab2.RecId
            join count(RecId) from tab2 
                where tab2.AccountId = tab1.AccountId;
    
    

    Also, please mark the userful answer(s) as Verified to close the thread.

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

#2
André Arnaud de Calavon Profile Picture

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

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 305 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans