on 11-18-2008 3:19 AM
I am trying to come up with an easy way to get the display value of a lookup table.
I currently have the Record of the main table, not the Lookup table.
I am open to suggestions on how to get the actual Lookup display value from where I am.
Edited by: Boris Todorov on Nov 18, 2008 4:19 AM
Boris,
There is a method on the Record object called getLookupDisplayValue that takes the main table's FieldId for the lookup field.
Hope this helps.
Greg
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you Greg,
I tried using this method, but it always returns a "null" value. I want to know if I need to have the Lookup table Record or it's enough for me to have the Main table Record like I do. There are some prerequisites that I need to have in order to get the value with this method.
I need some code samples to make that work.
Regards,
Boris
Thanks again,
I have the internal record id that is of type "R123" and that's the right value, but I'm missing the lookup value.
Here is how I'm adding the field to the result definition:
ResultDefinition rd = new ResultDefinition(connection.getMainTable());
fieldId = connection.getTableFieldIdFromName("Country");
rd.addSelectField(fieldId);
The method getTableFieldIdFromName returns the field Id that the record has.
Are you saying that I need to separately add the Lookup value to the ResultDefinition?
You add the FieldId from the main table only. So it looks like you have it correct. Try this, find out what data is returned for the lookup field. If you get a LookupValue you should be able to get the display value, but if you get a NullValue the record you have doesn't have a county assigned to it.
From the main table Record object you have.
MdmValue mdmval = rec.getFieldValue(fieldId);
if(mdmval instanceof LookupValue){
//do something here like print a message
}else if(mdmval instanceof NullValue){
//do something else here
}
Edited by: Greg Austin on Nov 18, 2008 3:41 PM
HI,
You will have to set the setSupportingResultDefinitions to get the lookup field display value.
Please find some sample code.
// Defining the Result Set for Main Table
ResultDefinition rdMain = new ResultDefinition(new TableId(tabProp.getId()));
// Array List for defining Lookup Fields Result set.
List<ResultDefinition> resultDefinitionList = new ArrayList<ResultDefinition>();
// Variable for LookupFieldProperties
LookupFieldProperties lookupField = null;
for (int d = 0; d < fieldProp.length; d++)
{
// Adding the Field to Main Table Result set.
rdMain.addSelectField(new FieldId(fieldProp[d].getId()));
// Checking for Lookup Field
if (fieldProp[d].isLookup())
{
lookupField = (LookupFieldProperties) fieldProp[d];
// Result Set for Lookup Table.
ResultDefinition rdlookup = new ResultDefinition(new TableId(lookupField.getLookupTableId()));
FieldProperties[] fieldlookupProp = retId.retrieveFieldIds(connections, sessionId, lookupField.getLookupTableId());
for (int h = 0; h < fieldlookupProp.length; h++)
{
rdlookup.addSelectField(new FieldId(fieldlookupProp[h].getId()));
}
resultDefinitionList.add(rdlookup);
}
}
// Load the attribute Values.
rdMain.setLoadAttributes(true);
// Defining the Result set array for the Lookup Tables.
ResultDefinition[] lookupValues = (ResultDefinition[]) resultDefinitionList.toArray(new ResultDefinition[resultDefinitionList.size()]);
RetrieveLimitedRecordsCommand recordsCommand = new RetrieveLimitedRecordsCommand(connections);
recordsCommand.setSession(sessionId);
recordsCommand.setSearch(ssearch);
recordsCommand.setResultDefinition(rdMain);
recordsCommand.setSupportingResultDefinitions(lookupValues);
For Lookup Field
System.out.println("Field Value : "+ resSet<i>.getFieldValue(fieldProp[j].getId())); -- will return record id
System.out.println("Lookup Value : "+ resSet<i>.getLookupDisplayValue(fieldProp[j].getId())); -- will return lookup display field value.
Hope it helps.
Thanks,
Priya.
Priya,
I disagree. You only need a supporting result definition if you need fields from the lookup table that are not display fields. getFieldValue will give you the MdmValue for that field, which in this case will be either a LookupValue or NullValue. When you print a LookupValue you will see the RecordId. getLookupDisplayValue will give the display value with the use of supporting result definitions.
-Greg
Hi,
Without the support Result definition, the lookup display value will not be displayed. You will only get the record id in the getFieldvalue method and the getLookupDisplayValue method will return null.
You can try both the options in your result set and see which is true.
You can also refer to Example program provided by SAP. which also includes the supportResult definition.
One thing you can change is.. you can just add the display field id to support result definition, instead of adding all the fields present in it.
Hope u got my point.
Thanks,
Priya.
I'm not sure if MDM version matters or not but I have used getLookupDisplayValue without supporting result definitions dozens of times. I have also been on performance improvement projects where I removed dozens of unneeded supporting result definitions from searches to help performance and code maintainability.
-Greg
Hi Priya,
My code is nothing special just not using a supporting result definition. Here is one example. Category and Product_Family are lookup fields.
TableId tblId = connDtls.getTableId("Products");
ResultDefinition rd = new ResultDefinition(tblId);
FieldId[] fields = new FieldId[4];
fields[0] = connDtls.getFieldId("Products", "Category");
fields[1] = connDtls.getFieldId("Products", "Product_Family");
fields[2] = connDtls.getFieldId("Products", "Manufacturer");
fields[3] = connDtls.getFieldId("Products", "Product_Number");
rd.setSelectFields(fields);
Search search = new Search(tblId);
RetrieveLimitedRecordsCommand rlrCmd = new RetrieveLimitedRecordsCommand(connDtls.getConnection());
rlrCmd.setSession(connDtls.getUserSessionId());
rlrCmd.setSearch(search);
rlrCmd.setPageSize(500000);
rlrCmd.setResultDefinition(rd);
try {
rlrCmd.execute();
} catch (CommandException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
RecordResultSet rrs = rlrCmd.getRecords();
Record[] records = rrs.getRecords();
for (int i = 0; i < rrs.getCount(); i++) {
String cat = records<i>.getLookupDisplayValue(fields[0]);
String pNum = records<i>.getFieldValue(fields[3]).toString();
String prodFam = records<i>.getLookupDisplayValue(fields[1]);
...Do some things with the data.....
}
To made sure it works for both SP05 and SP06, I'm reading the display value directly from the lookup table and displaying it. We have both systems here and when I asked the questions I was implementing it in SP05, therefore it was not working.
Now it works for both systems, but it takes some loops...
Cheers,
Boris
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Priya,
Thank you for your help. While I was waiting for your responses I made a subroutine to go and extract the Display Value from the Lookup table explicitly, but I can change that if I find it necessary.
Can you also send us a link or some information on where to find this SAP Example that you were referring to? I think that would be beneficial to all of us reading this thread.
Thanks in advance,
Boris
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
You can find the example program in
com.sap.mdm.examples package. present in the link
http://help.sap.com/javadocs/MDM/SP05/
Thanks,
Priya.
Hi Boris,
You need to set the supporting resultdefinition
ResultDefinition countryrd = new ResultDefinition(countrytableid);
Add the fieldids to countryrd of Countrytable using
countryrd.setSelectedFields(<fieldidsarray>);
//Create Support ResultDefinition
ResultDefinition supprd = new ResultDefinition[];
Then set this supprd to the command whatever you are using to retrieve the records.
hopefully this will resolve your problem.
Thanks & Regards,
Sreenivasulu.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
72 | |
26 | |
10 | |
9 | |
7 | |
6 | |
4 | |
4 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.