Wednesday, June 9, 2021

How to add reference fields in forms that don't have a data source

 

Create a temp table with reference fields you wanted 


Add the Temp table as a data source in the required from without any join source


To initialize values when opening form

    [FormDataSourceEventHandler(formDataSourceStr(PdsResetVendorBatchDetails, PdsVendBatchInfo), FormDataSourceEventType::Initialized)]
    public static void PdsVendBatchInfo_OnInitialized(FormDataSource sender, FormDataSourceEventArgs e)
    {
            FormRun formRun = sender.formRun() as FormRun;

            switch (formRun.args().dataset())
            {
                case tableNum(InventBatch) :

                    InventBatch  inventBatch = formRun.args().record() as InventBatch;

                    PdsVendBatchInfoTMP PdsVendBatchInfo1 = sender.cursor();


                    FormReferenceControl Manufact = formRun.design().controlName(formControlStr(PdsResetVendorBatchDetails, PdsVendBatchInfo_Manufact)) as FormReferenceControl;
                    FormReferenceControl ManufactSite = formRun.design().controlName(formControlStr(PdsResetVendorBatchDetails, PdsVendBatchInfo_ManufactSite)) as FormReferenceControl;
                
                    PdsVendBatchInfo1.Manufact = inventBatch.Manufact;
                    PdsVendBatchInfo1.ManufactSite = inventBatch.ManufactSite;

                    PdsVendBatchInfo1.doInsert();
                
                    sender.setRecord(PdsVendBatchInfo1);

                    Manufact.value(inventBatch.Manufact);
                    ManufactSite.value(inventBatch.ManufactSite);
                    break;
            }
    }

 

Lookup

public static void dirPartyLocationLookup(FormReferenceGroupControl _ctrl, DirPartyRecId _party)
    {
        container               conLocationRoles;
        LogisticsLocationRole   logisticsLocationRole;
        QueryBuildDataSource    qbdsDirPartyLocation, qbdsDirPartyLocationRole;

        Query   query = new Query();
        SysReferenceTableLookup sysTableLookup = SysReferenceTableLookup::newParameters(tablenum(DirPartyLocation), _ctrl);

        while select RecId from LogisticsLocationRole
            where LogisticsLocationRole.IsManufactSite
        {
            conLocationRoles += LogisticsLocationRole.RecId;
        }

        sysTableLookup.addLookupfield(fieldNum(DirPartyLocation, Location));
        sysTableLookup.addLookupfield(fieldNum(DirPartyLocation, Party));
        
        qbdsDirPartyLocation = query.addDataSource(tablenum(DirPartyLocation));
        
        qbdsDirPartyLocationRole = qbdsDirPartyLocation.addDataSource(tablenum(DirPartyLocationRole));
        qbdsDirPartyLocationRole.joinMode(JoinMode::ExistsJoin);
        qbdsDirPartyLocationRole.relations(true);

        qbdsDirPartyLocationRole.addRange(fieldNum(DirPartyLocationRole, LocationRole)).value(con2Str(conLocationRoles));
       
        if(_party)
        {
            qbdsDirPartyLocation.addRange(fieldNum(DirPartyLocation, Party)).value(queryValue(_party));

            qbdsDirPartyLocation.addRange(fieldNum(DirPartyLocation, Location)).value(SysQuery::valueNot(0));
        }
        else
        {
            qbdsDirPartyLocation.addRange(fieldNum(DirPartyLocation, Party)).value(SysQuery::valueEmptyString());
        }

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

    public static void DirPartyIdLookup(FormControl _ctrl)
    {
        Query   query = new Query();

        SysReferenceTableLookup sysTableLookup = SysReferenceTableLookup::newParameters(tablenum(DirPartyTable), _ctrl);

        sysTableLookup.addLookupfield(fieldNum(DirPartyTable, PartyNumber));
        sysTableLookup.addLookupfield(fieldNum(DirPartyTable, Name));

        query.addDataSource(tablenum(DirPartyTable));
        
        sysTableLookup.parmQuery(query);
        sysTableLookup.performFormLookup();
    }

Table browser URL in D365FO

Critical Thinking icon icon by Icons8