How to get RecId selected from lookup method? - axapta

I want to get the RecId selected from lookup method?
In lookup method in StringEditLookup_ZipCode
public void lookup()
{
Query query = new Query();
QueryBuildDataSource queryBuildDataSource;
QueryBuildRange queryBuildRange;
SysTableLookup sysTableLookup = SysTableLookup::newParameters(tableNum(LogisticsAddressZipCode), this);
sysTableLookup.addLookupField(fieldNum(LogisticsAddressZipCode, ZipCode));
sysTableLookup.addLookupField(fieldNum(LogisticsAddressZipCode, City));
sysTableLookup.addSelectionField(fieldNum(LogisticsAddressZipCode, RecId));
queryBuildDataSource = query.addDataSource(tableNum(LogisticsAddressZipCode));
sysTableLookup.parmQuery(query);
sysTableLookup.performFormLookup();
//super();
}
In modified method I would want to get ad read and use the RecId taken.
I want to populate the StringEditLookup_ZipCode with the ZipCode value.
It's possible to take a RecID ?
The LogisticsAddressZipCode Table is's not indexed by ZipCode
for this I need to take the RecID.
There is a way to save in a global variable or somehow recid selected in the lookup method or another point?
Thanks all,
enjoy!

As far as I know this cannot be done with the SysTableLookup Framework. Basically you want your lookup to return two values, the ZipCode and the RecId. But the framework can only return one value.
So instead of the framework you will need to implement your own lookup by creating a new lookup form. From this form you can then retrieve the selected record in the lookup method. Here is some code to give you an idea how the lookup method could look like:
public void lookup()
{
FormRun lookupFormRun;
Args args;
LogisticsAddressZipCode myLookupZipCode;
args = new Args();
args.name(formStr(MyNewLookupForm));
lookupFormRun = classFactory.formRunClass(args);
lookupFormRun.init();
this.performFormLookup(lookupFormRun);
lookupFormRun.wait();
if (lookupFormRun.closedOk())
{
myLookupZipCode= formRun.docCursor();
}
}
From there you can save the RecId of myLookupZipCode to a class variable and then later use it in the modified method.
Also take a look at Lookup form returning more than one value for additional Information.

What you have there is exactly what I would've tried. What happens when you choose a value from the lookup? If that isn't working, I'd try adding RecId as one of the lookupFields. It's probably something where it isn't selecting the field so it's always blank. That's the first thing I would try.

Related

What should be the type parameter for List<T> and .Query<T> for joined table?

