I have a command button inside vf page, which is used to send email to contact. after sending email, I want to display alert message with total no of Contacts.
I'm successfully able to send email but in alert contact list is always zero.
I guess my oncomplete is getting executed as soon as action method is getting called
<apex:commandButton value="Send Email" action="{!sendEmail}"
oncomplete="afterSendingEmail()"/>
</div>
</apex:form>
<script>
function afterSendingEmail(){
if('{!contactList.size}' == 0){
alert('No contact');
}
else {
alert('Email has been sent');
}
}
</script>
controller:
public List<Contact> contactList{ get; set; }
public void sendEmail(){
contactList= [Select Id, Email FROM Contact WHERE field__c= :recordID];
List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
for(Contact con: contactList){
Messaging.SingleEmailMessage singleEmail = sendEmail(con);
allmsg.add(singleEmail);
}
try {
if(contactList.size()>0){
Messaging.sendEmail(allmsg,false);
}
}