function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ray allenray allen 

I am trying to reset a logged in user password

I need finalizing this apex class to reset a password for the user and log the current user out of salesforce.  Here is my code any ideas I get this error message.
public class resetyourpassword {
  String user=userinfo.getuserId();
  system.resetPassword(user,true);
   PageReference demoPage = new pagereferenct('/secur/logout.jsp');
    demoPage.setRedirect(true);
}



I get an error unexpected token: ',' in line 3  where  system.resetPassword(user,true); id located
Best Answer chosen by ray allen
William TranWilliam Tran
It means that you need to return something when the if is not true:
 
if(userAgent.contains('android')) {
   	system.resetPassword(user,sendUserEmail);
    PageReference demoPage = new PageReference('/secur/logout.jsp');
    demoPage.setRedirect(true);
           return demoPage;}

return ......
Thx
 

All Answers

Shingo YamazakiShingo Yamazaki
It's simply because you didn't define the method.
 
public class resetyourpassword {

    public void execute() {
        String user=userinfo.getuserId();
        system.resetPassword(user,true);
        PageReference demoPage = new PageReference('/secur/logout.jsp');
        demoPage.setRedirect(true);
   }
}

I defined the method with no return value, but I'm not sure if you need to  return a value or not.

 
William TranWilliam Tran
Ray,

Welcome to the Developer's Forum, Since you are new, here are some guidelines: 

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you. 

This will help keep the forum clean and help future users determine what answers are useful and what answer was the best in resolving the user's issue. 

Thanks
 
William TranWilliam Tran
I looks like Shingo solution might fix your issue, if not you can 


Try explicitly passing a boolean in and using ID vs String.
 
public class resetyourpassword {
ID user=userinfo.getuserId();
Boolean sendUserEmail = true;
  system.resetPassword(user,sendUserEmail);
   PageReference demoPage = new pagereferenct('/secur/logout.jsp');
    demoPage.setRedirect(true);
}
Thx
 
William TranWilliam Tran
Actually, since you have pagereference you should return it.
 
public class resetyourpassword {

    public PageReference execute() {
        ID user=userinfo.getuserId();
	Boolean sendUserEmail = true;
  	system.resetPassword(user,sendUserEmail);
        PageReference demoPage = new PageReference('/secur/logout.jsp');
        demoPage.setRedirect(true);
        return demoPage;
   }
}
SFDCmack08180939826349907SFDCmack08180939826349907
Thanks so much it working
William TranWilliam Tran
Ray,  glad it worked out.

Be sure to give thumbs up for any answer that helped you and mark 1 best answer.

Thx
ray allenray allen
Would you be to help with the second error message I dont know what it means it saids Non-void method might not return a value or might have statement after a return statement in line 7 contains   if(userAgent.contains('android')) {

 
public class resetyourpassword {
   
    public PageReference execute() {
    String userAgent =ApexPages.currentPage().getHeaders().get('USER-AGENT');
    ID user=userinfo.getuserId();
	Boolean sendUserEmail = true;    
    if(userAgent.contains('android')) {
   	system.resetPassword(user,sendUserEmail);
    PageReference demoPage = new PageReference('/secur/logout.jsp');
    demoPage.setRedirect(true);
           return demoPage;}
   }
}

 
ray allenray allen
I am trying to block users from accessing salesforce through the browser in Android devices.
William TranWilliam Tran
It means that you need to return something when the if is not true:
 
if(userAgent.contains('android')) {
   	system.resetPassword(user,sendUserEmail);
    PageReference demoPage = new PageReference('/secur/logout.jsp');
    demoPage.setRedirect(true);
           return demoPage;}

return ......
Thx
 
This was selected as the best answer
ray allenray allen
Thanks I put return null.
ray allenray allen
it worked
William TranWilliam Tran
Sounds good, don't forget to give thumbs up to the answers that helped you.

Thx