Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Suggested answer

If check after select from table

Posted on by Microsoft Employee

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.

  • Martin Dráb Profile Picture
    Martin Dráb 230,235 Most Valuable Professional on at
    RE: If check after select from table

    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();
    }

  • Suggested answer
    nmaenpaa Profile Picture
    nmaenpaa 101,156 on at
    RE: If check after select from table

    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).

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans