Nowadays, heart diseases have become a leading cause of mortality worldwide and it affects a huge number of individuals. The early and accurate prediction of heart disease risk factors plays a crucial role in preventing opposing results. Additionally, it is necessary to recognize heart disease quickly and accurately by analyzing patient's data. This paper proposed a novel approach for predicting heart disease through machine learning techniques. The proposed Bat Algorithm (BA) and Particle Swarm Optimization (PSO) based Random Forest (RF), named BAPSO-RF is utilized for selecting optimum features that can enhance the heart-disease prediction accuracy. The proposed BAPSO-RF is evaluated on UCI heart disease dataset which contains 14 attributes and 270 records. The proposed BAPSO-RF model attains better results by utilizing metrics like accuracy, precision, recall, and f1-score values of about 98.71%, 98.67%, 98.23%, and 98.45% correspondingly which ensures early and accurate prediction of heart disease compared to existing techniques like hybrid of Genetic Algorithm and Particle Swarm Optimization (PSO) with Random Forest (GAPSO-RF), stacked Genetic Algorithm (GA) and Genetic Algorithm with Radial Basis Function (GA-RBF).