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

Announcements

News and Announcements icon
Community site session details

Community site session details

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

Select expression statement in D365FnO

(0) ShareShare
ReportReport
Posted on by 1,215

Hi all,

I wrote a code below its working fine for me.

headerCopy.ThirdPartyLeaveReturnTransRef    = (select firstonly TransTypeId from MPTransactionsSetup
where  MPTransactionsSetup.TransTypeWorker == MPTransactionTypeWorker::LeaveReturn).TransTypeId;
headerCopy.ThirdPartyLeaveEncTransRef       = (select firstonly TransTypeId from MPTransactionsSetup
where  MPTransactionsSetup.TransTypeWorker == MPTransactionTypeWorker::LeaveEnc).TransTypeId;
headerCopy.ThirdPartyLeaveReqTransReference = (select firstonly TransTypeId from MPTransactionsSetup
where  MPTransactionsSetup.TransTypeWorker == MPTransactionTypeWorker::Leave).TransTypeId;
headerCopy.ThirdPartyTicketEncTransRef      =(select firstonly TransTypeId from MPTransactionsSetup
where  MPTransactionsSetup.TransTypeWorker == MPTransactionTypeWorker::TicketEnc).TransTypeId;
headerCopy.ThirdPartyTicketReqTransRef      = (select firstonly TransTypeId from MPTransactionsSetup
where  MPTransactionsSetup.TransTypeWorker == MPTransactionTypeWorker::TicketEnc).TransTypeId;

headerCopy.insert();

Output : 

SQLExpreesion.PNG

The question is my TL rejected in code review "Don't use select statement expression and use single select"

so that i did it an another way, below my code.

select firstonly setup
      where (setup.TransTypeWorker == MPTransactionTypeWorker::LeaveReturn
    || setup.TransTypeWorker == MPTransactionTypeWorker::LeaveEnc
    || setup.TransTypeWorker == MPTransactionTypeWorker::Leave
    || setup.TransTypeWorker == MPTransactionTypeWorker::TicketEnc
    || setup.TransTypeWorker == MPTransactionTypeWorker::TicketEnc);

    headerCopy.ThirdPartyLeaveReturnTransRef     = setup.TransTypeId;
    headerCopy.ThirdPartyLeaveEncTransRef        = setup.TransTypeId;
    headerCopy.ThirdPartyLeaveReqTransReference  = setup.TransTypeId;
    headerCopy.ThirdPartyTicketEncTransRef       = setup.TransTypeId;
    headerCopy.ThirdPartyTicketReqTransRef       = setup.TransTypeId;
    headerCopy.insert();

Below output.

SQl2.PNG

issue : its showing all leave once select lookup its showing correct value. i want to once inserted show the correct status.

As shown in the first screen shot i want so that what am i missing in second code ?

please give me your idea.

Thanks  

I have the same question (0)
  • Verified answer
    nmaenpaa Profile Picture
    101,172 Moderator on at

    You should have just one select statement. But since you need many records, it must not be a "firstonly" select statement.

    Your original code selects 5 different records with 5 different select statements. Now you need to select the same 5 records with one select statement. Then iterate the result, and use if statements to set correct field in headerCopy table, based on setup.TransTypeWorker.

    You can also ask directly from the person who did the code review, I'm sure they can help you.

  • Verified answer
    Martin Dráb Profile Picture
    239,784 Most Valuable Professional on at

    It should be completely obvious that your code is wrong. You clearly assign the same value (setup.TransTypeId) to all fields, which isn't what you want.

    I don't know your MPTransactionsSetup table. If there is a single record for each TransTypeWorker, simply iterate the table and assign values to the right field based on the type:

    while select TransTypeId, TransTypeWorker from setup
    	//   where clause if needed
    {
    	switch (setup.TransTypeWorker)
    	{
    		case MPTransactionTypeWorker::LeaveReturn:
    			headerCopy.ThirdPartyLeaveReturnTransRef = setup.TransTypeId;
    			break;
    		case MPTransactionTypeWorker::LeaveEnc:
    			headerCopy.ThirdPartyLeaveEncTransRef = setup.TransTypeId;
    			break;
    		...
    	}
    }

    If there may be more records for the same type, you'll need to use 'group by' (and decide which TransTypeId you want to take).

    Notice that the code is not just more efficient, but also easier to write and read.

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

    while select  setup
          where (setup.TransTypeWorker == MPTransactionTypeWorker::LeaveReturn
        || setup.TransTypeWorker == MPTransactionTypeWorker::LeaveEnc
        || setup.TransTypeWorker == MPTransactionTypeWorker::Leave
        || setup.TransTypeWorker == MPTransactionTypeWorker::TicketEnc
        || setup.TransTypeWorker == MPTransactionTypeWorker::TicketEnc)
    {
    
        switch (setup.TransTypeWorker)
        {
    
            case MPTransactionTypeWorker::LeaveReturn:
                headerCopy.ThirdPartyLeaveReturnTransRef     = setup.TransTypeId;
                break;
            .
            .
            .
            case MPTransactionTypeWorker::TicketEnc:
                headerCopy.ThirdPartyTicketEncTransRef      = setup.TransTypeId;
                break;
            
        }
    }
    headerCopy.insert();

  • Riyas ahamed F Profile Picture
    1,215 on at

    Thanks for your valuable answer.

    And, in your three answer I learnt interesting things in while loop condition switch case.

    Again thank you very much sir(Nikolaos Mäenpää , Martin Dráb, Ergün Şahin). :)

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

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

#1
Abhilash Warrier Profile Picture

Abhilash Warrier 554 Super User 2026 Season 1

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 492 Super User 2026 Season 1

#3
Subra Profile Picture

Subra 422

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans