Frequently asked questions

From iDempiere en
Jump to navigation Jump to search

Hacker's Questions

How to create automatic line numbers?

Q: I created a new kind of table with a subtable and I want automatic created line numbers like for example in orders or invoices.

Answer 1

See into the sourcecode at [org.adempiere.base/src/org/compiere/model/MInvoiceLine.java] in method beforeSave().

Answer 2

For my Examples it is said I created a table to calculate interests. The header table is called "BAY_InterestCalculation". The subtable is called "BAY_InterestCalculationLine".

I inserted the following code into my own model class [MBAYInterestCalculationLine].

 @Override
 protected boolean beforeSave(boolean newRecord) {
 	// Get Line No
 	if (getLine() == 0) {
 		String sql = "SELECT COALESCE(MAX(Line),0)+10 FROM " + Table_Name
 				+ " WHERE " + COLUMNNAME_BAY_InterestCalculation_ID + "=?";
 		int ii = DB.getSQLValue(get_TrxName(), sql,
 				getBAY_InterestCalculation_ID());
 		setLine(ii);
 	}
 
 	return super.beforeSave(newRecord);
 }


Answer 3

For my Examples it is said I created a table to calculate interests. The header table is called "BAY_InterestCalculation". The subtable is called "BAY_InterestCalculationLine".

In the window "Table & Column" I go to the definition of the "Line" column and give the following "standard logic":

 @SQL=SELECT NVL(MAX(Line),0)+10 AS DefaultValue FROM BAY_InterestCalculationLine WHERE BAY_InterestCalculation_ID=@BAY_InterestCalculation_ID@

This solution does not need an own model class. It works before the line is created in the database and so the new line number is shown in a new created line record. This may introduce run conditions when multi users are creating records. To be more sure that line numbers are unique you better use Answer 2 (This is how Jorg Janke did it). To be really sure you should add a suitable unique constraint on the database.

Nothing keeps you from combining both approaches. :-)

How to query objects with the Query class?

The Query class is the recommended way to fetch objects. It uses the persistence layer and its cache and it uses a translation layer to be database independent.

see http://www.adempiere.com/ADempiere_Best_Practices#How_to_use_Adempiere_Query_class.3F