web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

How to use lookup form in UI Builder and get RecId

(0) ShareShare
ReportReport
Posted on by 65

This is my UI Builder
pastedimage1575366405709v1.png
Is it possible to get the RecID of this?

Contract

[
DataContractAttribute,
SysOperationContractProcessingAttribute(classstr(AnnualBudgetUIBuilder))
]
class AnnualBudgetContract
{
    BudgetCycleName                 budgetCycle;

    [
         DataMemberAttribute('BudgetCycleName'),
        SysOperationLabel('Budget Cycle')
    ]
    public BudgetCycleName parmBudgetCycle(BudgetCycleName _budgetCycle = budgetCycle)
    {
        budgetCycle = _budgetCycle;
        return budgetCycle;
    }

}

UI Builder

class AnnualBudgetUIBuilder  extends SysOperationAutomaticUIBuilder
{
    AnnualBudgetContract contract;
    DialogField dialogBudgetCycle;

    public void build()
    {
        DialogGroup dlgGrp;

        //get the current dialog
        Dialog      dlg			= this.dialog();
        dlgGrp					= dlg.addGroup("");
        dlgGrp.columns(2);
        contract				= this.dataContractObject();
        
        dialogBudgetCycle       = this.addDialogField(methodStr(AnnualBudgetContract, parmBudgetCycle), contract);

    }

    public void postBuild()
    {
        super();
        dialogBudgetCycle.registerOverrideMethod(methodStr(FormStringControl, lookup), methodStr(AnnualBudgetUIBuilder, budgetCycleLookup), this);
        dialogBudgetCycle.registerOverrideMethod(methodStr((FormStringControl, modified), methodStr(AnnualBudgetUIBuilder, budgetCycleModified), this);
    }


    private void budgetCycleLookup(FormStringControl _lookup)
    {
        Query query = new Query();
        QueryBuildDataSource qbds;
        SysTableLookup sysTableLookup;

        if(_lookup != null)
        {
            sysTableLookup = SysTableLookup::newParameters(tableNum(BudgetCycle), _lookup);
             
            qbds = query.addDataSource(tableNum(BudgetCycle));

            qbds.addSortField(FieldNum(BudgetCycle, Name),SortOrder::Ascending);

            qbds.fields().addField(fieldNum(BudgetCycle, StartFiscalCalendarPeriod));
            qbds.fields().addField(fieldNum(BudgetCycle, EndFiscalCalendarPeriod));

            //sysTableLookup.addLookupField(fieldNum(BudgetCycle, RecId), True);
            sysTableLookup.addLookupField(fieldNum(BudgetCycle, Name), True);
            sysTableLookup.addLookupMethod(tableMethodStr(BudgetCycle, editCycleStartDate));
            sysTableLookup.addLookupMethod(tableMethodStr(BudgetCycle, editCycleEndDate));
            sysTableLookup.parmUseLookupValue(false);

            sysTableLookup.parmQuery(query);
            sysTableLookup.performFormLookup();
            
        }
    }

    public boolean budgetCycleModified(FormStringControl _budgetCycle)
    {
        boolean ret = false;
        BudgetCycle budgetCycle;

        if(dialogBudgetCycle.value())
        {
            info(strFmt("%1", dialogBudgetCycle.value()));
            ret = true;
        }
        return ret;
    }

}

I saw this post from stackoverflow and that's why I'm asking how to use lookup form in the UI Builder. Am I on the right track?
https://stackoverflow.com/questions/33369015/how-to-get-recid-selected-from-lookup-method

  • Suggested answer
    Martin Dráb Profile Picture
    241,509 Most Valuable Professional on at

    You use budget cycle name, therefore you get the name. You could find BudgetCycle record from the name, but since names aren't enforced to be unique, it doesn't sound like a good idea.

    Therefore you must change what you ask for. You want RecId instead of the name, therefore use RecId in your data contract. Create a new EDT extended RefRecId and set Reference Table property to BudgetCycle. Then use this EDT in your contract.

  • jerrold-verzosa Profile Picture
    65 on at

    How can I edit the value on the FormReferenceGroupControl?

    I just want to show the Name in the field.
    pastedimage1575383533101v1.png

    Also tried changing my existing modified method to.

