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 add validation to lookup method in Form

(0) ShareShare
ReportReport
Posted on by 198

Hi there, as per the requirement we had added lookup method in InventTransferOrder Form for PurchId field.  In the lookup method we have added range like PurchLine.PurchStatus != Cancelled. Now how to add an Validation like if user manually enters any other Purchid  other than once coming from lookup method, we need ti throw error message and the record should not be saved. Now, the record is saving and we are looking how to add error message if user selects wrong PuchId.  Please find my below code for lookup method and suggest me accordingly. Thanks in advance

  public static void InventTransferLine_HAMPurchId_OnLookup(FormControl sender, FormControlEventArgs e)
    {
        
        Query                   query;
        QueryBuildDataSource    qbds;
        SysTableLookup          lookup;
       
        QueryBuildRange         queryBuildRange,queryBuildRange1;

        FormRun formRun = sender.formRun() as FormRun;
        FormDataSource inventTransferLine_ds = formRun.dataSource(formDataSourceStr(InventTransferOrders ,InventTransferLine)) as FormDataSource;
        InventTransferLine deliveryModeService = inventTransferLine_ds.cursor();
 
        // Create the query for the lookup
        query   = new query();
        qbds    = query.addDataSource( tableNum(PurchLine));
        FormControlCancelableSuperEventArgs event = e as FormControlCancelableSuperEventArgs;
        event.CancelSuperCall();
        InventTransferLine      inventTransferLine;
        PurchLine               purchLine;
    
        // Instantiate sysTableLookup object using table which will provide the visible fields
        lookup  = SysTableLookup::newParameters( tableNum(PurchLine), sender);
      
  
        // Add fields that will be shown in the lookup as columns
        lookup.addLookupfield( fieldNum(PurchLine,PurchId));
        lookup.addLookupMethod(tablemethodStr(PurchLine,getPurchName));
        queryBuildRange = qbds.addRange(fieldnum(PurchLine,ItemId));
        queryBuildRange.value(deliveryModeService.ItemId);
        queryBuildRange1 = qbds.addRange(fieldnum(PurchLine, PurchStatus));
        queryBuildRange1.value(SysQuery::valueNot(PurchStatus::Canceled));
     
        // Add the query to the lookup form
        lookup.parmQuery(query);
 
        // Perform the lookup
        lookup.performFormLookup();
    
           
    }
    
    // We need to add validation not to save record if PurchId is not as per Lookup and need to throw error message accordingly. Thanks

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

    You can validate the field in different validate methods:

    - validate() method of the form control (this is executed when the form control value is modified)

    - validate() method of the form data source field (this is executed when any form control that is based on this data source field is modified)

    - validateWrite() method of the form data source (this is executed before the record is saved to the database)

    - validateField() method of the table (this is executed when the field is modified on any form)

    - validateWrite() method of the table (this is executed before the record is saved to the database)

  • premK6969 Profile Picture
    198 on at

    Hi Nikolas, how i can validate in such a way that the user selected  Purchid is not from my lookup method and  stop saving that accordingly.  Please be more specific, Thanks

  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at

    You have some logic in your lookup method, to show some purch ids, correct?

    You can use similar logic in the validation to check if the entered value was any of the purch ids that are allowed/expected.

    So, if the user entered puch id "ABC", you need to have code that checks:

    1) Is this purchase order canceled? If yes, fail the validation.

    2) Does this purchase order have a line that has item that corresponds to deliveryModeService.ItemId? If no, fail the validation.

    You can't do validations in the lookup method, it's simply used to construct the lookup list.

  • André Arnaud de Calavon Profile Picture
    301,020 Super User 2025 Season 2 on at

    Hi premK6969,

    In the lookup you provided ranges. In table events like validateField or validateWrite, you need to write x logic.

    Like:

    if (purchLine.purchStatus == PurchStatus::Cancelled)
     {
        return false;
     }

    Of course you will need a table event handler extension class and follow the correct pattern for the method to implement.

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

    Your first step is deciding at which level the validation needs to be placed - see Nikolaos's reply for details.

    Then you need to check the value and return false if the validation fails. For example:

    boolean ok = super();
    
    public boolean validateWrite()
    {
        if (this.PurchId
          && PurchTable::find(this.PurchId).PurchStatus == PurchStatus::Canceled)
        {
            ok = checkFailed("A message");
        }
        
        return ok;
    }

  • premK6969 Profile Picture
    198 on at

    That helped a lot, Thanks

  • premK6969 Profile Picture
    198 on at

    Hi Martin,  i had placed the code in ValidateWrite event handler of the Form Data source. Now the error message is throwing as expected but still record is saving. How to make the record unsave if the error message is thrown ? Thanks in advance

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

    It sounds like you're not setting the result to false. Please show us your code.

  • premK6969 Profile Picture
    198 on at

    Ah yes sorry, i made it false and now its working fine.  Thanks Martin

  • premK6969 Profile Picture
    198 on at

    Hi Martin, still the validation for the lookup is not sucessful because now the issue is if we try to enter the PurchId other than coming from lookup still it is accepting and saving. We need validation for Cancelled Orders and Purchase Orders which is not coming from Lookup. It was successful for Cancelled Orders and It was still saving the Purchase Orders which are not from Lookup. Please find the code attached since i had written the logic in ValidateWrite event Handler.

    1. If we enter Cancelled Purchase Orders in the lookup field it was throwing error and Not Saving :  As expected

    2. If we enter the Purchase Id which are from Lookup it needs to save : As expected

    3. If we enter Purchase Id which  is not populating from Lookup  it needs to throw error and should not save :  Now it is saving and its an issue.

    Please suggest accordingly. Thanks

       [FormDataSourceEventHandler(formDataSourceStr(InventTransferOrders, InventTransferLine), FormDataSourceEventType::ValidatedWrite)]
        public static void  InventTransferLine_OnValidatedWrite(FormDataSource sender, FormDataSourceEventArgs e)
        {
            boolean ok;
            Query                   query;
            QueryBuildDataSource    qbds;
            QueryBuildRange         queryBuildRange,queryBuildRange1;
            FormRun formRun = sender.formRun() as FormRun;
            FormDataSource inventTransferLine_ds = formRun.dataSource(formDataSourceStr(InventTransferOrders ,InventTransferLine)) as FormDataSource;
            InventTransferLine formObj = inventTransferLine_ds.cursor();
            InventTransferLine      inventTransferLine;
            PurchLine               purchLine;
                
            query   = new query();
            qbds    = query.addDataSource( tableNum(PurchLine));
            queryBuildRange = qbds.addRange(fieldnum(PurchLine,ItemId));
            queryBuildRange.value(formObj.ItemId);
            queryBuildRange1 = qbds.addRange(fieldnum(PurchLine, PurchStatus));
            queryBuildRange1.value(SysQuery::valueNot(PurchStatus::Canceled));
             
    
            select firstonly purchLine where
                purchLine.PurchId == formObj.HAMPurchId;  // Tried adding this logic to fix Point 3 mentioned still unsuccessful
    
            if (!purchLine 
                || PurchTable::find(formObj.HAMPurchId).PurchStatus == PurchStatus::Canceled)
            {
                throw error("Specified Purchase Order does not exist");
                ok = false;
            }
            else
            {
                ok = true;
            }
        }
    
    }

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

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 285 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans