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
Deepu161Deepu161 

How can I get 100% code coverage for this class

public class generatePasswordController {
    public Boolean alert{get;set;}
    public String msg{get;set;}
    public String redirecturl{get;set;}
    private final Employee__c o;
    Public Id EmployeeId {get;set;}
    public generatePasswordController (ApexPages.StandardController stdController) {
        alert=false;
        this.o = (Employee__c)stdController.getRecord();
        EmployeeId = ApexPages.currentPage().getParameters().get('Id');
    }
    public PageReference CreateAndSendEmail() {
        String ats_URL = Label.ATS_URL;

        list<Employee__c> EmpDetails = [SELECT Id, Name, Site_Username__c, Site_Password__c,Email_1st__c FROM Employee__c WHERE id = : EmployeeId];
        if(EmpDetails[0].Email_1st__c == Null || EmpDetails[0].Email_1st__c == ''){
            alert = true;
            redirecturl = '/'+EmpDetails[0].Id;
            return null;
        }
        else{
            //Generating random passwords
            
            TimeCardEntrySiteURL__c mc = TimeCardEntrySiteURL__c.getInstance('TimeCardEntry');
            string urlcode = mc.URLforTimeCardEntry__c;
            
            Integer len = 10;
            Blob blobKey = crypto.generateAesKey(128);
            String key = EncodingUtil.convertToHex(blobKey);
            String pwd = key.substring(0,len);
            System.debug('************ '+pwd);
            
            for(employee__c E:EmpDetails){
                E.Site_Username__c = E.Email_1st__c + '.ats';
                E.Site_Password__c = pwd;
            }
            
            update Empdetails;
            OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'it@accesstechnologys.com'];
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String messageBody;
            String[] toAddresses = new String[] {EmpDetails[0].Email_1st__c};
            mail.setToAddresses( toAddresses ); 
            mail.setOrgWideEmailAddressId(owea.get(0).Id);
            mail.setReplyTo('donotreply@salesforce.com'); 
            //mail.setSenderDisplayName('IT Administrator'); 
            mail.setSubject('Access Technology Solutions Time Card Portal : Username And Password' ); 
            mail.setBccSender(false); 
            
            mail.setUseSignature(false);
            //messageBody = '<html><body>Dear ' +EmpDetails[0].Name  + ',<br></br><br></br> Below is the information for logging into the Time Card Portal <br></br><br></br> UserName : '+EmpDetails[0].site_username__c +' <br><br> Password :'+EmpDetails[0].site_password__c +' <br><br> You can Update password related with your account by clicking on this link below:<br><br>'+ UrlCode +'/UpdatePswdPage?Employeeid='+ EmpDetails[0].Id+'&EmployeeName='+EmpDetails[0].site_username__c+' <br></br><br></br>Regards,<br></br>Access Technology Solutions - IT Support<br></br>Office: (800) 637-9569 - 801 | Fax : (800) 637-9569<br></br>www.accesstechnologys.com</body></html>'; 
            messageBody = '<html><body>Dear ' +EmpDetails[0].Name  + ',<br></br><br></br> Below is the information for logging into the Time Card Portal <br></br><br></br> UserName : '+EmpDetails[0].site_username__c +' <br><br> Password :'+EmpDetails[0].site_password__c +' <br><br> You can Update password related with your account by clicking on this link below:<br><br>'+ ats_URL+'?update=updatepassword&Employeeid='+ EmpDetails[0].Id+'&username='+EmpDetails[0].site_username__c+' <br></br><br></br>Regards,<br></br>Access Technology Solutions - IT Support<br></br>Office: (800) 637-9569 - 801 | Fax : (800) 637-9569<br></br>www.accesstechnologys.com</body></html>';
            mail.setHtmlBody(messageBody);
            mail.setPlainTextBody(messageBody);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); 
            
            PageReference employeePage = new PageReference('/' + EmployeeId);
            employeePage.setRedirect(true);
            return employeePage;
        }
   } 
}