cancel
Showing results for 
Search instead for 
Did you mean: 

Accessing attributes through API

Former Member
0 Kudos

Can anyone provide me with sample code or hints about how to access the values of the taxonomy attributes through the API? I seem to be able to get the attributes themselves fine, but I want to get the actual values of the attributes.

I have this so far, and it seems to be getting me on the right track I think:

AttributeValueExArray attrArray = rs.GetAttributes(0, "pub_hier");

//Get the info from the attribute information

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

AttributeInfo attrInfo = attrArray.GetAttributeValueExAt(i).GetAttributeInfo();

String alias = attrInfo.GetAlias();

String name = attrInfo.GetName();

int priority = attrInfo.GetPriority();

int imageId = attrInfo.GetImageID();

int attrId = attrInfo.GetID();

int type = attrInfo.GetType();

}

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Sarah,

Bellow is the code for printing the attributes.

FieldId attFiledId = this.getFieldId(tableName, "<Taxonomy Field Name>");

AttributeId[] attributes = record.getAttributes(attFiledId);

for (int i=0;i<attributes.length;i++)

{

System.out.println(record.getAttributeValue(attFiledId, attributes[0]));

}

Note: If found useful please award points.

Thanks

Ninad

Former Member
0 Kudos

Thanks for the reply. I was using the old API, and I have now changed to the new API.

Can you tell me how to search on the taxonomy table? I cannot seem to get this working and I think that will need to be done before I am able to access the attributes. I have this much so far, which gets me the initial values in the table, but I am not able to access the attributes within it. pub_hier is the name of the field in the main table which is a taxonomy table also.

//The table to be searched on

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

//Specify the result definition (what to retrieve)

ResultDefinition rd = new ResultDefinition(mainTableId);

Instantiate a search, to which search criteria can be added

Search search = new Search(mainTableId);

//No criteria are added, thus the search returns all records of the search table

//Retrieve the records with the command RetrieveLimitedRecordsCommand

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

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

retrieveLimitedRecordsCommand.setResultDefinition(rd);

retrieveLimitedRecordsCommand.setSearch(search);

try {

retrieveLimitedRecordsCommand.execute();

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

PrintRecords.toConsole(retrieveLimitedRecordsCommand.getRecords());

} catch (CommandException e) {

e.printStackTrace();

} finally {

repository.cleanup();

}

Former Member
0 Kudos

Has anyone been able to successully get the attribute values from a taxonomy field through API? I'm sure I am not the only one who has this requirement.

Answers (5)

Answers (5)

Former Member
0 Kudos

I needed to check if it was multivalue before getting TextAttributeValue.

Former Member
0 Kudos

Please check if my answer on this Thread can help you.

Former Member
0 Kudos

Thanks for link Moreno!! I tried that piece of code.. Unfortunately i am getting 0 (zero) for textAttributeValueProperties.length

AttributeId[] olFldAttrIds=records[0].getAttributes(repsch.getFieldI("Products","PriceBookClassification"));

for(int y=0;y<olFldAttrIds.length;y++){

.

.

.

olTxtValue=(TextAttributeValue)olMdmValue;

oltxtatt=new TextAttributeProperties(olFldAttrIds[y]);

.

.

TextAttributeValueProperties[] textAttributeValueProperties =oltxtatt.getTextAttributeValues();

for (int ii = 0, jj = textAttributeValueProperties.length;ii < jj; ii++) {

System.out.println(textAttributeValueProperties[ii].getId().getIdValue() + " *** " + textAttributeValueProperties[ii].getName().toString());

}

Inspite of having a value in the olTxtValue which is TA01 or something like that.

If you have got the solution could you show us that code?

Thanks,

Raags

Former Member
0 Kudos

Ok, I am now able to get the values of the attributes after upgrading, however, I'm getting the same values you are (TA2, TA3, etc). Now I'm getting a ClassCastException when I try to run this line for text attributes, to cast the MdmValue to TextAttributeValue:

TextAttributeValue txtval = (TextAttributeValue)attValue;

It seems to blow up on the value [[TA5]] - I'm not sure exactly what that value means?

Former Member
0 Kudos

Thats nice to hear from you!! Yeah still struck at these values..!! I still dont get an idea.. what this could be .. for a while i thought it can be a record id of the taxonomy table.. but as attribute values wouldnt be at console, it prooved wrong.

unfortunately there is not even a single blog, forum which addresses this problem. I searched for sap notes.. still no luck.

hmmm

bye,

Raags

Former Member
0 Kudos

I'm not sure that upgrading is the solution. Can I have access to your MDM Repository? Maybe I can test my code with it.

BR.

Former Member
0 Kudos

Well what I'm thinking is the issue is the fact that the lookup tables are not included in the ResultSetDefinition until you add them using retrieveLimitedRecordsCommand.setSupportingResultDefinitions(supportingRd);

I don't know if this is why the issues are there or not, but I can't run that line of code, it blows up everytime, due to a ServerException.

Former Member
0 Kudos

Hi Sarah,

I am still struck at 'TA01' ie., at text attribute without actual value!!

Unable to get the value of textattribute. Hope at least you get that. Let us know when you get the solution.

Thanks,

Raags

Former Member
0 Kudos

That's further than I've gotten. Have you tried TextAttributeValueProperties to get the text value?

Former Member
0 Kudos

Hi, once you created the connection and the usersession MDM objects, you need the TableID object from the Taxonomy table with the attributes, then you need the RecordID Object from the record you want the values. Once you have this objects, all you need is this piece of code:

static public AttributeLink[] getAttributeLinks(ConnectionAccessor connection, String userSession,

TableId taxonomyTableID, RecordId taxonomyRecordID) {

RetrieveAttributeLinksCommand cmd = new RetrieveAttributeLinksCommand(connection);

cmd.setSession(userSession);

cmd.setTaxonomyTableId(taxonomyTableID);

cmd.setRecordId(taxonomyRecordID);

try {

cmd.execute();

} catch (CommandException e) {

e.printStackTrace();

}

return cmd.getAttributeLinks();

}

You will get an array of AttributeLinks type that you can convert to an AttributeProperties type like this

AttributeProperties[] attributes = new AttributeProperties[als.length];

for(int i=0, j=als.length; i<j; i++) {

attributes = getAttribute(connection, userSession, taxonomyTableID, als.getId());

}

Once you have the data on AttributeProperties, then you can go over the attributes types.

Please let me know if this help.

Regards.

Former Member
0 Kudos

Thank you for the code! I have tried it, however I cannot get it to work. I am going through and I have the record that I want to use. But I am getting both NullPointerException and ServerException.

We are upgrading to SP5 today, I'm hoping this may solve some of our problems. Are you using SP4? If so, how does your whole code look? I feel like maybe I am missing something else perhaps?

Here is the stack trace of my exceptions:

com.sap.mdm.commands.CommandException: com.sap.mdm.internal.protocol.manual.ServerException: Not found

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

at GetAttributes.getAttributeLinks(GetAttributes.java:82)

at GetAttributes.showAttributes(GetAttributes.java:40)

at Repository.main(Repository.java:379)

Caused by: com.sap.mdm.internal.protocol.manual.ServerException: Not found

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

... 4 more

java.lang.NullPointerException

at GetAttributes.showAttributes(GetAttributes.java:42)

at Repository.main(Repository.java:379)

Former Member
0 Kudos

Hi Sarah, do you want the list of attribute and attributes values linked to a given category? Or the values for a given record?

BR.

Former Member
0 Kudos

Hi, thanks for the reply. I am looking for the values for a given record.