cancel
Showing results for 
Search instead for 
Did you mean: 

Java API - 7.1 - Retrieve Multi-Valued Attribute Values

Former Member
0 Kudos

I am having trouble getting the values of attributes when the attribute is multi-valued.

Here is the excerpt from my code:


RecordResultSet rs = retrieveLimitedRecordsCommand.getRecords();

for(int i=0; i < rs.getCount(); i++)
{
	Record record = rs.getRecord(i);
	
	FieldId taxonomyField = repository.getFieldId("Products", "Category");
	AttributeId[] attrsIds = record.getAttributes(taxonomyField);
	
	if( attrsIds != null && attrsIds.length> 0 )
	{
		for( int j=0; j<attrsIds.length; j++)
		{
			AttributeProperties properties = as.getAttribute(taxonomyField, attrsIds[j]); 
			
			String strAttributeName = properties.getName().toString();
			String strAttributeValue = "";
			String strDimentions = "";
			
			switch(properties.getType())
			{
				case AttributeProperties.NUMERIC_TYPE: 
					//To be completed later
					break;
				case AttributeProperties.TEXT_TYPE: 

					MdmValue mdmVal = record.getAttributeValue(taxonomyField, attrsIds[j]);  //Cannot be cast into MultiValue
					
					TextAttributeValueId t = new TextAttributeValueId( attrsIds[j].getIdValue() );
					TextAttributeValueEx tav = new TextAttributeValueEx( t, repository.getTableId("Categories"), attrsIds[j], sessionContext );
					tav.getDisplayValue(); //Gets the display name for single values, not multi !!
					break;
			}
		}
	}
}

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

The trick is to cast the getAttributeValue method into a MultiValue object:


MultiValue attributeValues = (MultiValue) record.getAttributeValue(taxonomyField, attrsIds[j]);

for(int k=0; k<attributeValues.getValuesCount(); k++)
{
	TextAttributeValueId t = new TextAttributeValueId( attributeValues.getValue(k).toString() );
	TextAttributeValueEx tav = new TextAttributeValueEx( t, repository.getTableId("Categories"), attrsIds[j], sessionContext );
	strAttributeValue += (tav != null && !tav.isNull()) ? tav.getDisplayValue() : "NULL";
	strAttributeValue += ", ";
}