It is safe, but it should NEVER be taken if you are taking nitroglycerine (which is used to treat coronary artery disease) as the combination can lower blood pressure severely.
No, it will not give you a heart attack.
Sildenafil (Viagra) relaxes and therefore lowers systolic and diastolic blood pressure slightly. It is safe, even for coronary disease patients with stable angina.
It is not the medication but the activity after taking the medication that gives you some increase of risk of a heart attack.
A patient with coronary artery disease suddenly engaging in the physical exercise associated with sexual intercourse, has an increased risk of myocardial infarction or death. Sexual intercourse is estimated to increase the risk of myocardial infarction by a factor of 2, which is still only a very small risk.