Get the Current Wave Dataset Version Id based on a Dataset API Name
Sun Apr 14 2024 14:27:27 GMT+0000 (Coordinated Universal Time)
Saved by @Justus
// Run the method to get the dataset info Map<String,String> datasetInfo = getLatestWaveDataSetVersionId('Login'); // Construct your wave query String waveQuery = Wave.QueryBuilder.load( datasetInfo.get('datasetId'), datasetInfo.get('datasetVersionId') ).build('q'); // Validate the query is like we expect Assert.areEqual( // Expected String.format( 'q = load "{0}/{1}";', new String[]{ datasetInfo.get('datasetId'), datasetInfo.get('datasetVersionId') } ), // Actual waveQuery, 'Wave query was not as expected' ); /** * @description Method that gets the current datasetVersionId based on a dataset api name * @param datasetApiName The API Name of the dataset * @return Map containing 2 keys: The datasetId and datasetVersionId */ private static Map<String,String> getLatestWaveDatasetVersionId(String datasetApiName){ try{ // Create HTTP request HttpRequest request = new HttpRequest(); request.setMethod('GET'); request.setHeader('Content-Type' , 'application/json;charset=UTF-8'); // Create the endpoint URL to the current ORG // !! TESTING ONLY - REPLACE WITH NAMED CREDENTIAL FOR PROD IMPLEMENTATIONS !! request.setEndpoint(String.format( '{0}/services/data/v60.0/wave/datasets/{1}', new String[]{ URL.getOrgDomainUrl().toExternalForm(), datasetApiName } )); // Set the authorization header // !! TESTING ONLY - REPLACE WITH NAMED CREDENTIAL FOR PROD IMPLEMENTATIONS !! request.setHeader('Authorization', 'Bearer ' + userinfo.getSessionId()); // Execute the request HttpResponse response = new http().send(request); // Parse the JSON response if (response.getStatusCode() == 200){ // Pare the response as an object map Map<String, Object> responseMap = (Map<String, Object>) JSON.deserializeUntyped(response.getBody()); // Return the map return new Map<String,String>{ 'datasetId' => (String) responseMap?.get('id'), 'datasetVersionId' => (String) responseMap?.get('currentVersionId') }; }else{ throw new StringException('Unexpected API response ('+response.getStatusCode()+'):' + response.getBody()); } }catch(Exception e){ System.debug(e.getMessage()); return null; } }
Comments