List metadata from the metadata api

PHOTO EMBED

Thu Jul 11 2024 13:58:46 GMT+0000 (Coordinated Universal Time)

Saved by @Justus

String mdtType = 'GenAiPromptTemplate';
String endpoint = URL.getOrgMyDomainUrl().toExternalForm() + '/services/Soap/m/61.0';

// Setup SOAP body
String reqBody ='<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mdt="http://soap.sforce.com/2006/04/metadata"><soapenv:Header><mdt:SessionHeader><mdt:sessionId>' + UserInfo.getSessionId() + '</mdt:sessionId></mdt:SessionHeader><mdt:CallOptions><mdt:client></mdt:client><mdt:defaultNamespace></mdt:defaultNamespace></mdt:CallOptions><mdt:AllOrNoneHeader><mdt:allOrNone>true</mdt:allOrNone></mdt:AllOrNoneHeader></soapenv:Header><soapenv:Body><mdt:listMetadata><queries><folder></folder><type>'+mdtType+'</type></queries></mdt:listMetadata></soapenv:Body></soapenv:Envelope>';

// Setup POST request
HttpRequest req = new HttpRequest();
req.setEndpoint(endpoint);
req.setMethod('POST');
req.setHeader('Content-Type', 'text/xml; charset=UTF-8');
req.setHeader('SOAPAction', 'listMetadata');
req.setBody(reqBody);

// Output the list
System.debug(JSON.serializePretty(getMetadataTypeFullNameListMapFromListResponseBody(new Http().send(req).getBody())));


/**
 * Method to get a list of metadata per metadata type
 */
public Map<String,String[]> getMetadataTypeFullNameListMapFromListResponseBody(String listResponseBody){ 

	// XML Namespaces
	final String evNs = 'http://schemas.xmlsoap.org/soap/envelope/';
	final String mdNs = 'http://soap.sforce.com/2006/04/metadata';

	// Create output
	Map<String,String[]> mdtTypeNameFullNameListMap = new Map<String,String[]>();

	// Read XML
	Dom.Document domDocument = new Dom.Document();
	domDocument.load(listResponseBody);	

	// Parse XML into list, read the <Body> --> than <listMetadataResponse> to get the list of <results>
	Dom.XmlNode[] resultElements = domDocument.getRootElement()?.getChildElement('Body',evNs)?.getChildElement('listMetadataResponse',mdNs)?.getChildElements();

	// Iterate all result elements. Each result element represents a single metadata item from the list
	for(Dom.XmlNode resultElement : resultElements){

		// Variables for fetching the metadata type and metadata fullname
		String mdtType;
		String fullName;

		// Get all metadata type specific attributes
		for(Dom.XmlNode grandChildElement : resultElement.getChildElements()){
			if(mdtType == null && grandChildElement.getName() == 'type'){
				mdtType  = grandChildElement.getText();
			}else if (fullName == null && grandChildElement.getName() == 'fullName'){
				fullName  = grandChildElement.getText();
			}
		}

		if(!mdtTypeNameFullNameListMap.containsKey(mdtType)){
			mdtTypeNameFullNameListMap.put(mdtType, new String[]{});
		}
		mdtTypeNameFullNameListMap.get(mdtType).add(fullName);
	}
	return mdtTypeNameFullNameListMap;
}
content_copyCOPY