I have custom button which send email, I would like notify the user regarding if the action fail/success - but the message always present in new blank page.

I wish the message to be presented in the same page where the button is - similar to this.

Any Ideas?

Thank you.

enter image description here My Visual Force code:

<apex:page standardController="License_Request__c" extensions="send_mail_controller" action="{!send_mail}">
<apex:pageMessages id="msgs"/>


public class send_mail_controller 
string account_name;
License_Request__c license;
Contact contact;
User current_user;

public send_mail_controller(ApexPages.StandardController controller)
    license = (License_Request__c) controller.getRecord();

public PageReference send_mail()
    id current_user_id = UserInfo.getUserId();

    License_Request__c license_request = [select account__r.name, Contact_Name__c from License_Request__c where id =: license.id][0];

    if(license_request.Contact_Name__c == null || license_request.Contact_Name__c == '')
        ApexPages.AddMessage(new ApexPages.Message(ApexPages.Severity.Error,' email is missing, thank you.'));
        return null;

    //send mail..

PageReference pg = new PageReference('/'+license.Id);           

ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Lincense sent successfully');


return null;


  • why you have added PageReference ? – Himanshu Jan 7 '18 at 14:18
  • Since according to what I saw - VisualFoce face functions needs to return PageReference. – aralele Jan 7 '18 at 14:31
  • that is not true. as you have added return null; you can remove that line pg.setRedirect(true); and PageReference pg = new PageReference('/'+license.Id); – Himanshu Jan 7 '18 at 14:34
  • Please see my controller in the if(license_request.Contact_Name__c == null...) There I return null - and face the same behavior. P.S - I the 'CONFIRM' case it doesn't matter if I use redirect or not - both present blank page. – aralele Jan 7 '18 at 14:40
  • 2
    if it is standard layout page button and you are not taking any input from user. you should implement web service method callout and show message using prompt. developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… and salesforce.stackexchange.com/a/13209/17316 – Himanshu Jan 7 '18 at 15:20

Got it..

In this question - the button was created in the object layout and on click called to specific VF page. The correct way to have such case is create VF which would include apex:commandButton and add this VF to the layout.

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.