0

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"/>
</apex:page>

Controller:

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');
ApexPages.addMessage(msg);

pg.setRedirect(true);

return null;


  }

}
9
  • why you have added PageReference ?
    – Himanshu
    Jan 7, 2018 at 14:18
  • Since according to what I saw - VisualFoce face functions needs to return PageReference.
    – aralele
    Jan 7, 2018 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, 2018 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, 2018 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, 2018 at 15:20

1 Answer 1

0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .