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, ...
Answered

How to fill formReferenceGroupControl with a value in modified event handler?

(0) ShareShare
ReportReport
Posted on by 1,965

Hi,

So i want to set formReferenceGroupControl with a recId value, but it's not working:

    /// 
    ///
    /// 
    /// 
    /// 
    [FormControlEventHandler(formControlStr(CaseDetailCreate, AACaseCategory1), FormControlEventType::Modified)]
    public static void AACaseCategory1_OnModified(FormControl sender, FormControlEventArgs e)
    {

        FormStringControl categoryType = sender.formRun().design().controlName(formControlStr(CaseDetailCreate, AACategoryType)) as FormStringControl;
        FormReferenceGroupControl caseCategory = sender.formRun().design().controlName(formControlStr(CaseDetailCreate, CaseCategoryHierarchyDetail_CaseCategory)) as FormReferenceGroupControl;
    
        CaseCategoryHierarchyDetail caseCategoryHierarchyDetail;
        CaseCategoryType caseCategoryType;
        select firstonly RecId from caseCategoryHierarchyDetail where caseCategoryHierarchyDetail.CaseCategory == sender.valueStr()
        && caseCategoryHierarchyDetail.CategoryType == str2Enum(caseCategoryType,categoryType.text());

        caseCategory.value(caseCategoryHierarchyDetail.RecId); //// HERE I'm not seeing the RecId value reflected in the form
        caseCategory.modified();


    }

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    If CaseCategoryHierarchyDetail_CaseCategory control is bound to a data source field, which is likely the case, trying to assign its value from code is wrong. You need to set the value of the data source field.

  • .. Profile Picture
    1,965 on at

    Hi Martin,

    Thanks but i have one issue

    Here's what I did:

        /// 
        ///
        /// 
        /// 
        /// 
        [FormControlEventHandler(formControlStr(CaseDetailCreate, AACaseCategory2), FormControlEventType::Modified)]
        public static void AACaseCategory2_OnModified(FormControl sender, FormControlEventArgs e)
        {
            FormStringControl categoryType = sender.formRun().design().controlName(formControlStr(CaseDetailCreate, AACategoryType)) as FormStringControl;
            FormStringControl caseCategory1 = sender.formRun().design().controlName(formControlStr(CaseDetailCreate, AACaseCategory1)) as FormStringControl;
            FormReferenceGroupControl caseCategory = sender.formRun().design().controlName(formControlStr(CaseDetailCreate, CaseCategoryHierarchyDetail_CaseCategory)) as FormReferenceGroupControl;
            
            FormRun formRun = sender.formRun();
            formDataSource formDataSource = formRun.dataSource();
            CaseDetailBase caseDetailBase = formDataSource.cursor();
            CaseCategoryHierarchyDetail caseCategoryHierarchyDetail;
            CaseCategoryType caseCategoryType;
    
            if(sender.valueStr() != null)
            {
                select firstonly RecId from caseCategoryHierarchyDetail where caseCategoryHierarchyDetail.CaseCategory == sender.valueStr()
                    && caseCategoryHierarchyDetail.CategoryType == str2Enum(caseCategoryType,categoryType.text());
    
    
                caseDetailBase.CategoryRecId = caseCategoryHierarchyDetail.RecId;
    
            }
            else
            {
                select firstonly RecId from caseCategoryHierarchyDetail where caseCategoryHierarchyDetail.CaseCategory == caseCategory1.text()
                    && caseCategoryHierarchyDetail.CategoryType == str2Enum(caseCategoryType,categoryType.text());
    
    
                caseDetailBase.CategoryRecId = caseCategoryHierarchyDetail.RecId;
            }
            caseCategory.modified();
        }


    As you can see, I'm calling modified at the end but it's not calling the standard method:
    pastedimage1665474082300v1.png

  • Verified answer
    Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    You changed a value of the data source field, but you're calling modified() of the control, not of the field. Try calling modified() of the datasorce field:

    caseCategoryHierarchyDetail_ds.object(fieldNum(CaseCategoryHierarchyDetail, CaseCategory)).modified();

    By the way, the comparison sender.valueStr() != null is meaningless. Strings aren't nullable, therefore valueStr() can't ever return null. I think you want to compare it with an empty string.

  • .. Profile Picture
    1,965 on at

    Hi Martin,

    i always compare strings with null and it works every time and it works in this case as well. You prefer this instead sender.valueStr() != "" ???

    Also what's the difference between valurStr() and text() ?

    As for the modified field, it somehow worked. However, caseDetailBase when debugging is empty, I know the control I'm modifying doesn't relate to any datasource it's just a string that I added to the form, but how did the modify work

            FormRun formRun = sender.formRun();
            formDataSource formDataSource = formRun.dataSource();
            CaseDetailBase caseDetailBase = formDataSource.cursor();  //empty here why?
            caseDetailBase.CategoryRecId = 0;
            formDataSource.object(fieldNum(CaseDetailBase, CategoryRecId)).modified();

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

    Comparing a string with null is logically wrong, because a string variable can't ever contain null. It's not a valid value and therefore comparing with it makes no sense. Fortunately for you, X++ will make sense from it and use a correct comparison under the hood.

    text() is a methood of FormStringControl and that what you should use, IMHO. valueStr() is defined on FormControl (I believe) and it can't be used an non-string controls too (e.g. it'll give you a text represantion of a value of FormIntControl).

    CaseCategoryHierarchyDetail_CaseCategory in CaseDetailCreate form is bound to CategoryRecId field of CaseDetailBase data source.

  • .. Profile Picture
    1,965 on at

    Hi Martin,

    1. I think it's still correct to use valueStr as long as the formControl is a string.. correct?

    2. When I put sender.text() I get an error that the method is not defined that's why I used valueStr. And my control is a string as well. I think to be able use text I need define formControlStr sth = sender

    Then use sth.text()..right?

    3. Again do u want me to compare string wtih "" or is there a function for empty strings? Cause I think i might get a BP warning that this "" is not a label.

    4. As for the caseDetailBase...this table is currently empty in my environment

    And as I said, the control I'm modifying doesn't relate to any datasource and that's why formdatasource in my code was empty and therefore caseDetailBase was not found but I was still able to assign the recId to zero. I still didn't understand why it worked???

    5. Also why the form had caseDetailBase table and in referenced dataSources there was the CaseHirearchyDetails tables?  What does references dataSources do??? And why wasn't the hirearchy at the top since caseDetailBase can be empty?

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

    1. I would use text(), but you'll get the same result with valueStr() from string controls.

    2. It means that your variable isn't declared as FormStringControl.

    3. Use '' instead of "". Double quotes are used for labels.

    4. I already disproved your statement that CaseCategoryHierarchyDetail_CaseCategory isn't bound to any data source.

    5. Reference data sources and joined data sources used for reference controls.

  • .. Profile Picture
    1,965 on at

    4 & 5 i didn't say that CaseCategoryHierarchyDetail_CaseCategory isn't bound to any data source.

    I said that the modified event handler I made is for a string control that is not bound to any datasource "AACaseCategory2_OnModified" so is that why formdatasource was empty in my code? and also caseDetailBase was not found but I was still able to assign the recId to zero. I still didn't understand why it worked???

        /// 
        ///
        /// 
        /// 
        /// 
        [FormControlEventHandler(formControlStr(CaseDetailCreate, AACategoryType), FormControlEventType::Modified)]
        public static void AACategoryType_OnModified(FormControl sender, FormControlEventArgs e)
        {
            FormStringControl caseCategory1 = sender.formRun().design().controlName(formControlStr(CaseDetailCreate, AACaseCategory1)) as FormStringControl;
          
    
            if(sender.valueStr() != null)
            {
    
                ///
    
            }
            else
            {
                ///
            }
    
            FormRun formRun = sender.formRun();
            formDataSource formDataSource = formRun.dataSource();  // empty
            CaseDetailBase caseDetailBase = formDataSource.cursor();//empty
            caseDetailBase.CategoryRecId = 0;
            formDataSource.object(fieldNum(CaseDetailBase, CategoryRecId)).modified();
    
        }


    Also why the form had caseDetailBase table and in referenced dataSources there was the CaseHirearchyDetails tables?  What does references dataSources do??? And why wasn't the hirearchy at the top since caseDetailBase can be empty?

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

    Whether the sender control is bound or unbound is irrelevant, because you never work with such information. You use it just to get a reference to the current form. You seem to be believe it has something to do with the value of the data source, but these are two completely unrelated things and therefore your question makes no sense.

    Then the cursor is emoty means that there is no existing record as the current record of the data source. The form is called "CaseDetailCreate", so it's likely because there is a newly created record.

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

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 307 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans