Whenever I am unable to package a certain metadata type in a Managed Page I created them from a Setup page after installation using the Metadata API and this neat Apex Wrapper lib.

But this time I seem to be out of luck. The TopicsForObjects metadata type is not covered by this lib. Any other way to do it (simpler)? Without manually patching the lib?


I see TopicsForObjects in apex-mdapi. This is the below snippet from their code and taken from what's available in their github.

public class TopicsForObjects extends Metadata {
        public String type = 'TopicsForObjects';
        public String fullName;
        private String[] fullName_type_info = new String[]{'fullName',SOAP_M_URI,null,'0','1','false'};
        public Boolean enableTopics;
        public String entityApiName;
        private String[] enableTopics_type_info = new String[]{'enableTopics',SOAP_M_URI,null,'1','1','false'};
        private String[] entityApiName_type_info = new String[]{'entityApiName',SOAP_M_URI,null,'1','1','false'};
        private String[] apex_schema_type_info = new String[]{SOAP_M_URI,'true','false'};
        private String[] type_att_info = new String[]{'xsi:type'};
        private String[] field_order_type_info = new String[]{'fullName', 'enableTopics','entityApiName'};

I was also able to update the TopicsForObjects record for a given object that was disabled. I successfully flipped it to "enabled".

MetadataService.MetadataPort service = new MetadataService.MetadataPort();
service.SessionHeader = new MetadataService.SessionHeader_element();
service.SessionHeader.sessionId = UserInfo.getSessionId();
List<String> objectNames= new List<String>{'Account_Group__c'}; //add your objects api name here

//Get TopicForObject metadata records
List<MetadataService.TopicsForObjects> topicForObjectRecs = (List<MetadataService.TopicsForObjects>) service.readMetadata('TopicsForObjects', objectNames).getRecords();

//enable topics for given object
topicForObjectRecs[0].enableTopics = true;

//update metadata
List<MetadataService.SaveResult> results =
                new MetadataService.Metadata[] { topicForObjectRecs[0] });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.