cancel
Showing results for 
Search instead for 
Did you mean: 

How to get value of lookup field?

Former Member
0 Kudos

I have a lookup field "Category" in main table "Products". How to get the value of field "Category"?

Here is my code:

String searchTable= "Products";

String searchField = "ProductName";

String searchValue = "Product_001";

CMFieldInfoArray fields = catalogData.GetFields(searchTable);

ResultSetDefinition rsd = new ResultSetDefinition(searchTable);

for (int i = 0; i < fields.GetCount(); i++)

{

String fieldName = fields.GetCMFieldInfoAt(i).GetCode();

rsd.AddField(fieldName);

}

Search search = new Search(searchTable);

FreeFormTableParameter fftpNames = search.GetParameters().NewFreeFormTableParameter(searchTable);

FreeFormParameterField ffpfName = fftpNames.GetFields().New(searchField);

ffpfName.GetFreeForm().NewString(searchValue, FreeFormParameter.EqualToSearchType);

A2iResultSet result = catalogData.GetResultSet(search, rsd, null, true, 0);

for ( int i = 0; i < result.GetRecordCount(); i++ )

{

for ( int j = 0; j < result.GetFields().GetCount(); j++ )

{

// if this field is lookup field "Category"

if ( result.GetFieldAt(j).IsLookup() )

{

// Get lookup IDs

A2iIntArray lookupIDs = result.GetLookupIDAt(i, j);

// How to get the value of lookup field "Category"?

}

}

}

Thanks.

Forrest

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hello Forrest,

A2iIntArray lookupIDs = result.GetLookupIDAt(i, j);

//at this point you have the ID of the record of the table "Categories"

int recordId = lookupIDs.GetAt(0);

//you need to access to table "Categories" for get the value (you can create a

//new method getRelValueByID)

String category = this.getRealValueByID(id, CategoryTableName, FieldTableName, catalogData).

In method getRElVAlueByID you only have to implement a search like:

private String getRelValueByID(

int tableID,

String tableName,

String fieldName,

CatalogData catalogData)

{

ResultSetDefinition rsd = null;

A2iResultSet resultSet = null;

A2iIntArray ids = null;

A2iFields fields = null;

String fieldValue = "";

try {

Search search = new Search(tableName);

rsd = new ResultSetDefinition(tableName);

...

//Create here the result set definition

...

ids = new A2iIntArray();

ids.Add(tableID);

// Get Data from table

resultSet = catalogData.GetRecordsById(rsd, ids);

if (resultSet != null) {

fieldValue =

resultSet.GetValueAt(0, fieldName).TranslateToString();

}

} catch (Exception e) {

}

return fieldValue ; }

Regards

Former Member
0 Kudos

Hi, Yaiza,

It works. Thanks!

Forrest