cancel
Showing results for 
Search instead for 
Did you mean: 

Java API lookup field problem

Former Member
0 Kudos

Hi All,

I have a problem while retrieving data from look up (Taxonomy) field. It works well if the record has some value selected for look up but no look up value is selected as it is optional field program throws ServerException

Can any one help me in this regards.

Thanks

Ninad

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Ninad,

Please check this sample code: which has been worked. This is your code been manipulated.

Nb:Please award points if you find it useful.

TableId mainTableId = repository.getTableId("Products");

// fields to retrieve from Main Table

FieldId[] fieldsl = new FieldId [2];

fieldsl[0] = repository.getFieldId("Products", "Name");

fieldsl[1] = repository.getFieldId("Products", "Category");

ResultDefinition rd = new ResultDefinition(mainTableId);

rd.setSelectFields(fieldsl);

//Category table fields retrieval.

TableId classesTableIdc = repository.getTableId("Categories");

FieldId[] classesFields = new FieldId[1];

classesFields[0] = repository.getFieldId("Categories", "Name");

ResultDefinition rdClasses = new ResultDefinition(classesTableIdc);

rdClasses.setSelectFields(fieldsc);

ResultDefinition[] supportingRd = new ResultDefinition[] ;

KeywordSearchDimension keywordSearchDimension = new KeywordSearchDimension();

KeywordSearchConstraint keywordSearchConstraint = new KeywordSearchConstraint("ninad", KeywordSearchConstraint.PROGRESSIVE);

compose search

Search search = new Search(mainTableId);

search.addSearchItem(keywordSearchDimension, keywordSearchConstraint);

// retrieve the records

RetrieveLimitedRecordsCommand retrieveLimitedRecordsCommand = new RetrieveLimitedRecordsCommand(repository.getConnection());

retrieveLimitedRecordsCommand.setSession(repository.getAuthenticatedUserSession().getSession());

retrieveLimitedRecordsCommand.setResultDefinition(rd);

retrieveLimitedRecordsCommand.setSearch(search);

retrieveLimitedRecordsCommand.setSupportingResultDefinitions(supportingRd);

try {

retrieveLimitedRecordsCommand.execute();

System.out.println("Number of records found: " + retrieveLimitedRecordsCommand.getRecords().getCount());

} catch (CommandException e) {

e.printStackTrace();

}

Hope this code helps, it runs for me.

There is nothing like problem with the API, please try this and let me know if u have any isues.

Sorry for the late in seeing your thread, if not i could haev answered b4.

Thanks,

Mary Joseph.

Former Member
0 Kudos

Hi Mary,

Thanks a lot for your efforts but even this code does not work and have same problem.

Thanks

Ninad

Former Member
0 Kudos

Hi Ninad,

it seems that i have the same problem:

My MDM Server version is: Version 5.5 SP5 (5.5.40.83)

when i set Search Criteria which must get some Records everything all right,

but when i set Search Criteria which havn't get any Records i get Exception:

<i>Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: The array of table schemas must contain at least one table.

at com.sap.mdm.schema.RepositorySchema.<init>(RepositorySchema.java:63)

at com.sap.mdm.data.commands.RetrieveLimitedRecordsCommand.execute(Unknown Source)

</i>

as Anu i think that it's server version error.

I would like to ask u if u try to patch your MDM server and if it's solve the problem?

P.S. plz accept my apologise for my English

Former Member
0 Kudos

Hi Mike,

This is the problem of Version only I upgraded it to 5.5.42.75 and it worked

Thanks

Ninad

Answers (1)

Answers (1)

Former Member
0 Kudos

First get the value of the lookup field.

MdmValue value = Record.getFieldValue(fieldid); (fieldid is the FieldId of Lookup field from main table)

Then make sure the value is instance of LookupValue. If the lookup field is not filled then returns NullValue not LookupValue.

Ex:

if ( value instanceof LookupValue) {

LookupValue categoryLookUpValue = (LookupValue) result.getRecord(i).getFieldValue(fieldid);

Anu

Former Member
0 Kudos

Hi Anu,

I have already done that I think you don't got my problem.

I have a record where nothing is selected in look up field its blank and if that record comes in search criteria for example by searching on name filed or some thing like that. it throws me bellow error.

Exception in thread "main" java.lang.IllegalArgumentException: The array of table schemas must contain at least one table.

else part it displays the look up field value with the code you have mentioned in your post.

Thanks

Ninad

Former Member
0 Kudos

Ninad,

You have to post your code to look into the problem.

Thanks

Anu

Former Member
0 Kudos

Hi Anu

Bellow is the code & exception I am getting at the last execute statement of the code.

	String tableName = "product";
	Location mainTrace = Location.getLocation("");
	ConsoleLog log = new ConsoleLog();
	mainTrace.setEffectiveSeverity(Severity.NONE);
	mainTrace.addLog(log);

	// Create new repository instance and initialize with default values
	
	repository.initialize("javaAPITest");

	// The table to be searched on. Name hard coded ...
	TableId mainTableId = repository.getTableId(tableName);
	// 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(tableName, "address") });
	rd.addSelectField(repository.getFieldId(tableName, "phone"));
	rd.addSelectField(repository.getFieldId(tableName, "Category"));
	rd.addSelectField(repository.getFieldId(tableName, "Name"));

	// Instantiate a search, to whoch search criteria can be added
	Search search = new Search(mainTableId);

	
	////////////////Search Attributes///////////////////////////////////////////////////////////////
	//////////Option 1//////////
	KeywordSearchDimension keywordSearchDimension = new KeywordSearchDimension();
	KeywordSearchConstraint keywordSearchConstraint = new KeywordSearchConstraint("ninad", KeywordSearchConstraint.CONTAINS);
	search.addSearchItem(keywordSearchDimension, keywordSearchConstraint);
	//////////Option 1 End//////////

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

	// //////////////////TO ADD LOOK UP TABLES IN RESULTS/////////////////////////////////////////////////////////////////
	TableId tableId = repository.getTableId("Categories");
	GetFieldListCommand getFieldListCommand = new GetFieldListCommand(repository.getConnection());
	getFieldListCommand.setSession(repository.getAuthenticatedUserSession().getSession());
	getFieldListCommand.setTableId(tableId);
	try
	{
		getFieldListCommand.execute();
	}
	catch (CommandException e)
	{
		System.out.println(e);
	}

	FieldProperties[] lookupFields = getFieldListCommand.getFields();
	// System.out.println(lookupFields[0].getName());

	ResultDefinition supportingResultDef = new ResultDefinition(tableId);
	supportingResultDef.addSelectField(lookupFields[0].getId());

	ResultDefinition[] supportingResultDefs = new ResultDefinition[1];
	supportingResultDefs[0] = supportingResultDef;
	retrieveLimitedRecordsCommand.setSupportingResultDefinitions(supportingResultDefs);
	// //////////////////END OF ADD LOOK UP TABLES IN
	// RESULTS/////////////////////////////////////////////////////////////////

	retrieveLimitedRecordsCommand.setSearch(search);

	retrieveLimitedRecordsCommand.execute();

com.sap.mdm.commands.CommandException: com.sap.mdm.internal.protocol.manual.ServerException: Unhandled Exception

at com.sap.mdm.data.commands.AbstractRetrieveLimitedRecordsCommand.execute(Unknown Source)

at com.sap.mdm.data.commands.RetrieveLimitedRecordsCommand.execute(Unknown Source)

at com.mdm3.NewSearchRepository.main(NewSearchRepository.java:411)

Caused by: com.sap.mdm.internal.protocol.manual.ServerException: Unhandled Exception

at com.sap.mdm.internal.protocol.manual.AbstractProtocolCommand.execute(AbstractProtocolCommand.java:110)

... 3 more

Former Member
0 Kudos

Ninad,

Your code looks fine I did a sample test on my machine with your code and it didn't throw me any error. Here is the working code. I only modified the table codes and field code to match my repository but logic is same. Just check your table codes and field codes. Make sure your MDM is up and repository is loaded.

Search search = new Search(MDMStageConnectionUtil.getTableId("ITEMS"));

// //////////////Search Attributes///////////////////////////////////////////////////////////////

//////////Option 1//////////

KeywordSearchDimension keywordSearchDimension = new KeywordSearchDimension();

KeywordSearchConstraint keywordSearchConstraint = new KeywordSearchConstraint("Binders", KeywordSearchConstraint.CONTAINS);

search.addSearchItem(keywordSearchDimension, keywordSearchConstraint);

//////////Option 1 End//////////

ResultDefinition rd = new ResultDefinition(MDMStageConnectionUtil.getTableId("ITEMS"));

rd.setSelectFields(new FieldId[]

{ MDMStageConnectionUtil.getFieldId("ITEMS", "ITEM_NUMBER") });

rd.addSelectField(MDMStageConnectionUtil.getFieldId("ITEMS", "CATEGORY"));

// Retrieve the records with the command RetrieveLimitedRecordsCommand

RetrieveLimitedRecordsCommand retrieveLimitedRecordsCommand = new RetrieveLimitedRecordsCommand(conn);

retrieveLimitedRecordsCommand.setSession(session);

retrieveLimitedRecordsCommand.setResultDefinition(rd);

// //////////////////TO ADD LOOK UP TABLES IN RESULTS/////////////////////////////////////////////////////////////////

GetFieldListCommand getFieldListCommand = new GetFieldListCommand(conn);

getFieldListCommand.setSession(session);

getFieldListCommand.setTableId(MDMStageConnectionUtil.getTableId("TAXONOMY"));

try {

getFieldListCommand.execute();

} catch (CommandException e) {

System.out.println(e);

}

FieldProperties[] lookupFields = getFieldListCommand.getFields();

System.out.println(lookupFields[0].getName());

ResultDefinition supportingResultDef = new ResultDefinition(MDMStageConnectionUtil.getTableId("TAXONOMY"));

supportingResultDef.addSelectField(lookupFields[0].getId());

ResultDefinition[] supportingResultDefs = new ResultDefinition[1];

supportingResultDefs[0] = supportingResultDef;

retrieveLimitedRecordsCommand.setSupportingResultDefinitions(supportingResultDefs);

// //////////////////END OF ADD LOOK UP TABLES IN

// RESULTS/////////////////////////////////////////////////////////////////

retrieveLimitedRecordsCommand.setSearch(search);

retrieveLimitedRecordsCommand.execute();

System.out.println(retrieveLimitedRecordsCommand.getRecords().getCount());

Anu

Former Member
0 Kudos

Hi Anu,

Thanks a lot for so trying this and taken so much pain for this. But I think I am not able to convey the problem properly. I know the code is good and there is no problem with it. I will explain you the problem bellow.

I have repository with 4 fields Name, address, Category and phone from those Name & phone are text fields, address is text large and Category is Lookup [Taxonomy].

and in one record no Category is selected from lookup.

When I retrieve give search parameter and if the search result don't content the record where Category is blank everything works fine. But if search result contains that record where Category is blank or not selected it gives the problem. and throws exception.

You have any Idea about this.

Thanks

Ninad

Former Member
0 Kudos

Ninad,

I understand your problem. I tried 2 tests. One with all categories values populated and another by removing the category value. Both tests worked fine for me. Which version of MDM are you using? I am using MDM 5.5 SP5 with HF 1. ie.5.5.42.48

Anu

Former Member
0 Kudos

Hi Anu,

my server version is 5.5.40.83 could this problem because of server version?

Thanks

Ninad

Former Member
0 Kudos

Ninad,

It could be since, there are so many issues with new MDM Java API that are being fixed and released in hot fixes. I would advise to update the MDM server and API jars with the latest version and try.

Anu