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

Community site session details

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

Validate method in table

(0) ShareShare
ReportReport
Posted on by 182

Hi Experts,

I have a a custom module for Pettycash management which has Journal form(ANCPettyCashPlantJournal) and journal line(ANCPettyCashPlant) forms. In Journal, I can select the bank and when i am creating lines, there should be a validation to check if the sum of all the journal lines is less than the bank credit limit(It is in bank master).

I have written my validation in validate write like this.

public boolean validateWrite()
    {
        boolean				ret;
    
        ret = super();

		if(ret)
        {
           if(this.totalPCAmount(this.PlantJournalId) > abs(BankAccountTable::find(ANCPettyCashPlantJournal::find(this.PlantJournalId).BankAccount).OverDraftLimit))
            {
                ret = false;
                warning('@ANCLabel:CreditLimitError');
            }
         }
    }

And the method to find total line amount is below

private	Amount	totalPCAmount(ANCPettyCashPlantJournalId	_journalId)
    {
        ANCPettyCashPlant	pcPlantSum;
        Amount				total;

		select sum(GrossAmount)
            from pcPlantSum
            where pcPlantSum.PlantJournalId == _journalId;

            total = pcPlantSum.GrossAmount;

        return total;
	}

If I am entering a value greater than bank credit limit then its validating and giving warning messages, however when I editing the gross amount field, it is not considering the new entered value and still gives me error.

In the below example Bank credit limit is 1000. First i entered 3000 and it gave me validation error. Then I entered 500 in the same field, but it still gives me the same error. It should not give error since bank credit limit is above 500. 

PC-validation.png

I have the same question (0)
  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at
    RE: Validate method in table

    Did you debug it?

    Your totalPCAmount is reading the sum of already existing lines in ANCPettyCashPlant table.

    The (current version of the) record that the user is working on is not yet considered in the calculation, since it's not saved before the validation passes.

  • Maverick Profile Picture
    182 on at
    RE: Validate method in table

    Thanks Nikolaos for the reply. Yes, on debugging I can find the same issue as you explained. It is picking the sum of already existing lines. How do I correct this error?

  • Verified answer
    nmaenpaa Profile Picture
    101,160 Moderator on at
    RE: Validate method in table

    You need to pick the sum of existing lines, PLUS the sum of the current line. Something like this (notice that I also removed the parameter from totalPCAmount method since it's an instance method and therefore you already know the PlantJournalId.

    public boolean validateWrite()
    {
        boolean	ret = super();
        
    
    	if(ret)
        {
    	   Amount grossAmount = this.GrossAmount   this.totalPCAmount();
     	   Amount creditLimitAmount = abs(BankAccountTable::find(ANCPettyCashPlantJournal::find(this.PlantJournalId).BankAccount).OverDraftLimit);
            
            if(grossAmount > creditLimitAmount)
            {
                ret = checkFailed("@ANCLabel:CreditLimitError");
            }
        }
        return ret;
    }
    
    
    public	Amount totalPCAmount()
    {
        ANCPettyCashPlant	pcPlantSum;
        Amount				total;
    
    	select sum(GrossAmount) from pcPlantSum
         where pcPlantSum.PlantJournalId == this.PlantJournalId;
    
        total = pcPlantSum.GrossAmount;
    
        return total;
    }

  • Maverick Profile Picture
    182 on at
    RE: Validate method in table

    Hi Nikolaos,

    I have used the logic you provided with some minor fix in totalPCAmount() and now it works fine. Thanks a lot for your support.

    private	Amount	totalPCAmount()
        {
            ANCPettyCashPlant	pcPlantSum;
            Amount				total;
    
    		select sum(GrossAmount)
                from pcPlantSum
                where pcPlantSum.PlantJournalId == this.PlantJournalId
                && pcPlantSum.RecId != this.RecId;
    
            total = pcPlantSum.GrossAmount;
    
            return total;
    	}

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

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

#1
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 1,933

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 537 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans