4

I've migrated my portal to community now and all the functionalities are working fine. except one thing. I can't seem to send an email that is executed with an apex class.
I want to send a "thank you for using our services, your details have been changed" if the executiin is success

This is what I've got so far for the email sending:

   // setup email body
                String messageContents = '<h1>Heathrow Commercial Telecoms Details change confirmation</h1>\n' +
                   '<p> Your details have been changed  sucessfully </p>\n"; 

                    OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = '@sia.aeo'];
                    Messaging.reserveSingleEmailCapacity(3);
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

                    if ( owea.size() > 0 ) {
                    mail.setOrgWideEmailAddressId(owea.get(0).Id);
                    }



 String[] toAddresses = new String[] {email}; 
                    String[] bccAddresses = new String[] {'tracy@sia.aro', 'ss@ocus-TM.com'};
                    mail.setToAddresses(toAddresses);
                    mail.setBccAddresses(bccAddresses);
                    // Specify the address used when the recipients reply to the email. 
                    mail.setReplyTo('heathrow@sita.aero');

                    mail.setSubject('Commercial Telecoms Innovation Event Confirmation');
                    mail.setBccSender(false);
                    mail.setUseSignature(false);
                    mail.setHtmlBody(messageContents);
                    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

This is the my profile page controller:

public with sharing class MyProfilePageController {

private User user;
private boolean isEdit = false;

public User getUser() {
    return user;
}

public MyProfilePageController() {
    user = [SELECT id, email, username, usertype, communitynickname, timezonesidkey, languagelocalekey, firstname, lastname, phone, title,
            street, city, country, postalcode, state, localesidkey, mobilephone, extension, fax, contact.email
            FROM User
            WHERE id = :UserInfo.getUserId()];
    // guest users should never be able to access this page
    if (user.usertype == 'GUEST') {
        throw new NoAccessException();
    }
}

public Boolean getIsEdit() {
    return isEdit;
}

public void edit() {
    isEdit=true;
}

public void save() {
    if (user.contact != null) {
        setContactFields(user.contact, user);
    }

    try {
        update user;
        if (user.contact != null) {
            update user.contact;
        }
        isEdit=false;
    } catch(DmlException e) {
        ApexPages.addMessages(e);
    }
}

public PageReference changePassword() {
    return Page.calypso_changeppass;
}

public void cancel() {
    isEdit=false;
    user = [SELECT id, email, username, communitynickname, timezonesidkey, languagelocalekey, firstname, lastname, phone, title,
            street, city, country, postalcode, state, localesidkey, mobilephone, extension, fax, contact.email
            FROM User
            WHERE id = :UserInfo.getUserId()];
}

public static void setContactFields(Contact c, User u) {
    c.title = u.title;
    c.firstname = u.firstname;
    c.lastname = u.lastname;
    c.email = u.email;
    c.phone = u.phone;
    c.mobilephone = u.mobilephone;
    c.fax = u.fax;
    c.mailingstreet = u.street;
    c.mailingcity = u.city;
    c.mailingstate = u.state;
    c.mailingpostalcode = u.postalcode;
    c.mailingcountry = u.country;
}

}


Could someone help me with putting the first bit of code into the second one. So if someone saves his changes sucessfully it will send a message to that user.

///////////////////////////////EDIT//////////////////////////////////

public with sharing class MyProfilePageController {

    private User user;
    private boolean isEdit = false;

    public User getUser() {
        return user;
    }



      public void sendSuccessEmail() {



                    user = [SELECT id, email, contact.email
                    FROM User
                    WHERE id = :UserInfo.getUserId()];


                    String messageContents = '<h1>Heathrow Commercial Telecoms Details change confirmation</h1>\n' +
                   '<p> Your details have been changed  sucessfully </p>\n'; 

                    OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'heathrow@sita.aeo'];
                    Messaging.reserveSingleEmailCapacity(3);
                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

                    if ( owea.size() > 0 ) {
                    mail.setOrgWideEmailAddressId(owea.get(0).Id);
                    }


                    String[] toAddresses = new String[] {user.email}; 
                    mail.setToAddresses(toAddresses);
                    mail.setReplyTo('heathrow@sita.aero');

                    mail.setSubject('Details Change');
                    mail.setBccSender(false);
                    mail.setUseSignature(false);
                    mail.setHtmlBody(messageContents);
                    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });


    }


    public MyProfilePageController() {
        user = [SELECT id, email, username, usertype, communitynickname, timezonesidkey, languagelocalekey, firstname, lastname, phone, title,
                street, city, country, postalcode, state, localesidkey, mobilephone, extension, fax, contact.email
                FROM User
                WHERE id = :UserInfo.getUserId()];
        // guest users should never be able to access this page
        if (user.usertype == 'GUEST') {
            throw new NoAccessException();
        }
    }

    public Boolean getIsEdit() {
        return isEdit;
    }

    public void edit() {
        isEdit=true;
    }

    public void save() {
        if (user.contact != null) {
            setContactFields(user.contact, user);
        }

        try {
            update user;
            if (user.contact != null) {
                update user.contact;
                sendSuccessEmail();

            }
            isEdit=false;
        } catch(DmlException e) {
            ApexPages.addMessages(e);
        }
    }

    public PageReference changePassword() {
        return Page.calypso_changeppass;
    }

    public void cancel() {
        isEdit=false;
        user = [SELECT id, email, username, communitynickname, timezonesidkey, languagelocalekey, firstname, lastname, phone, title,
                street, city, country, postalcode, state, localesidkey, mobilephone, extension, fax, contact.email
                FROM User
                WHERE id = :UserInfo.getUserId()];
    }

    public static void setContactFields(Contact c, User u) {
        c.title = u.title;
        c.firstname = u.firstname;
        c.lastname = u.lastname;
        c.email = u.email;
        c.phone = u.phone;
        c.mobilephone = u.mobilephone;
        c.fax = u.fax;
        c.mailingstreet = u.street;
        c.mailingcity = u.city;
        c.mailingstate = u.state;
        c.mailingpostalcode = u.postalcode;
        c.mailingcountry = u.country;
    }
}


Above there is a code that sends an email and updates user information. But the page reference is all messed up. Before when you pressed edit and save it just went back to the same page with an edit option. NOW it either redirects to to a blank page or saves but doesnt refresh. anyone has any ideas ?

//////////////////////CODE FOR THE VISUAL FORCE PAGE ///////////////////////

<apex:page ></i>

    <apex:composition template="calypso_base">
        <apex:define name="hero-content">
            <section class="pageheader-default text-center">
                <div class="semitransparentbg">
                    <h1 class="animated fadeInLeftBig notransition">{!$Label.site.my_profile_page}</h1>
                    <h2 class="animated fadeInRightBig notransition container page-description">
                    </h2>
                </div>
            </section>
        </apex:define>

        <apex:define name="content">




<section class="container">
    <div class="row">


      <apex:form id="theForm">







              <apex:outputPanel style="padding:50px 50px 50px 50px" id="userDetail">
               <apex:pageBlock title="{!$Label.Edit}" id="editUserDetail" rendered="{!isEdit}">
                 <apex:pageMessages />
                 <apex:pageBlockButtons id="editButtons" location="top">
                   <apex:commandButton id="save" value="{!$Label.site.save}" action="{!save}" rerender="userDetail"/>
                   <apex:commandButton id="cancel" value="{!$Label.site.cancel}" action="{!cancel}" rerender="userDetail" immediate="true"/>
                 </apex:pageBlockButtons>
                 <apex:pageBlockSection id="editUserSection" columns="1" title="{!$Label.site.user_information}">
                     <apex:inputField id="editusername" value="{!user.username}"/>
                     <apex:inputField id="edittimezone" value="{!user.timezonesidkey}"/> 
                     <apex:inputField id="editlocale" value="{!user.localesidkey}"/> 
                     <apex:inputField id="editlanguage" value="{!user.languagelocalekey}"/> 
                     <apex:inputField id="editcommunityNickname" value="{!user.communityNickname}"/> 
                 </apex:pageBlockSection>
                 <apex:pageBlockSection id="editContactSection" columns="2" title="{!$Label.site.contact_information}">
                     <apex:inputField id="editfirstName" value="{!user.firstName}"/> 
                     <apex:inputField id="editemail" value="{!user.email}"/> 
                     <apex:inputField id="editlastName" value="{!user.lastName}"/> 
                     <apex:inputField id="editphone" value="{!user.phone}"/> 
                     <apex:inputField id="edittitle" value="{!user.title}"/> 
                     <apex:inputField id="editextension" value="{!user.extension}"/> 
                     <apex:pageBlockSectionItem />
                     <apex:inputField id="editfax" value="{!user.fax}"/>
                     <apex:pageBlockSectionItem />
                     <apex:inputField id="editmobile" value="{!user.mobilephone}"/> 
                   </apex:pageBlockSection>
                   <apex:pageBlockSection id="editAddressInformation" columns="1" title="{!$Label.site.address_information}">
                     <apex:inputField id="editstreet" value="{!user.street}"/> 
                     <apex:inputField id="editcity" value="{!user.city}"/> 
                     <apex:inputField id="editstate" value="{!user.state}"/> 
                     <apex:inputField id="editpostalcode" value="{!user.postalcode}"/> 
                     <apex:inputField id="editcountry" value="{!user.country}"/> 
                   </apex:pageBlockSection>
               </apex:pageBlock>         

                <apex:pageBlock title="{!$Label.site.my_profile}" id="viewUserDetail" rendered="{!!isEdit}">
                     <apex:pageBlockButtons id="detailButtons" location="top">
                       <apex:commandButton id="edit" value="{!$Label.site.edit}" action="{!edit}" rerender="userDetail"/>
                       <apex:commandButton id="changePassword" value="{!$Label.site.change_password}" action="{!changePassword}"/>
                     </apex:pageBlockButtons>
                     <apex:pageBlockSection id="viewUserSection" columns="2" title="{!$Label.site.user_information}">
                         <apex:outputField id="detailusername" value="{!user.username}"/>
                         <apex:outputField id="detailtimezone" value="{!user.timezonesidkey}"/> 
                         <apex:outputField id="detaillocale" value="{!user.localesidkey}"/> 
                         <apex:outputField id="detaillanguage" value="{!user.languagelocalekey}"/> 
                         <apex:outputField id="detailcommunityNickname" value="{!user.communityNickname}"/> 
                     </apex:pageBlockSection>
                     <apex:pageBlockSection id="viewContactSection" columns="2" title="{!$Label.site.contact_information}">
                         <apex:outputField id="detailemail" value="{!user.email}"/> 
                         <apex:outputField id="detailfirstName" value="{!user.firstName}"/> 
                         <apex:outputField id="detaillastName" value="{!user.lastName}"/> 
                         <apex:outputField id="detailtitle" value="{!user.title}"/> 
                         <apex:outputField id="detailphone" value="{!user.phone}"/> 
                         <apex:outputField id="detailstreet" value="{!user.street}"/> 
                         <apex:outputField id="detailcity" value="{!user.city}"/> 
                         <apex:outputField id="detailstate" value="{!user.state}"/> 
                         <apex:outputField id="detailpostalcode" value="{!user.postalcode}"/> 
                         <apex:outputField id="detailcountry" value="{!user.country}"/> 
                         <apex:outputField id="detailextension" value="{!user.extension}"/> 
                         <apex:outputField id="detailfax" value="{!user.fax}"/> 
                         <apex:outputField id="detailmobile" value="{!user.mobilephone}"/> 
                      </apex:pageBlockSection>
                   </apex:pageBlock>
                </apex:outputPanel>
             </apex:form>






                    </div>


                </section>
            </apex:define>
        </apex:composition>
    </apex:page>

1 Answer 1

0

You can place your email sending code in a new function say sendSuccessEmail and call it as below:

public void save() {
    if (user.contact != null) {
        setContactFields(user.contact, user);
    }

    try {
        update user;
        if (user.contact != null) {
            update user.contact;
        }
        isEdit=false;
    sendSuccessEmail();
    } catch(DmlException e) {
        ApexPages.addMessages(e);
    }
}

As you are calling this function after DML it will only fire when everything goes good with save operation.

10
  • Okay, So do I just put my code into a function called sendSucessEmail and then call it from the if statement
    – user26057
    Jul 13, 2016 at 11:03
  • 1
    Yes it will work if there are not any problem with email sending code. Jul 13, 2016 at 11:04
  • OKay it works whenever the details are saved it sends an email to a customer. but the page redirect doesnt work now it goes to a blank page or doesnt go back to the original state. any ideas ?
    – user26057
    Jul 13, 2016 at 12:18
  • I can not see any impact of this edit on page redirection. If you expect control to go to some other page use Pagereference return type on Save function and return appropriate page at last. Jul 13, 2016 at 13:24
  • I did I tried to put a function as a page reference, and redirect it from the function to the same page, Also tried to place the execution of that function in different places, Still doesn't work :/
    – user26057
    Jul 13, 2016 at 13:26

Your Answer

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