cancel
Showing results for 
Search instead for 
Did you mean: 

Java API lookup field problem

Former Member
0 Kudos

Hi All,

I am facing problem in retrieving data from lookup field bellow is the code I have written and its printing <b>null (R1)</b> here R1 is the first record selected from look up table but its not printing the display filed value can any one give me the suggest me anything for this.

This the code I am using.

		// The table to be searched on. Name hard coded ...
		TableId mainTableId = repository.getTableId("ninad");
		//TableId catTableId = repository.getTableId("Categories");
	
		// Specify the result definition (what to retrieve)
		ResultDefinition rd = new ResultDefinition(mainTableId);
		
		// And add one field to get the value of (Again, hard coded name...)
		rd.setSelectFields(new FieldId[]
		{ repository.getFieldId("ninad", "Name") });
		rd.addSelectField(repository.getFieldId("ninad", "phone"));
		rd.addSelectField(repository.getFieldId("ninad", "Category"));
		
	
		
		// Instantiate a search, to whoch search criteria can be added
		Search search = new Search(mainTableId);
		//search.addSearchItem(searchItem)
		
		// In this example no criteria are added, thus the search returns all
		// records of the search table

		// Retrieve the records with the command RetrieveLimitedRecordsCommand
		RetrieveLimitedRecordsCommand retrieveLimitedRecordsCommand = new RetrieveLimitedRecordsCommand(repository.getConnection());
		retrieveLimitedRecordsCommand.setSession(repository.getAuthenticatedUserSession().getSession());
		retrieveLimitedRecordsCommand.setResultDefinition(rd);
		retrieveLimitedRecordsCommand.setSearch(search);

		try
		{
			retrieveLimitedRecordsCommand.execute();
			System.out.println("Number of records found: " + retrieveLimitedRecordsCommand.getRecords().getCount());
			
			RecordResultSet resultSet = retrieveLimitedRecordsCommand.getRecords();
			
			if(resultSet.getCount()<1) 
			{
				System.out.println("No Records");
				return;
			}
			
			for(int i=0;i<resultSet.getCount();i++)
			{
				Record record = resultSet.getRecord(i);
				FieldId[] fields = record.getFields();
				//System.out.println(fields.length);
				
				for (int j=0; j<fields.length;j++)
				{
					FieldId fieldId = fields[j];
					//Get the field properties for the currently process field
					FieldProperties fieldProps = resultSet.getRecordMetadata().getField(fieldId);
					//Get the code of the field for display
					String fieldCode = fieldProps.getCode();
					//Get the value
					MdmValue value = record.getFieldValue(fieldId);
					
					System.out.print(fieldCode);
					System.out.print(":t");
					//Check if this is a lookup field
					//if(value.equals(""))
					//System.out.println(value);
					
					if(fieldProps.isLookup() && !value.isNull()) 
					{
						//If the field is a lookup, do not display the lookup id, but a sensible
						//display value as defined in the lookup table.
						String lookupDisplayValue = record.getLookupDisplayValue(fieldId);
						System.out.println(lookupDisplayValue + "(" + value + ")");
					} 
					else 
					{
						if(!value.isNull())
						{
							System.out.println(value);
						}
						else
						{
							System.out.println("");
						}
					}
				}
				System.out.println("----------------------------------------------");
			}

Thanks

Ninad

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hallo Ninad,

the reason you don't get any nice result for your lookup fields is that you did not define the supportingResultSet. You have to define a resultSet for each lookup table from which you want to retrieve values. You combine all these lookupTable-ResultDefinitions into an array of resultDefinitions and add them to the retrieveLimitedRecordsCommand with


retrieveLimitedRecordsCommand.setSupportingResultDefinitions(supportingresultDefinitions);

After that, the getLookupDisplayValue(...) stuff should work.

PS: you can use something like this to create the lookup resultDefinitions:


List productLookupTables = new ArrayList();
GetTableListCommand tableListCommand = new GetTableListCommand(CONNECTION);
tableListCommand.setSession(sessionID);
try{
	tableListCommand.execute();
	} catch (CommandException e){
		doLog(e);
		return;
	}
	TableProperties lookupTable = null;
	TableProperties productMainTable = null;
	TableProperties[] tables = tableListCommand.getTables();
	List productLookupTables = new ArrayList();
	for (int i = 0; i < tables.length; i++){
		if (tables<i>.getType() == TableProperties.MAIN){
			productMainTable = tables<i>;
		} else if (tables<i>.getType() == TableProperties.FLAT){
			lookupTable = tables<i>;
			productLookupTables.add(lookupTable.getId());
		}
	}
}catch(Exception e){
	e.printStackTrace();
}

supportingProductResultDefinitions = new ResultDefinition[productLookupTables.size()];

for (int i = 0; i < productLookupTables.size(); i++){
	TableId tableId = (TableId) productLookupTables.get(i);
	getFieldListCommand = new GetFieldListCommand(CONNECTION);
	getFieldListCommand.setSession(sessionID);
	getFieldListCommand.setTableId(tableId);
	try{
		getFieldListCommand.execute();
	} catch (CommandException e){
		doLog(e);
	}
	FieldProperties[] lookupFields = getFieldListCommand.getFields();

	supportingProductResultDefinitions<i> = new ResultDefinition(tableId);

 	for (int m = 0; m < lookupFields.length; m++){
		supportingProductResultDefinitions<i>.addSelectField(lookupFields[m].getId());
	}
}

Good luck,

Martin