initializing variable with select in init method using x++ - axapta

I'm new with X++ and I'm trying to modify the INIT method of a form in order to greet the user wit a message on top of the form.
The greeting message should look like "Happy Birthday EmplTable.name!".
The code from the INIT METHOD looks like this till now:
public void init()
{
CustName custName = SELECT EmplTable.name FROM Empltabe JOIN UserLogInfo WHERE EmplTable.EmplId == UserInfo.UserId;
;
//"#NET4183"
super();
GreetingMessage.text(strfmt("#NET4183", custName));
}
I have a hard time understanding what is wrong here and why I can't initialize the custName variable.
Thank for the help!
Have a great day!

All variables must be declared before they can be used. X++ does not allow variable declarations to be mixed with other X++ statements; variables must be declared before X++ statements.
Declaration of Variables
You should declare variables EmplTable, UserInfo before you can use them in select statement.
Results of a select statement are returned in a table buffer variable. If you use a field list in the select statement, only those fields are available in the table variable.
You can assign value to your custName variable using this peace of code
custName = emplTable.name;
This link will give you a hint how to find the current user
curUserId Function

Related

Passing parameters to parameter dialog for Query-based reports

I have a query-based report in which the query has some interactive ranges enabled on them. This is great except the value is blank, or has the last values used pre-populated. One of these is Vendor account number. If I wanted to have this report to pre-populate the Vend account based on whichever Vendor account record is selected (the caller), how would I be able to achieve this?
The answer was easy, although hard to find. I wasn't aware that you could access query objects from within a controller. The solution is to create a Controller class with only a main() method defined as normal, and the prePromptModifyContract method overridden. The following code will solve the problem:
SomeTable someTable;
Query query;
super();
if (this.parmArgs() && this.parmArgs().dataset() == tableNum(SomeTable))
{
someTable = this.parmArgs().record();
query = this.getFirstQuery();
SysQuery::findOrCreateRange(query.dataSourceTable(tableNum(SomeOtherTable)), fieldNum(SomeOtherTable, SomeOtherField)).value(SysQuery::value(someTable.SomeField));
}
I haven't tried this, but you could override the query's init method and call to element.args().record() as in a Form.
Something like this:
public void init()
{
VendTable vendTable;
super();
if (element.args().dataset() == tableNum(VendTable))
{
vendTable = element.args().record();
//populate your ranges with vendTable
}
}
I hope it works!

Symbol was not found when using tmpaccountsum variable in form

In the ReqTransPO form I want to use the tmpAccountSum table to store some values in which I later need to filter my grid.
In the classdeclaration I declared:
tmpAccountSum mytable;
In a helper method I have:
//Store data in tmptable
ttsbegin;
mytable.AccountNum = _reqTrans.RefId;
mytable.Txt = _reqPo.RefId;
mytable.Voucher = enum2str(_prodstatus);
mytable.insert();
ttscommit;
I don't get an error on 'mytable' but can't really use it / it's not initialized. If I add a watch in debug I see:
Error: Symbol "mytable" was not found
If I declare mytable locally it works ok.
Guess I'm doing something wrong?
Thanks in advance,
Mike
My extrasensory perception tells me, that your helper method is static.
Static methods do not have access to instance variables declared in classDeclaration.

Magic Casting to collection of results

In the Simple.Data examples, there is an example of 'Magic Casting':
// When you assign the dynamic record to a static type, any matching properties are auto-mapped.
var db = Database.Open();
Customer customer = db.Customers.FindByCustomerId(1);
Does Simple.Data also magically cast if there are multiple records returned? Something like this:
var db = Database.Open();
IEnumerable<Customer> customers = db.Customers.FindBySurname("Smith");
Obviously I have tried the above and it doesn't work ("Cannot implicitly convert type" from SimpleQuery to my concrete type). Any advice would be welcome.
FindBySurname returns a single record. If you use FindAllBySurname you'll get an enumerable, which should magic cast OK. (If for some reason it doesn't, you can call .Cast() on it.)

Possible to assign a data type to an anonymous type's members in a linq query?

If I have a linq query that creates the anonymous type below:
select new
{
lf.id,
lf.name,
lf.desc,
plf.childId
};
Is it possible to assign a specific type to one of the members? Specifically I would like to make lf.id a null-able int rather than an int...
Not sure if this would work without trying, but could you do this:
select new
{
(int?)lf.id,
}
To force the cast?
Edit: looks like no, but I was able to do this:
List<int> il = new List<int>(){1,2,3};
var z = from i in il.AsQueryable<int>()
select new
{
Foo = (int?)i
};
And that worked fine.
That may not work. In anonymous types, the data type of the property is used. So in the new type, the data type of id would be used to create a property id in the new type.
If the id property in 'lf' is nullable, then you should be able to have it nullable in the anonymous type as well.

Birt Global Integer

I have a Birt Report which read some stuff from a database.
After that i want to increment a global Integer for every Detailrow that is loaded.
So far i have initialized a global Integer with the following lines:
importPackage(Packages.java.lang);
reportContext.setPersistentGlobalVariable("minTotalPlus", new Integer(0));
After that i added the following line into a field in my detail row:
reportContext.setGlobalVariable("minTotalPlus", new Integer reportContext.getGlobalVariable("minTotalPlus")) + 1);
When i preview the report i get an "java.lang.NumberFormatException: null" which means that the global variable is null. Why is that so? How could I fix that?
Dont' declare variables like that in the initialize method
declare the like the following
materiales=0;
tools=0;
then in the fetch method
use the following
tools++;
...etc.

Resources