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, ...
Suggested answer

Execute code for every record in the form

(0) ShareShare
ReportReport
Posted on by 212

Hi all,

I want to execute some piece of code for every record in the form. This is because the data on the form needs to be updated every time the form is opened. How do I make sure that the piece of code is executed for all the records every time when the form is opened and updated data is displayed on the form.

Thanks and Regards.

I have the same question (0)
  • Mohit Rampal Profile Picture
    12,563 Moderator on at
    RE: Execute code for every record in the form

    Hi, Can you provide more detail on what logic you want to update on which field/s? Also, if there are lot of records on form then better to use tmp table as datasource and populate it every time form is open.

  • GirishS Profile Picture
    27,825 Moderator on at
    RE: Execute code for every record in the form

    Hi,

    It's not possible to update each record when opening the form - It will impact the performance for opening of the form.

    Can you elaborate what you want to update?

    Thanks,

    Girish S.

  • Dynamics365Admin Profile Picture
    212 on at
    RE: Execute code for every record in the form

    [Form]
    public class AK_ForwardLookingAllocation extends FormRun
    {
        #TimeConstants
        #ISOCountryRegionCodes
    
       
        int WeekCount = 38;
    
        
        int                                 firstDayIndex = 1;
        LabelString                         dayLabels[62];
        NoYes                               requireStartStopTime;
    
    
    
        public Array setDatesInArray()
        {
            TSDate d;
            TSDate cwstartDate;
            
            Array dates = new Array(Types::Date);
    
            d = DateTimeUtil::date(systemDateget());
            TSDate temp_date;
            cwstartDate = Global::dateStartWk(d) 1;
            for(int i=-6; i<=31;i  )
            {
                temp_date = (cwstartDate  7*i);
                dates.value(i 7, temp_date);
            }
            return dates;
        }
    
        void setLineGridDays(
            TransDate _periodFrom,
            TransDate   _periodTo)
        {
            
            Array gridDays = this.setDatesInArray();
            str     dayControlNames = strReplace(formcontrolstr(AK_ForwardLookingAllocation, INCK_ResourceRequitionLine_RealBaseCopy_1),'1','%1');
            
            for(int i=1;i<=gridDays.lastIndex();i  )
            {
                info(strFmt("Dates : %1",gridDays.value(i)));
            }
    
            for (int dayNum = 1; dayNum <= WeekCount; dayNum  )
            {
                FormRealControl hoursField = element.design().controlName(strfmt(dayControlNames,dayNum));
                
    
                if (any2date(gridDays.value(dayNum)) != dateNull())
                {
                    dayLabels[dayNum] = this.getDateCaption(any2date(gridDays.value(dayNum)));
                    hoursField.visible(true);
                    //hoursField.label(gridDays.value(dayNum));
                    hoursField.label(dayLabels[dayNum]);
                    hoursField.allowEdit(!requireStartStopTime);
                }
                else
                {
                    hoursField.visible(false);
                    hoursField.label('');
                }
            }
            
        }
    
        public str getDateCaption(TransDate _date)
        {
            //TSWeekDayAbbrev dayAbbr = dayOfWk( _date);
            //str             dateStr = TSPeriods::date2StrUsrMMDD(_date);
            str             dateStr = this.date2StrUsrMMDDYYYY(_date);
            return strFmt("%1", dateStr);
        }
    
        public TempStr date2StrUsrMMDDYYYY(date _transDate)
        {
    
            return date2str(_transDate, -1, DateDay::Digits2, -1, DateMonth::Digits2, -1, DateYear::Digits4, DateFlags::FormatAll);
        }
    
        public void init()
        {
            super();
            this.setLineGridDays(systemDateGet() (-7*6),systemDateGet() (7*31));
            INCK_ResourceRequitionLine_ds.executeQuery();
           // element.refreshTimesheetGrid();
    
          
    
        }
    
        [DataSource]
        class INCK_ResourceRequitionLine
        {
            /// 
            ///
            /// 
            public void executeQuery()
            {
                QueryBuildDataSource  queryBuildDataSource;
     
                queryBuildDataSource = this.query().dataSourceTable(tablenum(INCK_ResourceRequitionLine));
                queryBuildDataSource.clearRanges();
                queryBuildDataSource.addRange(fieldnum(INCK_ResourceRequitionLine, LineAllocationStatus)).value(enum2Str(INCK_LineAllocationStatus::Allocated));
                queryBuildDataSource.addRange(fieldnum(INCK_ResourceRequitionLine, LineAllocationStatus)).value(enum2Str(INCK_LineAllocationStatus::ExternalHiring));
                queryBuildDataSource.addRange(fieldnum(INCK_ResourceRequitionLine, LineAllocationStatus)).value(enum2Str(INCK_LineAllocationStatus::Hold));
                queryBuildDataSource.addRange(fieldnum(INCK_ResourceRequitionLine, LineAllocationStatus)).value(enum2Str(INCK_LineAllocationStatus::Offered));
                queryBuildDataSource.addRange(fieldnum(INCK_ResourceRequitionLine, LineAllocationStatus)).value(enum2Str(INCK_LineAllocationStatus::Pending));
    
                super();
            }
    
            [DataField]
            class AllocationPercentage 
            {
                /// 
                ///
                /// 
                public void modified()
                {
                    //Array dates = element.setDatesInArray();
                    str     dayControlNames = strReplace(formcontrolstr(AK_ForwardLookingAllocation, INCK_ResourceRequitionLine_RealBaseCopy_1),'1','%1');
                    super();
                    
    
                    for (int i=1; i<=38; i  )
                    {
                        FormRealControl hoursField = element.design().controlName(strfmt(dayControlNames, i));
                      //  if( dates.value(i) >= INCK_ResourceRequitionLine.AllocationStartDate)
                        Info(strFmt("%1", INCK_ResourceRequitionLine.AllocationStartDate));
                        Info(strFmt("%1", hoursField.labelText()));
                        if( str2Date(hoursField.labelText(),213) >= INCK_ResourceRequitionLine.AllocationStartDate)
                        {
                            INCK_ResourceRequitionLine.RealBaseCopy[i-1] = ((6-dayOfWk(INCK_ResourceRequitionLine.AllocationStartDate))*(INCK_ResourceRequitionLine.AllocationPercentage/5));
                            break;
    
                        }
                    }
    
                    for (int i=1; i<=38; i  )
                    {
                        FormRealControl hoursField = element.design().controlName(strfmt(dayControlNames, i));
    
                       //if (dates.value(i) <= INCK_ResourceRequitionLine.AllocationEndDate && dates.value(i) >= INCK_ResourceRequitionLine.AllocationStartDate)
                        Info(strFmt("%1", INCK_ResourceRequitionLine.AllocationStartDate));
                        Info(strFmt("%1", hoursField.labelText()));
                        if (str2Date(hoursField.labelText(),213) <= INCK_ResourceRequitionLine.AllocationEndDate && str2Date(hoursField.labelText(),213) >= INCK_ResourceRequitionLine.AllocationStartDate)
                        {
                            INCK_ResourceRequitionLine.RealBaseCopy[i] = INCK_ResourceRequitionLine.AllocationPercentage;
                        }
                    }
    
                    for (int i=38; i>=1; i--)
                    {
                        FormRealControl hoursField = element.design().controlName(strfmt(dayControlNames, i));
                       // if( dates.value(i) <= INCK_ResourceRequitionLine.AllocationEndDate)
                        Info(strFmt("%1", INCK_ResourceRequitionLine.AllocationStartDate));
                        Info(strFmt("%1", hoursField.labelText()));
                        if( str2Date(hoursField.labelText(),213) <= INCK_ResourceRequitionLine.AllocationEndDate)
                        {
                            INCK_ResourceRequitionLine.RealBaseCopy[i] = ((dayOfWk(INCK_ResourceRequitionLine.AllocationEndDate))*(INCK_ResourceRequitionLine.AllocationPercentage/5));
                            break;
    
                        }
                    }
    
                }
    
            }
    
            [DataField]
            class EmployeeId 
            {
                /// 
                ///
                /// 
                public void modified()
                {
                    ResourceView resourceView;
                    super();
                    select * from resourceView where resourceView.ResourceId == INCK_ResourceRequitionLine.EmployeeId;
                    INCK_ResourceRequitionLine.INCK_Worker = resourceView.Worker;
                }
    
            }
    
        }
    
    }

    Hi Manoj and Girish, Thanks for your response.

    I have a table in which I have a field named RealBaseCopy which is a base enum containing 38 elements. Each element of enum is meant to store a real value based on some conditions. I also a a real field named percentageAllocation. Based on the value of percentageAllocation, the values are inserted in to the elements of the base enum RealBaseCopy.

    I have modified the labels of the base enum elements dynamically to display dates of upcoming weeks. The data is inserted into the base enum elements after comparison of the labels of the base enum elements with the allocationstartdate and allocationenddate fields of the table. Refer to the attached code.

    The issue is that if I create an entry this week into the table, the data is entered correctly but when I view that entry next week, the existing data is moved to the next base enum element (because the current week date is moved to the previous label but the data in table remains at the same index thus showing it below the next week label in form) thus resulting in wrong display of data.

    How do I resolve it? Please help.

    Thanks and Regards.

  • Dynamics365Admin Profile Picture
    212 on at
    RE: Execute code for every record in the form

    [quote user="Mohit Rampal"]

    Hi, Can you provide more detail on what logic you want to update on which field/s? Also, if there are lot of records on form then better to use tmp table as datasource and populate it every time form is open.

    [/quote]

    Hi Mohit,

    How can I populate the temp table for every record everytime the form is opened?

    Any help on this would be highly appreciated. Thanks!

    Regards.

  • Suggested answer
    Mohit Rampal Profile Picture
    12,563 Moderator on at
    RE: Execute code for every record in the form

    I have done the same for a form having more than 50k transactional records and few calculated fields in form grid. Create a temporary table, add method to populate records (we are using insert_recordset and update_recordset for better performance).

    alexdmeyer.com/.../

    community.dynamics.com/.../ax-2012-using-temporary-table-as-form-s-datasource

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…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

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

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 582 Most Valuable Professional

#3
CU05031448-0 Profile Picture

CU05031448-0 526

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans