0

I have below action function in vf page

<apex:actionFunction name="changeOwnership" action="{!changeOwnership}" rerender="changeOwnershipId" oncomplete="ownerShipChangedComplete()">

and in the controller I have below method

 public void changeOwnership() { 
    String usId = ApexPages.CurrentPage().getParameters().get('usId');
    String userEmail = ApexPages.CurrentPage().getParameters().get('userEmail');
    try{       
          sendEmailMethod(usId,userEmail);
    }       
     catch(Exception e){
         System.debug(e);
     }   
    }

the send Email Method is below

public void sendEmailMethod(String usId,String userEmail){
    try{           
            System.debug('sending email');
             Messaging.singleEmailmessage Email = new Messaging.singleEmailmessage();
             email.setsubject('test');
             email.setPlainTextbody('test');
             email.setToAddresses(new String[] { usId });
             email.saveAsActivity = false;
             Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
             System.debug('###email sent');
    }
    catch(exception e){
        System.debug('cannot send email'+e);
    }
}

I tried to run the send email code in developer console and it works fine and I get the email but it is not working through my action function. I checked the debug statements and the send method is being called with success. I checked the deliveribility as well which is assigned to all emails.

3
  • Does the URL you are using to access the VF page? contain the usId and userEmail parameters?
    – Eric
    Aug 30, 2017 at 2:44
  • Yes it does. Actually I solved the problem. It was with the sending email. The user logged in from which I was sending the email was invalid. So changing the email to valid email did the trick. Thanks Aug 31, 2017 at 18:24
  • Cool. Was just checking if the URL contained the params as if it did then no need to pass them in the action function
    – Eric
    Aug 31, 2017 at 18:26

1 Answer 1

0

You are not passing usId and userEmail which are the parameters to the method.

This usId has been used to assign setToAddresses which is mandatory for sending emails. That's why email functionality is not working because of missing values.

Use <apex:param/> and pass necessary values to controller.

<apex:actionFunction name="changeOwnership" action="{!changeOwnership}" rerender="changeOwnershipId" oncomplete="ownerShipChangedComplete()">
    <apex:param id="usId" name="usId" value=""/>
    <apex:param id="userEmail", name="userEmail" value=""/>
</apex:actionFunction>
1
  • As an aside you do not need to pass the parameters if the URL already contains them.
    – Eric
    Aug 30, 2017 at 2:43

You must log in to answer this question.

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