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

How to validate field based on a recursive function? D365FO

(0) ShareShare
ReportReport
Posted on by 1,552

I added a checkbox field in projTable where i limit turning it into a yes based on a condition in recursive function. So i created a new recursive method that i call inside the validateField method. If my recursive method returns true no validation occurs but if it returns false an error message should appear. What my recursive method is doing, is that it makes the return value false but it then calls itself again and make the return value true always. what should i do? Here's what i did:

[ExtensionOf(tableStr(ProjTable))]
final class ProjTable_Extension
{

/// <summary>
///
/// </summary>
/// <returns></returns>
///
public boolean validateField(FieldId fieldId)
{
    boolean ret;
    boolean retCheck = true;
    ret = next validateField(fieldId);
   switch (fieldId)
  {
     case fieldNum(ProjTable, TestField) :
     retCheck = this.getProj(this.ProjId); // this is the method i created
     if (!retCheck)
     {
        ret = ret && checkFailed("Error");
     }
  break;
  }
return ret;

}

boolean getProj(ProjId _projId = "")
{

boolean ret = true; // initial value
ProjTable projTable;
while select projTable
where projTable.parentId == _projId
{
if(projTable.TestField== NoYes::Yes)
{

ret = false;
return ret ; // it comes inside and put ret = false then it gets out of the while loop, then go back to the else, then go to the final ret and return it true always
}
else
{
this.getProj(this.projId); //after setting ret = false it still comes here and then return ret =  true 
}
}
return ret ; //always returns true
}


what am I doing wrong? If my code in the new method makes ret false, why does it return true value to retCheck?

I have the same question (0)
  • Suggested answer
    Sukrut Parab Profile Picture
    71,710 Moderator on at

    Why you have while select on projtable ? ProjId  is part of  unique index on the table so you dont need  while select . Also why are you returning in if loop  ? You dont need that , just return at the end of method. Any reason for calling same method again in else loop ?

    boolean getProj(ProjId _projId = "")
    {
    
    boolean ret = true; // initial value
    ProjTable projTable;
    select firstonly  projTable
    where projTable.parentId == _projId
    {
        if(projTable.TestField== NoYes::Yes)
        {
        
            ret = false;
        
        }
        else
        {
            ret = true
        }
    }
    return ret ; 
    }

  • junior AX Profile Picture
    1,552 on at

    I need it to be a recursive method. Because i want to check all the chids of my project that are in the same branch that's why i added the while.

    So the logic is that i can make the new field true only if all the childs of  my projects (and childs of childs.. Etc) don't have this field ticked

  • Sukrut Parab Profile Picture
    71,710 Moderator on at

    ahh .. My mistake I did not notice you have parentID in your query. You have to pass  projTable projId  in else loop to your method and get result in your Boolean variable ? If you pass this its going to pass  original projId to the method  not the child projId.

    else

    {

    ret  =this.getProj(projTable .projId);

    }

  • Suggested answer
    Eugen Glasow Profile Picture
    6,359 on at

    This is useless, Junior AX. Look at the forms and queries: the ProjId of the children ALWAYS starts with the ProjID of the parent: 908439 ->  908439_01 -> 908439_01_01.

    This is why in the standard code you are going to see the queries "LIKE ProjIdParent*".

    Replace your recursive code with the simple query above.

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

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 250 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans