Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Unanswered

To apply validation on formrealcontrol and pass it to table field

(0) ShareShare
ReportReport
Posted on by 1,479
I have added the form real control which takes the value from the edit method i need to apply the validation on that control so when the user enter the value it should not exceeds the required limit ,if user enter the value more then limit then the warning should be generated the code for edit method is below  with the code i have written on the table validate write method which i have return on form .
  • Dineshkarlekar Profile Picture
    Dineshkarlekar 1,479 on at
    To apply validation on formrealcontrol and pass it to table field
    hi martin ,
    thanks for reply,
    i will try some other way to update the record .
  • Martin Dráb Profile Picture
    Martin Dráb 230,848 Most Valuable Professional on at
    To apply validation on formrealcontrol and pass it to table field
    What you call a working method is method causing data inconsistency, which I call a very serious bug.
  • Dineshkarlekar Profile Picture
    Dineshkarlekar 1,479 on at
    To apply validation on formrealcontrol and pass it to table field
    hi martin,
    thanks for replying ,
     
    as of now the field project value is empty on sales quotation table , so when the user enter the specific amount on project quotation form where is i have created one form real control ,the value should pass to the salesquotationtable .projectvalue  and the value should not be more than the sum of all the total cost price in pSAActivityEstimates table so now my edit method is working but i need to work on validate write where my validate write method  will check for previous value of salesquotationtable .projectvalue  field with the new salesquotationtable .projectvalue if user update it ans then throw the validation .my code is below .i hope you got what i want to do in this .
    [ExtensionOf(formStr(ProjWorkBreakdownStructureV2))]
    final class DT_PSAActivityEstimatesDK_Extension
    {
       Edit NonNegativeTotal TotalCostPrice(boolean _set ,NonNegativeTotal    _TotalCostPrice )
        {
    
            NonNegativeTotal             TotalCostPrice    =  _TotalCostPrice ;
            SalesQuotationTable   salesQuotationTable;
            Hierarchy             hierarchy = this.args().record();    
            HierarchyLinkTable    hierarchyLinkTable;
            ProjPlanVersion       projPlanVersion;
            HierarchyTreeTable    hierarchyTreeTable;
            HierarchyIdBase       HierarchyId;
            QuotationIdBase       _quotationId;
    
            _quotationId = this.dataSource(formDataSourceStr(ProjWorkBreakdownStructureV2,ProjPlanVersion )).object(fieldNum(ProjPlanVersion, TaskName)).getValue();
    
            HierarchyId      =   hierarchy.HierarchyId;
    
            select * from salesQuotationTable
                          join hierarchyLinkTable
                                 where  hierarchyLinkTable.RefRecId == salesQuotationTable.RecId
                          join hierarchy         
                                 where  hierarchyLinkTable.HierarchyId == hierarchy.HierarchyId
                          join hierarchyTreeTable   
                                 where  hierarchyTreeTable.HierarchyId == hierarchy.HierarchyId
                          join projPlanVersion 
                                 where  projPlanVersion.HierarchyTreeTableRefRecId ==  hierarchyTreeTable.RecId
                                    &&    projPlanVersion.TaskName == _quotationId;
    
           
           
            ;
            if(_set)
            {
                if(TotalCostPrice)
                {
                    ttsbegin;
                    salesQuotationTable.reread();
                    salesQuotationTable.selectForUpdate(true);
                    salesQuotationTable.ProjectValue = TotalCostPrice;
                    salesQuotationTable.validateWrite();
                    salesQuotationTable.doupdate();
                    ttscommit;
                }
            }
            else
            {
                TotalCostPrice    = salesQuotationTable.ProjectValue;
            }
            //info(this.dataSource(formDataSourceStr(ProjWorkBreakdownStructureV2,ProjPlanVersion )).object(fieldNum(ProjPlanVersion, TaskName)).getValue());
            info(strFmt("QuotationID%1 & Projectvalue%2",salesQuotationTable.QuotationId,salesQuotationTable.ProjectValue));
            return TotalCostPrice;
          
        }
    }
    and my code for validate write 
    [ExtensionOf(tableStr(SalesQuotationTable))]
    final class DT_SalesQuotationTableDK_Extension
    {
        public boolean validateWrite()
        {
    
            PSAActivityEstimates pSAActivityEstimates;
            ProjPlanVersion       projPlanVersion;
            HierarchyTreeTable    hierarchyTreeTable;
            Hierarchy             hierarchy;
            HierarchyLinkTable    hierarchyLinkTable;
            SalesQuotationTable   salesQuotationTable;
            NonNegativeTotal       TotalCostPrice;
      
            boolean ret;
            ;
    
            next validateWrite();
    
           
         while  select sum(TotalCostPrice) from psaActivityEstimates
                    join projPlanVersion
                                   where projPlanVersion.HierarchyId  == psaActivityEstimates.ProjPlanHierarchyId
                                     &&  projPlanVersion.HierarchyTaskId == psaActivityEstimates.ProjPlanHierarchyTaskId
                        join hierarchyTreeTable
                                         where hierarchyTreeTable.RecId  == projPlanVersion.HierarchyTreeTableRefRecId
                        join hierarchy
                                         where hierarchy.HierarchyId == hierarchyTreeTable.HierarchyId
                        join hierarchyLinkTable
                                         where hierarchyLinkTable.HierarchyId == hierarchy.HierarchyId
                        join salesQuotationTable
                                         where salesQuotationTable.RecId       == hierarchyLinkTable.RefRecId
                                          &&   salesQuotationTable.QuotationId == this.QuotationId 
            {
                TotalCostPrice =    psaActivityEstimates.TotalCostPrice;
    
                   if(this.ProjectValue >  TotalCostPrice)
                 {
                     ret = checkFailed("Value must not be greater than total cost price");
                 }
    
            }
    
            return ret;
        }
    }
    thanks ,
    regards,
    dinesh
  • Martin Dráb Profile Picture
    Martin Dráb 230,848 Most Valuable Professional on at
    To apply validation on formrealcontrol and pass it to table field
    The edit method is fundamentally wrong; you must redesign your logic. It seems to me that the edit method shouldn't exist at all. You can have a method displaying the value of salesQuotationTable.ProjectValue, but the update must happen at a different place  - when writing the record to database, in the same transaction.
     
    Unfortunately I don't know your business scenario, so I can't design it for you (unless you share more information with us).
  • Dineshkarlekar Profile Picture
    Dineshkarlekar 1,479 on at
    To apply validation on formrealcontrol and pass it to table field
    thanks for reply ,
    martin, 
    yes you are right with the edit method the edit  method is not populating the field as i am getting different record in edit method but i did info of the record which i selected from edit method  but i need to pass the record which i am getting into info to get field populated can you suggest how can i pass the record from info to the select query so the edit method can work fine .beleow is my code . i am getting the selected record in info i just want to pass that in select may be that works .
    [ExtensionOf(formStr(ProjWorkBreakdownStructureV2))]
    final class DT_PSAActivityEstimatesDK_Extension
    {
       Edit NonNegativeTotal TotalCostPrice(boolean _set ,NonNegativeTotal    _TotalCostPrice )
        {
    
            NonNegativeTotal             TotalCostPrice    =  _TotalCostPrice ;
            SalesQuotationTable   salesQuotationTable;
            Hierarchy             hierarchy = this.args().record();    
            HierarchyLinkTable    hierarchyLinkTable;
            ProjPlanVersion       projPlanVersion;
            HierarchyTreeTable    hierarchyTreeTable;
            HierarchyIdBase       HierarchyId;
    
            HierarchyId      =   hierarchy.HierarchyId;
    
            select * from salesQuotationTable
                          join hierarchyLinkTable
                                 where  hierarchyLinkTable.RefRecId == salesQuotationTable.RecId
                          join hierarchy         
                                 where  hierarchyLinkTable.HierarchyId == HierarchyId
                          join hierarchyTreeTable   
                                 where  hierarchyTreeTable.HierarchyId == HierarchyId
                          join projPlanVersion 
                                 where  projPlanVersion.HierarchyTreeTableRefRecId ==  hierarchyTreeTable.RecId;
    
            //salesQuotationTable = this.args().record();
           
            ;
            if(_set)
            {
                if(TotalCostPrice)
                {
                    ttsbegin;
                    salesQuotationTable.reread();
                    salesQuotationTable.selectForUpdate(true);
                    salesQuotationTable.ProjectValue = TotalCostPrice;
                    salesQuotationTable.validateWrite();
                    salesQuotationTable.doupdate();
                    ttscommit;
                }
            }
            else
            {
                TotalCostPrice    = salesQuotationTable.ProjectValue;
            }
            info(this.dataSource(formDataSourceStr(ProjWorkBreakdownStructureV2,ProjPlanVersion )).object(fieldNum(ProjPlanVersion, TaskName)).getValue());
            return TotalCostPrice;
            //info(strFmt('%1' ,this.args().caller()));
        }
    }
    validate methods is working fine but need to test it 
    [ExtensionOf(tableStr(SalesQuotationTable))]
    final class DT_SalesQuotationTableDK_Extension
    {
        public boolean validateWrite()
        {
    
            PSAActivityEstimates pSAActivityEstimates;
            ProjPlanVersion       projPlanVersion;
            HierarchyTreeTable    hierarchyTreeTable;
            Hierarchy             hierarchy;
            HierarchyLinkTable    hierarchyLinkTable;
            SalesQuotationTable   salesQuotationTable;
            NonNegativeTotal       TotalCostPrice;
      
            boolean ret;
            ;
    
            next validateWrite();
    
           
         while  select sum(TotalCostPrice) from psaActivityEstimates
                    join projPlanVersion
                                   where projPlanVersion.HierarchyId  == psaActivityEstimates.ProjPlanHierarchyId
                                     &&  projPlanVersion.HierarchyTaskId == psaActivityEstimates.ProjPlanHierarchyTaskId
                        join hierarchyTreeTable
                                         where hierarchyTreeTable.RecId  == projPlanVersion.HierarchyTreeTableRefRecId
                        join hierarchy
                                         where hierarchy.HierarchyId == hierarchyTreeTable.HierarchyId
                        join hierarchyLinkTable
                                         where hierarchyLinkTable.HierarchyId == hierarchy.HierarchyId
                        join salesQuotationTable
                                         where salesQuotationTable.RecId       == hierarchyLinkTable.RefRecId
                                          &&   salesQuotationTable.QuotationId == this.QuotationId 
            {
                TotalCostPrice =    psaActivityEstimates.TotalCostPrice;
    
                   if(this.ProjectValue >  TotalCostPrice)
                 {
                     ret = checkFailed("Value must not be greater than total cost price");
                 }
    
            }
    
            return ret;
        }
    }
    
     
  • Martin Dráb Profile Picture
    Martin Dráb 230,848 Most Valuable Professional on at
    To apply validation on formrealcontrol and pass it to table field
    There is also huge problem in your edit method. You update data in database immediately when the control value changes, but the whole record may never be saved to database (because the changes his mind, the record failed to validate, there is a network issue or so). You can cause inconsistency in production database of your client! Don't you have any experienced developer in your company who would help with the design?
  • Martin Dráb Profile Picture
    Martin Dráb 230,848 Most Valuable Professional on at
    To apply validation on formrealcontrol and pass it to table field
    You showed us your code, but forgot to explain the problem you have with it. Please add this information.
     
    By the way, validateWrite() is called when the record is to be saved, not when you change a value of the field. Therefore users will be able to enter a value exceeding the limit, but they won't be able to save the record (if you fix bugs you have there). I'm not sure that it's what you want.
     
    The bug is that you show a warning, but don't change the return value to false. Therefore the record would be saved anyway. You need something like this:
    if (this.ProjectValue > psaActivityEstimates.TotalCostPrice)
    {
        ret = checkFailed("Value must not be greater than total cost price");
    }
    And there is a typical bug of yours: you declared pSAActivityEstimates variable, but forgot to initialize it with any value. And you didn't debug your code, otherwise you would have noticed it.
  • Dineshkarlekar Profile Picture
    Dineshkarlekar 1,479 on at
    To apply validation on formrealcontrol and pass it to table field
    edit method on form 
    [ExtensionOf(formStr(ProjWorkBreakdownStructureV2))]
    final class DT_PSAActivityEstimatesDK_Extension
    {
      Edit NonNegativeTotal TotalCostPrice(boolean _set ,NonNegativeTotal    _TotalCostPrice )
        {
    
            NonNegativeTotal             TotalCostPrice    =  _TotalCostPrice ;
            SalesQuotationTable   salesQuotationTable;
            Hierarchy             hierarchy = this.args().record();
            HierarchyLinkTable    hierarchyLinkTable;
            HierarchyIdBase       HierarchyId;
    
            HierarchyId      =   hierarchy.HierarchyId;
    
            select * from salesQuotationTable
                          join hierarchyLinkTable
                                 where  hierarchyLinkTable.RefRecId == salesQuotationTable.RecId
                          join hierarchy         
                                 where  hierarchyLinkTable.HierarchyId == HierarchyId;
    
            //salesQuotationTable = this.args().record();
           
            ;
            if(_set)
            {
                if(TotalCostPrice)
                {
                    ttsbegin;
                    salesQuotationTable.reread();
                    salesQuotationTable.selectForUpdate(true);
                    salesQuotationTable.ProjectValue = TotalCostPrice;
                    salesQuotationTable.doupdate();
                    ttscommit;
                }
            }
            else
            {
                TotalCostPrice    = salesQuotationTable.ProjectValue;
            }
            return TotalCostPrice;
           
        }
    }
    validate write method on table salesquotationtable
    [ExtensionOf(tableStr(SalesQuotationTable))]
    final class DT_SalesQuotationTableDK_Extension
    {
        public boolean validateWrite()
        {
    
            PSAActivityEstimates pSAActivityEstimates;
            boolean ret;
            ;
    
            next validateWrite();
    
            if(this.ProjectValue > PSAActivityEstimates.TotalCostPrice)
            {
                warning("Value must not be greater than total cost price");
            }
            //else
            //warning(" Please fill the address value");
            return ret;
        }
    }
     
     

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,979 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,848 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans