cancel
Showing results for 
Search instead for 
Did you mean: 

LookUp Record Id from lookup table

Former Member
0 Kudos

Hi,

how to retrieve the lookup record id from a lookup table?Any code snippet?

Thanks,

Rekha

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Thanks

nitin_mahajan2
Contributor
0 Kudos

You'r kind welcome.

Regards,

Nitin

Former Member
0 Kudos

Hi Rekha,

Try the below code for getting the Lookup Values and record Ids:

1. Create a ResultDefinition

2. Create a Search Criteria

3. Use RetrieveLimitedRecordsCommand and set the above values

4. Execute the RetrieveLimitedRecordsCommand command

5. Once you execute the above command you will get RecordResultSet in return. Further code is given below:


RecordResultSet objRecordResultSet = objRetrieveLimitedCommands.getRecords();

for(int i =0;i<objRecordResultSet.getCount() ; i++)
{
      String strRecordId = objRecordResultSet .getRecord(i).getId().toString() ; 
      String strValue = objRecordResultSet .getRecord(i).getFieldValue(FIELD_ID).toString() ;
}				}

Hope this helps!!

Cheers,

Arafat

nitin_mahajan2
Contributor
0 Kudos

Simpler way,

In the retrievelimmited records command set the supporting result set definitions of the lookup tables.

retrievelimitedrecordscommand.setSupportingResultDefinitions( RD[] ) ;

execute the retrievelimited records command.

on the resulting record from the resultset, use the command

record.findLookupRecord( fieldId of the lookup field on the main record ) ;

This command returns a array of LookUp Table records

Use the command Record.getDisplayValue() to get the display value of the record. If multiple fields are set as Display = true, it will return a comma seperated MDM String value.

Here is an illustrative code


	  
ResultDefinition[ ] rdss = new ResultDefinition[3] ;

RetrieveLimitedRecordsCommand retrRecCom =
				new RetrieveLimitedRecordsCommand(connecPool);

retrRecCom.setSupportingResultDefinitions( rdss ) ;
retrRecCom.setResultDefinition(rd);
retrRecCom.setSearch(search);
retrRecCom.setSession(userSession);
try {
	retrRecCom.execute();
	wdComponentAPI.getMessageManager().reportSuccess(
	          "Record Count " + retrRecCom.getRecords().getCount());
	rs = retrRecCom.getRecords();
} catch (CommandException e) { }


Record record = command.getRecords().getRecord(0);

//Main table code = Customers, 
//Country as lookup table's field code in maintable
Record[] countries =	record.findLookupRecords(
		         repSchema.getFieldId("Customers", "Country"));

wdComponentAPI.getMessageManager().reportSuccess(
	" Country Field Code " + repSchema.getFieldId("Customers", "Country"));
//Name : Field in country table, with code "Name"
wdComponentAPI.getMessageManager().reportSuccess(
	" Country Name Code " + repSchema.getFieldId("Countries", "Name"));
//Number of lookup elements attached to the main record.	
wdComponentAPI.getMessageManager().reportSuccess(
	" countries length = " + countries.length);

//display the name of first country record mapped to the main table.
wdComponentAPI.getMessageManager().reportSuccess(
	" Country = " + countries[0].getDisplayValue());


You may also need how to get the result definitions of the lookup tables. Create an array and set result definitions of different lookup tables that you want into it. and set it to record.

I am trying to put that in this message, but the system is not letting, i will have that in next post.

Edited by: Nitin Mahajan on Jun 3, 2009 11:12 PM

nitin_mahajan2
Contributor
0 Kudos

How to create RD array


	  ResultDefinition[ ] rdss = new ResultDefinition[3] ;
	  
	  ResultDefinition rds1 = new ResultDefinition( tiSalesOrg ) ;
	  rds1.setSelectFields( fieldsSO ) ;
	  rdss[0] = rds1 ;
	  
	  ResultDefinition rds2 = new ResultDefinition( tiInc ) ;
	  rds2.setSelectFields( fieldsInc ) ;
	  rdss[1] = rds2 ;

	  ResultDefinition rds3 = new ResultDefinition( tiPL ) ;
	  rds3.setSelectFields( fieldsPL ) ;
	  rdss[2] = rds3 ;

	  
	  command.setSupportingResultDefinitions( rdss ) ;


//There is a simpler way to create RD[] for all the tables in repository



	getSupportingResultDefinitions() {
		
		RepositorySchema repositorySchema = wdContext.currentContextElement().getSchema() ;
		
		String userSession = wdContext.currentContextElement().getUsrSessionId();
		
		ArrayList list = new ArrayList();

		TableId[] tableIds = repositorySchema.getTableIds();
		
		for (int tableIndex = 0; tableIndex < tableIds.length; tableIndex++) {
	
			TableProperties table = repositorySchema.getTable(tableIds[tableIndex]);
				
			//wdComponentAPI.getMessageManager().reportSuccess("Table : " + table.getCode( )  ) ;
		
			if (table.isBlobTable())
				continue;
	
			//ResultDefinition rd = new ResultDefinition( schema.)
			FieldId[] fields = getAllFieldsOfTable( table.getId(), repositorySchema ) ;
			
			ResultDefinition rd = new ResultDefinition( table.getId());
			rd.setSelectFields( fields ) ;
					
			list.add(rd);
			
			rd = null; 
			fields  = null ;
			table = null ;

		}

	ResultDefinition[] rds = new ResultDefinition[0];
	rds = (ResultDefinition[]) list.toArray(rds);
	return rds;
	}

Let me know if you need any help here.

The same method can be used on Tuples.

Regards,

Nitin

Former Member
0 Kudos

Hi Rekha,

Check the below link. The code you are looking for is on page-12

https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/70a7afe4-9e3e-2b10-de8d-b105d0b8...

Regards,

Jitesh Talreja