Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.07 KB | None | 0 0
  1. public with sharing class ConfirmationPageController {
  2.        
  3.        
  4.         //public String userName {get; set;}
  5.         public String password {get; set;}
  6.         public String confirmPassword {get; set;}
  7.         public Contact contact2 {get; set;}
  8.        
  9.         public Boolean index {get; set;}
  10.         public Boolean incorrectLink {get; set;}
  11.         public Boolean userCreated {get; set;}
  12.         public Boolean signUperror {get; set;}
  13.         public Boolean showMessages {get; set;}
  14.         private static Id PORTAL_ACCOUNT_ID = '001A0000005FfqY';
  15.        
  16.         public ConfirmationPageController() {
  17.                
  18.                 String contactID = ApexPages.currentPage().getParameters().get('id');
  19.                
  20.                 index = true;
  21.                 incorrectLink = false;
  22.                 userCreated = false;            
  23.                 signUperror = false;
  24.                 showMessages = false;
  25.                 password = '';
  26.                 confirmPassword = '';
  27.                
  28.                 try {                          
  29.                         contact2 = [SELECT Id,AccountId,FirstName,email,lastName,Speciality__c FROM Contact WHERE id = :contactID ];                    
  30.                 } catch (Exception e) {
  31.                         incorrectLink = true;
  32.                         index = false;
  33.                 }
  34.                 Apexpages.getMessages().clear();
  35.         }
  36.        
  37.         public PageReference createUser() {
  38.        
  39.        
  40.            Boolean error = false;      
  41.            showMessages = true;
  42.                
  43.            if (password == null || password == '') {
  44.                   ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please, Insert password'));
  45.           error = true;
  46.            }
  47.                        
  48.            if (confirmPassword == null || confirmPassword == '') {
  49.                   ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Please, Insert the password confirmation'));
  50.                   error = true;        
  51.            }            
  52.            
  53.            if (error) {
  54.                  return null;
  55.            }
  56.            
  57.            if (password != confirmPassword) {
  58.                   ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'The confirmation password is wrong'));
  59.                  
  60.                   return null;
  61.            }
  62.            
  63.            User u = new User();
  64.        u.ContactId = contact2.id;
  65.        u.Username = contact2.Email;
  66.        u.firstName = contact2.FirstName;
  67.        u.LastName = contact2.LastName;
  68.        u.Email = contact2.Email;      
  69.        u.ProfileId = Label.ProfileDoctor;
  70.        
  71.        u.CommunityNickname = contact2.Email;
  72.        
  73.        String userId = Site.createPortalUser(u,PORTAL_ACCOUNT_ID, password);
  74.  
  75.        if (userId != null) {
  76.          if (password != null && password.length() > 1) {
  77.          
  78.          userCreated = true;
  79.          index = false;
  80.          Site.login(contact2.Email, password, '/BackgroundInfo');
  81.          return null;
  82.          }
  83.        }        
  84.                        
  85.           PageReference page = new Pagereference('/BackgroundInfo');                    
  86.           return null;
  87.         }
  88.        
  89.         public static testMethod void testing() {
  90.        
  91.         Contact c = new Contact();
  92.         c.FirstName = 'gonzalo';
  93.         c.LastName = 'sosa';
  94.         c.Email = 'test@email.com';
  95.        
  96.         insert c;
  97.        
  98.         Pagereference page = new Pagereference('/ConfirmationPage?id=' + c.Id);
  99.        
  100.         ConfirmationPageController controller = new ConfirmationPageController();
  101.        
  102.         Test.setCurrentPage(page);
  103.        
  104.         controller = new ConfirmationPageController();
  105.        
  106.         controller.createUser();
  107.        
  108.         controller.password = 'password';
  109.        
  110.         controller.createUser();
  111.        
  112.         controller.confirmPassword = 'password';
  113.        
  114.         controller.createUser();
  115.        
  116.         controller.createUser();
  117.        
  118.         }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement