1

I am new to Salesforce and facing a problem. I am working on mail sending page. I have two visualforce pages and one controller class for both. From first page when i press the button to go to second page then variable value is settled but when on pressing button on second page the variable value is changed.

Can somebody will help me plz.

First Page

<apex:page controller="AccountAndContactsListController">
    <apex:form >
        <apex:pageBlock title="AccountsList">
            <apex:pageBlockTable value="{!accounts}" var="acct">
                <apex:column value="{!acct.Name}"/>
                <apex:column value="{!acct.NumberOfEmployees}"/>
                <apex:column value="{!acct.Industry}"/>
                <apex:column value="{!acct.AnnualRevenue}"/>
                <apex:column >
                    <apex:commandButton value="Get Contacts" action="{!getContacts}" reRender="actList">
                        <apex:param name="Accnts" value="{!acct.Id}" assignTo="{!actId}"/>
                    </apex:commandButton>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>

        <apex:pageBlock title="Contacts List">
            <apex:pageBlockTable value="{!contacts}" var="ct" id="actList">
                <apex:column value="{!ct.Name}"/>
                <apex:column value="{!ct.Email}"/>
                <apex:column value="{!ct.Phone}"/>
                <apex:column value="{!ct.Department}"/>
                <apex:column >
                    <apex:commandButton value="Mail" action="{!openMailPage}" reRender="actList">
                        <apex:param value="{!ct.Email}" assignTo="{!ctMail}" name="contactEmail" />
                    </apex:commandButton>
                </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock>

    </apex:form>
</apex:page>

Second Page

<apex:page controller="AccountAndContactsListController">
    <apex:form >
        <center>
            <apex:pageBlock title="Email Information">
                <h2>Subject :  </h2>
                <apex:inputTextarea cols="40" rows="1" value="{!sbjct}" id="sbjctId"></apex:inputTextarea>
                <br/> <br/> <br/>
                <h2>Body :  </h2>
                <apex:inputTextarea cols="70" rows="12" value="{!content}"></apex:inputTextarea>
                <br/> <br/>
                <apex:commandButton value="Send" action="{!sendEmail}">
                    <apex:param value="{!sbjct}" assignTo="{!sbjct}" name="Subject"/>
                    <apex:param value="{!content}" assignTo="{!content}" name="Content"/>
                </apex:commandButton>
            </apex:pageBlock>
        </center>

    </apex:form>
</apex:page>

Controller

public class AccountAndContactsListController {
    public List<Account> accounts{get; set;}
    public List<Contact> contacts{get; set;}
    public String actId{get; set;}
    public String ctMail{get; set;}
    public String sbjct{get; set;}
    public String content{get; set;}

    public AccountAndContactsListController(){
        accounts = [SELECT Id, Name, NumberOfEmployees, Industry, AnnualRevenue FROM Account];
        contacts = null;
        actId = null;
        ctMail = null;
        sbjct = null;
        content = null;
        system.debug('Constructor is called.');
    }

    public void getContacts(){
        contacts = [SELECT id, Name, Email, Phone, Department FROM Contact WHERE AccountId = :actId];
    }

    public PageReference openMailPage(){
        system.debug('In OpenMailPage Function ctMail value : '+ctMail);
        PageReference pr = new PageReference('/apex/emailSendigPage');
        pr.setRedirect(true);
        return pr;
    }

    public void sendEmail(){
        System.debug('In sendEmail Function ctMail value : '+ctMail);
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        List<String> emailAddresses = new List<String>();
        emailAddresses.add(ctMail);
        message.setToAddresses(emailAddresses);
        message.setSubject(sbjct);
        message.setPlainTextBody(content);
        List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>{message};
        Messaging.SendEmailResult[] result = Messaging.sendEmail(messages);
        if(result[0].success)
            system.debug('Mail sent Successfully');
        else
            system.debug('Mail sending failed due to some error');
    }
}
1
  • 2
    Welcome to SFSE! Please take the tour and read How to Ask. You'll probably need to include additional details such as the Visualforce pages that you have (and likely the controller as well) for someone to be able to give you an accurate answer. You can add these details by making an edit to your question (comments are limited in length and formatting options, and are best thought of as temporary. As such, additional details like these are better off as an edit to your question)
    – Derek F
    Sep 18 '18 at 15:37
1

Looks to me like your issue is likely the pr.setRedirect(true); line in your controller's openMailPage() method.

The documentation on setRedirect() should shed some light on what's going on. From that page...

This type of redirect performs an HTTP GET request, and flushes the view state, which uses POST. If set to false, the redirect is a server-side forward that preserves the view state if and only if the target page uses the same controller and contains the proper subset of extensions used by the source page.

The viewstate is what Salesforce uses to transfer data from one visualforce page to another (assuming they use the same controller/extensions). Your setRedirect(true) means that the viewstate is being flushed, and the data from your first page is therefore not making it to your second page.

changing pr.setRedirect(true); to pr.setRedirect(false); should solve your problem.

3
  • Hi Derek. It is not working. When i set Redirect to false then second page is not opened but this message is displayed The page you submitted was invalid for your session. Please refresh your page and try again Sep 18 '18 at 16:40
  • @KHAWAJAABDURREHMAN Are you certain that your session didn't time out? I'd try navigating to your first page again, forcing a cache flush on refresh by pressing ctrl + F5, and then trying to push your button to go to your second page.
    – Derek F
    Sep 18 '18 at 16:51
  • Hi! If i remove reRender from commandButton of first page then second page is opening. But on removing reRender the mailAddress value is not being passed in controller. Sep 18 '18 at 18:16

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.