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

object reference is not set to an instance of an object

(0) ShareShare
ReportReport
Posted on by 1,552

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

I have the same question (0)
  • Suggested answer
    Mohit Rampal Profile Picture
    12,565 Moderator on at
    RE: object reference is not set to an instance of an object

    Hi, I think its failing because you are passing RecId in the name field. I am assuming name here is String field.

    Try this code

    name.value(DirPartyTable::findRec(hcmWorker.Person).Name); 

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

    Hi Mohit,

    Name is a reference group as u can see from the code..so it's a recId but the form display it as string..so shall I use what you suggested?

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

    The title of the thread mentions a NullReferenceException. If that's what you're getting, you must find which exact place of your code is throwing it. When you know where you have null instead of an object reference, we can start talking about why and how to address it.

    Nevertheless I see the bug in this case. You're wrong in thinking that DirPartyTable_Name is a reference group. It's a string edit control; the reference group control is called DirPartyTable.

    When you use the 'as' operator to case DirPartyTable_Name control to FormReferenceGroupControl, you'll get null, because the types aren't compatible. And you'll get an exception when trying to call value() on null.

    You would have immediately seen the problem if you simply run your code in debugger, rather then going to a forum.

  • junior AX Profile Picture
    1,552 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);
    }

  • Martin Dráb Profile Picture
    237,697 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
    1,552 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
    237,697 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
    1,552 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
    237,697 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
    1,552 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

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

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 398 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans