3

I am creating a project in AX 2009 on a form. There is a lookup field called "SubsPersonName", which performs a lookup on the ContactPerson table using the following method.

public void lookup()
{
    boolean ret;
    Query                   query;
    QueryBuildDataSource    querybuilddatasource;
    QueryBuildRange         querybuildrange;
    SysTableLookup          sysTableLookup;
    CustInvoiceAccount      _thisinvoiceaccount     = CustTable::find(CustTable::find(TTN_CustVendSubscriptions.AccountNum).InvoiceAccount).PartyId;
    CustAccount             _thisaccountnum         = CustTable::find(TTN_CustVendSubscriptions.AccountNum).PartyId;
    ;

    //TODO: Lookup value should read ContactPerson.Name, not ContactPerson.ContactPersonId
    sysTableLookup = SysTableLookup::newParameters(tablenum(ContactPerson), this);
    sysTableLookup.addLookupfield(fieldnum(ContactPerson, Name));
    sysTableLookup.addLookupfield(fieldnum(ContactPerson, ContactPersonId), true);
    sysTableLookup.addLookupfield(fieldnum(ContactPerson, CustAccount));
    sysTableLookup.addLookupfield(fieldnum(ContactPerson, VendAccount));
    query = new Query();
    querybuilddatasource = query.addDataSource(tablenum(ContactPerson));
    querybuildrange = querybuilddatasource.addRange(fieldnum(ContactPerson, Name));
    querybuildrange = querybuilddatasource.addRange(fieldnum(ContactPerson, OrgPartyId));
    if (!_thisinvoiceaccount)
        _thisinvoiceaccount = _thisaccountnum;
    querybuildrange.value(strfmt("%1,%2", _thisinvoiceaccount,_thisaccountnum));
    sysTableLookup.parmQuery(query);
    sysTableLookup.performFormLookup();
}

The code above works fine, but I need to adjust it slightly. The value that needs to be inserted into the database is ContactPerson.ContactPersonId as per this code, but I would like the field in the form to display ContactPerson.Name.

I realise that I can use a display method to show this, as per this question: Axapta: Lookup field display the string value instead of the ID?, but that will involve an unnecessary extra field, which I'd like to avoid if possible.

Please could someone point me in the right direction?

Community
  • 1
  • 1
Phil Matthews
  • 31
  • 1
  • 3

2 Answers2

3

In order to make the ContactPersonId field invisible in the lookup, but still keep it as the return value, change the class SysTableLookup method:

void addLookupfield(fieldId _fieldId, boolean _returnItem = false, boolean _visible = true)
{
   lookupItems += [[_fieldId, _returnItem, '', _visible]];
}

Also change the buildGrid method to change the field visibility.

Then change your code like this:

sysTableLookup.addLookupfield(fieldnum(ContactPerson, ContactPersonId), true, false)

See also Axaptapedia.

Jan B. Kjeldsen
  • 17,817
  • 5
  • 32
  • 50
0

If you refer to hiding the ContactPersonId in the calling form then have a look at the CustTable form in the "Contact" tab page.

As stated in the forms ClassDeclaration:

The field contactpersonId is hiddeen on the form design, but must be avalible in order to make it possible to have lookup on contact name

The field editContactPersonName has a lookup referring to the hidden contactpersonId field:

void lookup()
{;
    ContactPerson::lookupCustContactPerson(contactPersonId, custTable.AccountNum, this, custTable.ContactPersonId);
}
Jan B. Kjeldsen
  • 17,817
  • 5
  • 32
  • 50