Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Suggested answer

object reference is not set to an instance of an object

(0) ShareShare
ReportReport
Posted on by 1,550

Hi,

I'm trying to assign the value of a formReferenceGroupControl

FormReferenceGroupControl name = sender.formRun().design().controlName(formControlStr(CaseDetailCreate, DirPartyTable_Name)) as FormReferenceGroupControl;
HcmWorker hcmWorker = HcmWorker::findByPersonnelNumber(conPeek(con,counter));  -- hcmWorker has a value
name.value(DirPartyTable::findRec(hcmWorker.Person).RecId); //it fails here

  • Martin Dráb Profile Picture
    Martin Dráb 230,848 Most Valuable Professional on at
    RE: object reference is not set to an instance of an object

    Well, you may want it, but it won't work. So maybe you should consider a design that can actually do what you need.

  • junior AX Profile Picture
    junior AX 1,550 on at
    RE: object reference is not set to an instance of an object

    Hi Martin,

    But caseDetail form already has dirPartyTable_name control...and if you manually create the case by filling the name field in the caseDetail form..when u click ok, then you can see that the same value is also appearing in caseDetailCreate form.

    So what I want to do is fill the name field in caseDetail by code instead of manually filling it..so that at the end it appears in caseDetailCreate

  • Martin Dráb Profile Picture
    Martin Dráb 230,848 Most Valuable Professional on at
    RE: object reference is not set to an instance of an object

    It's nice that it opens CaseDetail, but that's not the form where you're setting the value. You're using DirPartyTable control of CaseDetailCreate form.

    I think you shouldn't play with form controls at all; you should write a value to DirPartyTable field of CaseDetailBase data source. And should you should do it when saving the record, or sooner.

  • junior AX Profile Picture
    junior AX 1,550 on at
    RE: object reference is not set to an instance of an object

    Hi Martin,

    here's the full code. When the button for "CaseDetailCreate" form closes it opens the caseDetail Form

    The worker field is filled 


     

    [FormControlEventHandler(formControlStr(CaseDetailCreate, OK), FormControlEventType::Clicked)]
    public static void OK_OnClicked(FormControl sender, FormControlEventArgs e)
    {
    
        FormStringControl worker = sender.formRun().design().controlName(formControlStr(CaseDetailCreate, XXWorker)) as FormStringControl;
        FormReferenceGroupControl caseCategory = sender.formRun().design().controlName(formControlStr(CaseDetailCreate, CaseCategoryHierarchyDetail_CaseCategory)) as FormReferenceGroupControl;
        FormReferenceGroupControl name = sender.formRun().design().controlName(formControlStr(CaseDetailCreate, DirPartyTable)) as FormReferenceGroupControl;
        
        FormRun formRun = sender.formRun();
        FormDataSource formDataSource = formRun.dataSource(formDataSourceStr(CaseDetailCreate, CaseDetailBase));
        CaseDetailBase caseDetailBase = formDataSource.cursor();
        CaseAssociation caseAssociation;
        
        if(worker.text() != '')
        {
            Container con;
            SysLookupMultiSelectCtrl multiSelectCtrl = formRun.XXMultiSelectCtrl();
            con = multiSelectCtrl.getSelectedFieldValues();
            int counter;
            for(counter=1; counter <= conlen(con); counter  )
            {
                caseAssociation.EntityType = CaseEntityType::Employee;
                caseAssociation.CaseRecId = caseDetailBase.RecId;
                HcmWorker hcmWorker = HcmWorker::findByPersonnelNumber(conPeek(con,counter));
                caseAssociation.RefRecId = hcmWorker.RecId;
                
                CaseCategoryHierarchyDetail caseCategoryHierarchyDetail;
                select firstonly caseCategoryHierarchyDetail where caseCategoryHierarchyDetail.RecId == caseCategory.value()
                    && caseCategoryHierarchyDetail.XXIsPrimary == NoYes::Yes;
                if(caseCategoryHierarchyDetail)
                {
                    caseAssociation.IsPrimary = NoYes::Yes;
                    name.value(DirPartyTable::findRec(hcmWorker.Person).RecId);
                }
                
                caseAssociation.insert();
            }
        
        }
    }


    As you can see Worker assocation is filled... but the name is not filled

    pastedimage1678450537335v1.png

  • Martin Dráb Profile Picture
    Martin Dráb 230,848 Most Valuable Professional on at
    RE: object reference is not set to an instance of an object

    Yes, this code now access the right control and sets a value there.

    But it doesn't mean that it makes sense. The button closes the form, doesn't it? So how you can whether the value was set or not, if the form is closed and you can't see anything?

    Also, we can't know whether your code finds any worker.

  • junior AX Profile Picture
    junior AX 1,550 on at
    RE: object reference is not set to an instance of an object

    Hi Maritn,

    You mean you want it like this?  -- it's still not working, the value is not filled in the form

    [FormControlEventHandler(formControlStr(CaseDetailCreate, OK), FormControlEventType::Clicked)]
    public static void OK_OnClicked(FormControl sender, FormControlEventArgs e)
    {
        FormReferenceGroupControl name = sender.formRun().design().controlName(formControlStr(CaseDetailCreate, DirPartyTable)) as FormReferenceGroupControl;
        //more logic
        HcmWorker hcmWorker = HcmWorker::findByPersonnelNumber(conPeek(con,counter)); -- hcmWorker has a value
        name.value(DirPartyTable::findRec(hcmWorker.Person).RecId);
    }

  • Martin Dráb Profile Picture
    Martin Dráb 230,848 Most Valuable Professional on at
    RE: object reference is not set to an instance of an object

    No, you weren't. You were using the same control as now, DirPartyTable_Name, which isn't a reference group control. It's a string edit control.

    As I said in my first reply, the reference group control is called DirPartyTable.

  • junior AX Profile Picture
    junior AX 1,550 on at
    RE: object reference is not set to an instance of an object

    Hi Martin,

    But at first I was using formReferenceGroupControl and then you said it's wrong.

    Can you please explain more what I should do...maybe I misunderstood you

  • Martin Dráb Profile Picture
    Martin Dráb 230,848 Most Valuable Professional on at
    RE: object reference is not set to an instance of an object

    I think that trying to set the name directly isn't a good idea. You should set the value of the reference group control.

  • junior AX Profile Picture
    junior AX 1,550 on at
    RE: object reference is not set to an instance of an object

    Hi Mohit and Martin,

    I tried this but the value is still not set in the form

    [FormControlEventHandler(formControlStr(CaseDetailCreate, OK), FormControlEventType::Clicked)]
    public static void OK_OnClicked(FormControl sender, FormControlEventArgs e)
    {
        FormStringControl name = sender.formRun().design().controlName(formControlStr(CaseDetailCreate, DirPartyTable_Name)) as FormStringControl;
        //more logic
        HcmWorker hcmWorker = HcmWorker::findByPersonnelNumber(conPeek(con,counter)); -- hcmWorker has a value
        name.text(DirPartyTable::findRec(hcmWorker.Person).Name);
    }

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