[SalesForce] How to assign data categories to articles using apex controller

I am trying to assign data categories to knowledge base articles records. I am using articlename_DataCategorySelection to define the assignment and then to inset it.
The object to be inserted as follows:

DescribeDataCategoryGroupResult[] results = Schema.describeDataCategoryGroups(
    new String[] { 'KnowledgeArticleVersion'}
); 
tempCat = new MSDS__DataCategorySelection ();
tempCat.DataCategoryGroupName = results[0].getName();  // key line
tempCat.DataCategoryName = 'Latin America'; //m.Category_Path__c;
tempCat.ParentId = m.id;

Above, results[0] is of type DescribeDataCategoryGroupResult –that contains data category group description. but it is not allowing me to insert the record successfully.

While running the code above, I get an error:

INVALID_DATA_CATEGORY_GROUP_REFERENCE, invalid data category group referenced

It seems the DataCategoryGroupName is not handled properly. Does anyone know what value to assign to that field?

Best Answer

You might be getting this error based on DataCategoryName, not DataCategoryGroupName. Double-check that Latin America is a valid category for the category group you selected based on results[0].

Related Topic