0

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***********');
}
3
  • try adding value="javascript:void(0)" to output link. Jul 2, 2019 at 6:38
  • @rahulgawale Thanks Rahul. It is working as expected. Would like to know the meaning if void(0), if you can explain a bit that it would be better for understanding. And thanks a ton. Jul 2, 2019 at 7:11
  • I have added a detailed explanation in the answer section. Jul 2, 2019 at 7:27

1 Answer 1

0

Outputlink is nothing but anchor tag of html at the end, and the value attribute of apex:outputlink is nothing but href of an anchor tag in plain HTML. By default, Outputlink reloads the page on single click and that is the issue here. Using value="javascript:void(0)" should solve your problem.

Why use it:

To prevent the page reloading upon a single click, we can use JavaScript:void(0); within the anchor link.

Know more about anchor tag

Know more about outputlink

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.