I have a VF Page where one link is to generate email of current detail page content. Once the email has been triggered to the user we need to show pop-up msg that the email has been sent.
So i have a outputLink from which i used to call actionFunction, once the actionfunction gets executed we have declared one attribute of actionfunction call onComplete based on this action we are calling a js function which is showing the alert msg "Email has been sent."
But this alert is populating in every alternate clicks but it should be populating in every clicks because email has been triggered in every click.
Below are the code.
Vf Page
<apex:form >
<apex:actionFunction action="{!sendEmail}" oncomplete="onCompleteHandler();" name="callSendEmailMethod" reRender="success" >
</apex:actionFunction>
<li><apex:outputLink onClick="callSendEmailMethod()" rendered="{!showGenerateEmailLink}">{!$Label.CP_Action_GenerateEmail}</apex:outputLink></li>
</apex:form>
<!--JavaScript -->
<script>
function onCompleteHandler(){
alert("Email Has Been Sent.");
}
</script>
Controller function which used to send email :
public void sendEmail(){
system.debug('***********Inside sendEmail Method***********');
EmailTemplate template = [SELECT Id, Name, Body, Subject FROM EmailTemplate Where Name = 'Single Asset End Of Life Template'];
String[] setCcAddresses = new list<string> {};
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateId(template.id);
mail.setTargetObjectId(contactId);
mail.setWhatId(assetId);
mail.setCcAddresses(setCcAddresses);
mail.setUseSignature(false);
mail.setBccSender(false);
mail.setSaveAsActivity(false);
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
sentEmail = TRUE;
system.debug('***********Last Line Of sendEmail Method***********');
}
value="javascript:void(0)"
to output link.