Monday, April 6, 2015

X++ How to create a lookup which stores data from column A, but show data from column B

Hi everybody. This is a very simple lesson for dev. Most of times we'd like to have a simple "Lookup" which store a field data but display another field on the control.

The techniques we need are Edit method, lookup and jumpRef.

Let us learn this lesson through the example scenario, at module Product information management. I will add some characteristics of the soccer ball on it.

The final result would looks like below.


























Step#1
- Create Base Enum.
- Create EDT.
- Create table "BrandCategory" also added its index.
- Fill data into the table. 



















Then add fields in table EcoResProduct and modify form EcoResProductDetails.










At the end of this step, you can see that we can enter the data on the new fields of EcoResProduct but no lookup, just a simple textbox.















Step#2
- Add relation on table EcoResProduct
- Modify autolookup on table BrandCategory





























At the end of this step, we got the lookup! Great, but still display the number of value.












































Step#3
From step#2 you see that lookup works however our main purpose is to display another value on the control so we need "Edit method" here.

- Create a find method on table BrandCategory.
- Create an edit method on table EcoResProduct.








public edit BrandNumber editBallBrandName(boolean _set, BrandNumber _value)
{
    if (_set)
        this.BallBrandName = _value;

    return  BrandCategory::find(BrandTypeBase::Name, this.BallBrandName).BrandName;
}

then change the property on the control
Before change






















After change
























- Create method lookup on the control.

















public void lookup()
{
    SysTableLookup sysTableLookup = SysTableLookup::newParameters(tablenum(BrandCategory), this);

    Query query = new Query();
    QueryBuildDataSource qbds;

    ;

    sysTableLookup.addLookupField(fieldnum(BrandCategory, BrandNumber), true);
    sysTableLookup.addLookupField(fieldnum(BrandCategory, BrandName));

    qbds = query.addDataSource(tablenum(BrandCategory));

    qbds.addRange(fieldnum(BrandCategory, BrandType)).value(queryValue(BrandTypeBase::Name));

    sysTableLookup.parmQuery(query);

    sysTableLookup.performFormLookup();
}

OK, at the end of step3, the result looks good. We have both edit method and lookup which enable us to store a field and show another field. 
























Unfortunately, we can't stop here, because users note that when they do right-click, the "view details" menu disappear. 





















This is because we use datamethod instead of datafield so we have to add somethings.


Step#4 (The last one)
- Create a simple form BrandCategoryFrm for table BrandCategory.
- Add method init on that form.













public void init()
{
    BrandCategory    localBrandCategory;
    ;

    super();

    if (element.args().caller())
    {
        if (element.args().caller() is FormRun)
        {
            callerForm = element.args().caller();
            if (callerForm.name() == formstr(EcoResProductDetails))
            {
                if (element.args())
                {
                    callerRecord = element.args().record();

                    if(callerRecord.TableId == tableNum(BrandCategory))
                    {
                        localBrandCategory = callerRecord;
                        BrandCategory_ds.query().dataSourceTable(tableNum(BrandCategory)).addRange(fieldNum(BrandCategory, BrandType)).value(queryValue(localBrandCategory.BrandType));
                    }
                }
            }
        }
    }
}


- Then add method jumpRef on the control.

















public void jumpRef()
{
    //super();

    Args             args;
    MenuFunction     menuFunction;
    BrandCategory    brandCategory;
    ;

    brandCategory = BrandCategory::find(BrandTypeBase::Name, EcoResProduct.BallBrandName);
    if (!BrandCategory)
        return;

    args = new Args();
    args.caller(element);
    args.lookupRecord(BrandCategory);
    args.record(BrandCategory);

    menuFunction = new MenuFunction(menuitemDisplayStr(BrandCategoryMnu), MenuItemType::Display);
    menuFunction.run(args);
}


Finally, the things looks ok. 




















Thank you for your reading. Have a good day!
Shoot

No comments:

Post a Comment