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, ...
Suggested Answer

If check after select from table

(0) ShareShare
ReportReport
Posted on by

String abc;

Select firstOnly vendTable where vendTable.Id == 'US-111';

if(vendTable){

abc = vendTable.Name;

}

EDIT:

VendTable vendTable;
str abc;

select firstOnly vendTable
	where vendTable.AccountNum == 'US-111';

if (vendTable)
{
	abc = vendTable.name();
}

My question is: is the if statement necessary after the select statement.

I'm thinking that the if statement is unnecessary and i can just do the asssignment directly. (abc = vendTable.Name).

if vendTable returns empty, then abc will default to empty string. Wonder if my understanding is correct here.

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

    For me "necessary" means that the solution will not work without it.  Actually your code will not even compile (no matter if you have the condition there or not), because VendTable doesn't have a field called Name in AX2012.

    However VendTable has a method called name(), which returns the name of the vendor. Calling that method and executing the logic in it adds more overhead than checking "if (vendTable)" so in my opinion having the check is recommended (but not functionally necessary).

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

    Another bug in the code is using 'string' instead of 'str'; and you probably meant AccountNum field instead of Id.

    By the way, please Use > Insert Code (in the rich formatting view to paste source code - it makes code easier to read and work with. For example:

    str abc;
    
    select firstOnly vendTable
    	where vendTable.AccountNum == 'US-111';
    
    if (vendTable)
    {
    	abc = vendTable.name();
    }

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