Try the following code:
//The following code is used to refresh the data source
// Wrap everything in an anonymous function to avoid polluting the global namespace
function refreshMySql() {
$(document).ready(function() {
tableau.extensions.initializeAsync().then(function() {
// Since dataSource info is attached to the worksheet, we will perform
// one async call per worksheet to get every dataSource used in this
// dashboard. This demonstrates the use of Promise.all to combine
// promises together and wait for each of them to resolve.
let dataSourceFetchPromises = [];
// Maps dataSource id to dataSource so we can keep track of unique dataSources.
let dashboardDataSources = {};
// To get dataSource info, first get the dashboard.
const dashboard = tableau.extensions.dashboardContent.dashboard;
// Then loop through each worksheet and get its dataSources, save promise for later.
dashboard.worksheets.forEach(function(worksheet) {
dataSourceFetchPromises.push(worksheet.getDataSourcesAsync());
});
Promise.all(dataSourceFetchPromises).then(function(fetchResults) {
fetchResults.forEach(function(dataSourcesForWorksheet) {
dataSourcesForWorksheet.forEach(function(dataSource) {
if (!dashboardDataSources[dataSource.id]) { // We've already seen it, skip it.
dashboardDataSources[dataSource.id] = dataSource;
var datasourceName = dashboardDataSources[dataSource.id].name;
if (dashboardDataSources[dataSource.id].name == "ABC") {
refreshDataSource(dashboardDataSources[dataSource.id]);
console.log("refreshSql() was called for Datasource Name: 'ABC'");
}
}
});
});
});
}, function(err) {
// Something went wrong in initialization.
console.log('Error while Initializing: ' + err.toString());
});
});
// Refreshes the given dataSource.
function refreshDataSource(dataSource) {
dataSource.refreshAsync().then(function() {
//alert(dataSource.name + ': Refreshed Successfully');
console.log(dataSource.name + ': Refreshed Successfully');
});
}
Make the right move by enrolling now in Tableau Online Course to learn it.