I am very new to Xamarin Forms and SQLite. I have the following method that returns a list from two joined tables. My problem is I don't know what is the right type parameter the List<T> and .Query<> should have to be able to get the values of both Category and Phrase table. Can anyone enlighten me on this one?
public List<?> GetWordsByCategory(int category)
{
lock (locker)
{
var words = databaseConnection
.Query<?>("Select Category.*, Phrase.*
From Category
Join Phrase on Category.Id = Phrase.CategoryId
Where Category.Id = 1")
.ToList();
return words;
}
}
I have tried List<Category> but would only return the properties of the Category table likewise List<Phrase>
You can return a dynamic or create a new class.
But I would suggest you to use the SQLite.Net PCL which is a wrapper around SQL and which enables you to query database like EF using linq and lamda than string queries.
You can look into this similar question which should help you.

How to filter by date and do a Form's query by code?

I have to do a Form query by code. The datasource table is CustomVendTable(is a custom table).
I open a form and in my init method I get the table caller :
public void init ()
{
VendTable = myVendTableCaller;
myVendTableCaller = element.args().record();
// There is a dialog and get a date by a _Dialog_ and save in a date variable
super();
}
In my data source I have a build a query. The table in my datasource is related whit VendTable. I filter by code my DataSource by myVendTableCaller.RecId and variable date dateByDialog inserted in the opening dialog
My query is this:
public void executeQuery()
{
query q = new Query();
QueryBuildRange qbr;
QueryBuildDataSource qbds ;
QueryRun queryRun;
qbds = q.addDataSource(tableNum(CustomVendTable) );
qbds.addRange(fieldNum(CustomVendTable, ValidFrom)).value(SysQuery::value( strFmt ("<=%1 ", _dateByDialog)) ) ;
qbds.addRange(fieldNum(CustomVendTable, ValidTo)).value(SysQuery::value( strFmt (">=%1 ", _dateByDialog))) ;
qbds.addRange(fieldNum(CustomVendTable, Vendor )).value(SysQuery::value(myVendTableCaller.recId));
queryRun = new QueryRun (q);
CustomVendTable_ds.query(queryRun.query());
super();
}
*For information there is a Table Relation CustomVendTable.Vendor == VendTable.RecId
So, I have some problems! I think not to be able to make a correct query by date. The fields ValidFrom - ValidTo are UTCdatetime type.
1) I have to convert my _dateByDialog in UTC ? How to do ? It' s correct my query date way ?
Considering that the conversion is not impossible, my BIG problem is that by filtering by date, if I have only one record with recid same range and dates I can somehow see it, BUT if there are more record with these same characteristics (If I have 2 record) I don't see nothing! My Form Grid is void!
I have read that you should set the Session date time (I'm talking about this ) to have control over dates.
I still believe that I do not build my Query very well.
Do you have an idea how can I do ?
If you used DateTimeUtil::newDateTime and made new UTCDateTime parameters for the dates?
https://community.dynamics.com/ax/b/alirazatechblog/archive/2012/09/03/date-to-utcdatetime-convertion-dynamics-ax-2012
Maybe you use a table which use date effective. You assume you have to do the selection yourself, this is not true.
Instead call method validTimeStateAsOfDate on the datasource. See this answer for details.

Selection on Lookup to display name on UI and select RecId ax 2012

I have unbound field on a form. Right now i am showing a look-up on that field using a look-up method which shows All distinct values from a field in a table. Now i changed that name field in table to Recid field. Now i need to display look-up with names but when i select one value i want to show name on U I but i have to select Recid. I don't want recid to display in look-up. How can i achieve that?
Before i used to get lookup of name using this method like this
public void lookup()
{
SysTableLookup sysTableLookup = SysTableLookup::newParameters(tablenum(RTTable), this);
Query query = new Query();
QueryBuildDataSource queryBuildDataSource;
;
sysTableLookup.addLookupfield(fieldnum(RTTable, AsstManager));
queryBuildDataSource = query.addDataSource(tablenum(RTTable));
queryBuildDataSource.addSortField(fieldnum(RTTable, AsstManager));
queryBuildDataSource.addRange(fieldNum(RTTable, AsstManager)).value(queryNotValue(''));
queryBuildDataSource.orderMode(ordermode::GroupBy);
sysTableLookup.parmQuery(query);
sysTableLookup.performFormLookup();
super();
}
You need .addSelectionField(...), and most likely to remove the super() line.
See these links for some examples:
https://daxbeginners.wordpress.com/tag/lookup-table/
http://dynamicsuser.net/forums/p/56634/303103.aspx
Ah, I forgot also you can specify the return field in the optional second argument of:
sysTableLookup.addLookupfield(fieldnum(RTTable, AsstManager), TRUE);
See second argument at https://msdn.microsoft.com/en-us/library/systablelookup.addlookupfield.aspx
You can do it by having a custom lookup form and using the CloseSelect() method on the form. There you can put in the code to return your recId

Make order by with Query in AOT

I have made a query in AOT. My goal is to print information using Group by "CustGroup" and Order by "count(RecId)" desc of CustTable table. The group by works properly, but the order by does not. I can not understand why...
This is what my query looks like:
This is code I use:
Static void Query(Args _args)
{
QueryRun qr;
CustTable myCustTable;
;
qr = new QueryRun(queryStr(MyQuery));
while(qr.next())
{
myCustTable = qr.get(tableNum(CustTable));
info(strFmt("Group %1 Num %2", myCustTable.Custgroup, myCustTable.RecId));
}
}
The result is:
AX does not sort by Count(RecId) but by the grouping.
You can solve your problem by dragging your query to a new view, then doing the sort on the count field of the view. You can also define the view without a query.

How to create a lookup with fields from more than one datasource?

I need to create Dynamic lookup in my form field which should display fields from two different datasources.. I am trying to perform it as:
public void lookup()
{
query = new Query();
sysTableLookup = SysTableLookup::newParameters(tableNum(smmBusRelTable), this);
qbds = query.addDataSource(tablenum(smmBusRelTable));
// qbds.addDataSource(tableNum(DirPartyTable));
//qbds.relations(true);
sysTableLookup.parmQuery(query);
sysTableLookup.addLookupField(fieldNum(smmBusRelTable, Busrelaccount));
//sysTableLookup.addLookupfield(fieldNum(DirPartyTable, Name));
sysTableLookup.performFormLookup();
}
Commented lines are the operation I am trying to perform to add different datasource.
As far as I know the SysTableLookup class does not support showing fields from other tables. The addLookup() method doesn't take a TableId and assumes all fields are in the first datasource of the query.
You could write your own version of SysTableLookup that supports referencing fields from various tables. A simpler and more practical (and less expensive) approach might be to create a display method on SmmBusRelTable to fetch the name from DirPartyTable (if it doesn't already exists) and use that as a field in the lookup. Display methods on the main table are supported if I remember correctly.
Depending on what exactly you're trying to accomplish there might be an even simpler way. You could add the display method to the AutoLookup table field group of SmmBusRelTable and avoid having to override the lookup() method.
It is actually very easy to combine multiple datasources in a sysTableLookup. Here is the trick I used to be able to filter on the name from the EcoResProductTranslation in the lookup for items.
1) Create a view that combines all your datasources and add the fields you would like to see in your lookup to the view.
2) Create a query from the view created in step 1.
3) Use these to perform your lookup as follows...
static client void lookupItemActive(FormStringControl _ctrl)
{
SysTableLookup sysTableLookup = SysTableLookup::newParameters(tablenum(<ViewName>),_ctrl);
Query query = new Query(queryStr(<QueryName>));
sysTableLookup.addLookupfield(fieldnum(<ViewName>, ItemId));
sysTableLookup.addLookupfield(fieldNum(<ViewName>, Name));
sysTableLookup.addLookupfield(fieldNum(<ViewName>, ItemGroupId));
sysTableLookup.addLookupfield(fieldnum(<ViewName>, Status));
sysTableLookup.addLookupfield(fieldnum(<ViewName>, RevId));
sysTableLookup.addLookupfield(fieldnum(<ViewName>, ItemType));
sysTableLookup.parmQuery(query);
sysTableLookup.performFormLookup();
}

Resources