        public boolean budgetCycleModified(FormReferenceGroupControl _budgetCycle)
        {
            boolean ret = false;
            BudgetCycle budgetCycle;
    
            if(dialogBudgetCycle.value())
            {
                info(strFmt("%1", dialogBudgetCycle.value()));
                dialogBudgetCycle.value(strFmt("%1", BudgetCycle::find(dialogBudgetCycle.value()).Name));
                
                ret = true;
            }
            return ret;
        }

    and nothing happens.

    Thanks for the help.

  • Martin Dráb Profile Picture
    241,509 Most Valuable Professional on at

    What are you trying to achieve by your code? It seems that you're still trying to work with Name, although I don't know why. You also use dialogBudgetCycle.value() once as if it was a record ID (in BudgetCycle::find()) and once as if it was a string (when you're assigning the name into it). It's definitely a bug, because it may be either int64 or string, but not both.

    Regarding how to show different fields, you can replacementFieldGroup() method or override lookupReference().

  • jerrold-verzosa Profile Picture
    65 on at

    Hi Martin,

    My bad, didn't include other part of my code. I created custom EDT and followed your instructions, it works. Tried to do info() in modified and it shows RecId.
    Now my problem is when I try to click my lookup, my modified method doesn't work. I want to set the budgetCycleDialog value to Name.

    This is my updated code.

    My parmBudgetCycle returns my custom RecId

        public void build()
        {
            DialogGroup dlgGrp;
    
            //get the current dialog
            Dialog      dlg			= this.dialog();
            dlgGrp					= dlg.addGroup("");
            dlgGrp.columns(2);
            contract				= this.dataContractObject();
            
            dialogBudgetCycle       = this.addDialogField(methodStr(AnnualBudgetContract, parmBudgetCycle), contract);
    
        }

    build()

        public void postBuild()
        {
            super();
            dialogBudgetCycle.registerOverrideMethod(methodStr(FormReferenceGroupControl, lookup), methodStr(AnnualBudgetUIBuilder, budgetCycleLookup), this);
            dialogBudgetCycle.registerOverrideMethod(methodStr(FormReferenceGroupControl, modified), methodStr(AnnualBudgetUIBuilder, budgetCycleModified), this);
        }
    

    budgetCycleLookup

        private void budgetCycleLookup(FormReferenceGroupControl _lookup)
        {
            Query query = new Query();
            QueryBuildDataSource qbds;
            SysReferenceTableLookup sysTableReferenceLookup;
    
            if(_lookup != null)
            {
                sysTableReferenceLookup = SysReferenceTableLookup::newParameters(tableNum(BudgetCycle), _lookup);
                
                qbds = query.addDataSource(tableNum(BudgetCycle));
    
                qbds.addSortField(FieldNum(BudgetCycle, Name),SortOrder::Ascending);
    
                qbds.fields().addField(fieldNum(BudgetCycle, Name));
                qbds.fields().addField(fieldNum(BudgetCycle, StartFiscalCalendarPeriod));
                qbds.fields().addField(fieldNum(BudgetCycle, EndFiscalCalendarPeriod));
    
                sysTableReferenceLookup.addLookupField(fieldNum(BudgetCycle, Name));
                sysTableReferenceLookup.addLookupMethod(tableMethodStr(BudgetCycle, editCycleStartDate));
                sysTableReferenceLookup.addLookupMethod(tableMethodStr(BudgetCycle, editCycleEndDate));
                //sysTableLookup.parmUseLookupValue(false);
    
                sysTableReferenceLookup.parmQuery(query);
                sysTableReferenceLookup.performFormLookup();
                
            }
        }

    I just want to set my budgetCycleDialog value to BudgetCycleName when I click the dropdown.
    Example, when I click FY2015, I just want to see the FY2015 on the texbox.

    pastedimage1575428363826v1.png

  • Martin Dráb Profile Picture
    241,509 Most Valuable Professional on at

    I'm still not getting what you're trying to achieve. We discussed how you can get the system do the all work for you - you'll get the Name displayed in UI and RecId stroed in the data contract.

    But now you have code adding an additional dialog field and you're saying that you want to attach Name to that control? Does it mean that you want two fields showing the name? But it would still makes no sense overriding lookup() on the string control, because as we discussed on the very beginning, it isn't enough to identify RecId.

    In my opinion, you should throw away everything working with strings and you should use RecId and ReferenceGroup control as discussed above.

  • jerrold-verzosa Profile Picture
    65 on at

    Let's be clear. I already did the custom EDT. And yes the system did all the work for me but the other thing I want to do is to modify the text field once you click the BudgetCycle.


    Here's my custom EDT

    pastedimage1575440174239v1.png

    pastedimage1575440191382v2.png

        [
             DataMemberAttribute('BudgetCycleRecIdCustom'),
            SysOperationLabel('Budget Cycle')
        ]
        public BudgetCycleRecIdCustom parmBudgetCycle(BudgetCycleRecIdCustom _budgetCycle = budgetCycle)
        {
            budgetCycle = _budgetCycle;
            return budgetCycle;
        }


    Here is what is looks like without the

    dialogBudgetCycle.registerOverrideMethod(methodStr(FormReferenceGroupControl, lookup), methodStr(AnnualBudgetUIBuilder, budgetCycleLookup), this);

    pastedimage1575440396821v3.png

    This is the dialog with the lookup override

    dialogBudgetCycle.registerOverrideMethod(methodStr(FormReferenceGroupControl, lookup), methodStr(AnnualBudgetUIBuilder, budgetCycleLookup), this);

    and without the

    dialogBudgetCycle.registerOverrideMethod(methodStr(FormReferenceGroupControl, modified), methodStr(AnnualBudgetUIBuilder, budgetCycleModified), this);

    pastedimage1575440656946v4.png


    Now, what I'm trying to do is. I want to set the value of the dialogBudgetCycle to Name(Like FY2015) by using the method below

    public boolean budgetCycleModified(FormReferenceGroupControl _budgetCycle)
        {
            boolean ret = false;
            BudgetCycle budgetCycle;
    
            if(dialogBudgetCycle.value())
            {
                info(strFmt("%1", dialogBudgetCycle.value()));
                dialogBudgetCycle.value(strFmt("%1", BudgetCycle::find(dialogBudgetCycle.value()).Name));
                
                ret = true;
            }
            return ret;
        }

    Here's what my UI Builder looks like with this code below.

    dialogBudgetCycle.registerOverrideMethod(methodStr(FormReferenceGroupControl, modified), methodStr(AnnualBudgetUIBuilder, budgetCycleModified), this);

    pastedimage1575441152383v5.png

    My BudgetCycle field is empty once you click something in the lookup and as you can see the Info log has the RecId already.
    I'm trying to override the modified in dialogBudgetCycle to show only the Name once you click on the lookup.
    pastedimage1575441430777v6.png

    Is it possible?

    Hope this is more clear now. Thanks for the help

  • Verified answer
    Martin Dráb Profile Picture
    241,509 Most Valuable Professional on at

    No, it's not possible. The value of the reference group control is the unique record ID (int64 number) and a string value (such as "FY2015") isn't a record ID. You would have to use a record ID instead.

    If Name was unique, you could find the record ID corresponding to the Name and use this ID, but as I mentioned before, the Name isn't guaranteed to be unique and therefore it's not safe.

    Nevetheless you already have the right value there, therefore even if your code was correct, it wouldn't have any effect.

    Let me examine your code:

    dialogBudgetCycle.value(strFmt("%1", BudgetCycle::find(dialogBudgetCycle.value()).Name));

    First you get the record ID of the selected BudgetCycle record:

    dialogBudgetCycle.value()

    As this point, you already have the record ID.

    Then you find the whole BudgetCycle record from its RecId and get Name:

    BudgetCycle::find(...).Name

    If you get the right Name, it proves that dialogBudgetCycle.value() contains the right record ID and there is nothing else to do.

    But then you seemed to forget that dialogBudgetCycle.value() is the record ID of BudgetCycle table and you're trying to assign a name into it:

    dialogBudgetCycle.value(budgetCycle.Name))

    It's a wrong type, it's a logically wrong value and we've proven that dialogBudgetCycle.value() already contains the right value, therefore there is no reason for changing it.

    If you have a problem with the lookup, I suggest you start by using the right method (lookupReference() instead of lookup()). The documentation page How to: Add a Run-Time Lookup Form > To Add a Run-Time Lookup Form for a Surrogate Foreign Key Field might help you.

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

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

#1
Martin Dráb Profile Picture

Martin Dráb 536 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 320 Super User 2026 Season 2

#3
CU-1234529-002 Profile Picture

CU-1234529-002 210

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans