Showing posts with label Add Custom lookup on EP. Show all posts
Showing posts with label Add Custom lookup on EP. Show all posts

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();
    }

Wednesday, April 22, 2020

Add Custom lookup on EP

The trick is to override datasetLookup() on Dataset > data source > field > methods. You will not find this method in the list of override methods so just create this as new method.
the table which we are using for lookup should be in the Dataset

public void dataSetLookup(SysDataSetLookup sysDataSetLookup)
{
    List list = new List(Types::String);

    Query query = new Query();

    QueryBuildDataSource    queryBuildDataSource;

    QueryBuildRange qbr;

    // Add the table to the query.

    queryBuildDataSource  = query.addDataSource(tableNum(HcmWorker));

    //add the range

    qbr = queryBuildDataSource.addRange( fieldnum(HcmWorker, PersonnelNumber));

   // qbr.value(DirPersonUser::currentWorkerPersonnelNumber());

   // qbr.status(RangeStatus::Locked);

     // Specify the fields to use for the lookup.

    list.addEnd(fieldStr(HcmWorker,PersonnelNumber));

    list.addEnd(fieldStr(HcmWorker,Name()));


     sysDataSetLookup.parmLookupFields(list);

     // Specify the field that is returned from the lookup.

    sysDataSetLookup.parmSelectField('PersonnelNumber');

     // Pass the query to the SysDataSetLookup so that the query is used.

    sysDataSetLookup.parmQuery(query);
 
}

Table browser URL in D365FO

Critical Thinking icon icon by Icons8