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
NLTNLT 

Need help in writing test class for below apex class for code coverage?

Main class:
---------------
public class PR_Extension {
    public String RECORD_TYPE {get;set;}
    public String FormType {get;set;}
    public String PageIconID {get;set;}
    
    private Contact formContact, queriedContact;
    private Account formAccount, queriedAccount;
    private Credit_Request__c formCreditRequest;
    
    public String tempAccountId {get;set;}
    public String tempContactId {get;set;}
    
    public Boolean queriedAccountBoolean {get;set;}
    public Boolean queriedContactBoolean {get;set;}
    
    //new
    public Boolean inSF1 {get;set;}
    
    //Used for Contact Auto Complete
    public String soql{
        get{
            if (!String.isBlank(tempAccountId))
            {
                return  ('select Number_of_Lines_Req__c, firstname, lastname, Name, Phone, Email, MailingStreet, MailingCity, MailingState, MailingPostalCode, Retail_Store_Number__c, Store_Info__c, Store_Manager__c from Contact WHERE AccountId = \'' + tempAccountId + '\'');
            }
            else
            {
                return ('');
            }
            
        }set;}
    
    
    /*
* Constructor
*/
    public PR_Extension(ApexPages.StandardController stdController)
    {
        PageIconID = '';
        populateExistingObjects();
        createForm();
    }
    
    /*
* Save action
*/
    public PageReference saveprcreditrequest()
    {
        System.debug('inside save function');   
        Boolean isSuccessful = true;
        Savepoint sp = Database.setSavepoint();
        isSuccessful = StoreService.validateStoreNumber(formContact.Retail_Store_Number__c);
        if(isSuccessful){
            if (!String.isBlank(formContact.MailingState))
            {
                formContact.MailingState = formContact.MailingState.toUpperCase();
            }
            
            List<Account> tempAccounts = [Select Id FROM Account where id = :tempAccountId LIMIT 1];
          //RecordType RT = [Select Id, Name, DeveloperName from RecordType where SobjectType = 'Credit_Request__c' AND DeveloperName = :RECORD_TYPE limit 1];
            if (tempAccounts.size() > 0)
            {
                
                formContact.Accountid = tempAccounts[0].id;
                //System.debug('formContact.Accountid : '+formContact.Accountid);
                //formCreditRequest = new Credit_Request__c();
                //formCreditRequest.RecordTypeId=RT.id;
                formCreditRequest.Business_Name__c = tempAccounts[0].id;
            }
            else
            {
                formAccount.BillingStreet = formContact.MailingStreet;
                formAccount.BillingCity = formContact.MailingCity;
                formAccount.BillingState = formContact.MailingState;
                formAccount.BillingPostalCode = formContact.MailingPostalCode;
                
                if(formAccount.id != null){
                    //when creating a new account, if formAccount id is not null reset
                    formAccount.Id = null;
                }
                
                isSuccessful = SObjectService.insertSingleSobject(formAccount);
                
                if(isSuccessful){
                    
                    Account newAccount = [Select Id, name FROM Account where Id = :formAccount.id  LIMIT 1];
                    formContact.Accountid = newAccount.Id;
                    
                    formCreditRequest.Business_Name__c = newAccount.Id;
                }else{
                    Database.rollback(sp);
                    return null;
                }
            }
            
            List<Contact> tempContacts = [Select Id FROM Contact where id = :tempContactId LIMIT 1];
            
            if (tempContacts.size() > 0)
            {
                formCreditRequest.Business_Contact_Name__c = tempContacts[0].id;
            }
            else
            {
                
                if(formContact.id != null){
                    //when creating a new contact, if formContact id is not null reset
                    formContact.id = null;
                }
                
                formContact.Lead_Status__c = 'Credit Request';
                formContact.RSA_Name__c = formCreditRequest.RSA_Name__c;
                formContact.RSA_Dealer_Code__c = formCreditRequest.RSA_Dealer_Code__c;
                
                if(isSuccessful){
                    isSuccessFul = SObjectService.insertSingleSobject(formContact);
                }
                
                if(isSuccessFul){
                    
                    Contact newContact = [Select Id FROM Contact where id = :formContact.id LIMIT 1];
                    formCreditRequest.Business_Contact_Name__c = newContact.id;
                }else{
                    Database.rollback(sp);
                    return null;
                }
            }
            
            if(isSuccessful){
                if(SObjectService.saveResultInsertListProcessErrors(new List<Credit_Request__c>{formCreditRequest}))
                {
                    PageReference p = null;
                    if(!inSF1){
                        p = new PageReference('/'+formCreditRequest.id);
                    }else{
                        p = Page.SF1LeadSuccess;
                    }    
                    //PageReference p = new PageReference('/'+formCreditRequest.id);
                    p.setRedirect(true);
                    return p;
                }else{
                    Database.rollback(sp);
                    return null;
                }
            }
            else
            {
                Database.rollback(sp);
                return null;
            }
        }else{
            //Display the error message
            return null;
        }
    }
    
    /*
* Contact getter
*/
    public Contact getContact() {
        
        return formContact;
    }
    
    /*
* Account getter
*/
    public Account getAccount() {
        
        return formAccount;
    }
    
    // Update SOQL query with accountid
    
    public PageReference updateSOQL(){
        if (!String.isBlank(tempAccountId))
        {
            soql = 'select Number_of_Lines_Req__c,firstname, lastname, Name, Phone, Email, MailingStreet, MailingCity, MailingState, MailingPostalCode, Retail_Store_Number__c, Store_Info__c, Store_Manager__c from Contact WHERE AccountId = \'' + tempAccountId + '\'';
        }
        else
        {
            soql = '';
        }
        
        return null;
    }
    
    // Credit Request getter
    
    public Credit_Request__c getCreditRequest() {
        
        return formCreditRequest;
    }
    
    /*
* Initialize objects
*/
    public void createForm() {
        //get current user
        User currentUser = [select id, SAP_Store_Number__c, Retail_Store_List__c from User where id =: UserInfo.getUserId()];
        System.debug('### Record Type : ' +RECORD_TYPE );
        RecordType crRT = [Select Id, Name, DeveloperName from RecordType where SobjectType = 'Credit_Request__c' AND DeveloperName = :RECORD_TYPE limit 1][0];
        String LinkName = '%' + crRT.Name + '%';
        PageIconID = crRT.DeveloperName;
        FormType = crRT.Name;
        
        formContact = new Contact();
        formContact.LeadSource = 'Walk-In Lead';
        
        //populate store number on load
        formContact.Retail_Store_Number__c = currentUser.SAP_Store_Number__c;
        
        //populate with existing contact if it exists
        if(queriedContact != null){
            formContact.LastName = queriedContact.LastName;
            formContact.FirstName = queriedcontact.FirstName;
            formContact.Retail_Store_Number__c = queriedContact.Retail_Store_Number__c;
            formContact.Email = queriedContact.Email;
            formContact.Phone = queriedContact.Phone;
            formContact.MailingStreet = queriedContact.MailingStreet;
            formContact.MailingCity = queriedContact.MailingCity;
            formContact.MailingState = queriedContact.MailingState;
            formContact.MailingPostalCode = queriedContact.MailingPostalCode ;
            formContact.Number_of_Lines_Req__c = queriedContact.Number_of_Lines_Req__c;
            formContact.LeadSource = queriedContact.LeadSource;
        }
        
        formAccount = new Account();
        //populate with existing account if it exists
        if(queriedAccount != null){
            formAccount.Name = queriedAccount.Name;
        }
        
        formCreditRequest = new Credit_Request__c(RecordTypeId = crRT.id, Channel__c = 'Retail', Submit_for_Approval__c = true);
        
    }
    
    //Populate the existing Account and Contact if they exist
    
    public void populateExistingObjects(){
        String acctId = ApexPages.currentPage().getParameters().get('accountid');
        String contId = ApexPages.currentPage().getParameters().get('contactid');
        
        if (!String.isBlank(ApexPages.CurrentPage().GetParameters().Get('Type')))
        {
            RECORD_TYPE = ApexPages.CurrentPage().GetParameters().Get('Type');
        }
        
        if (!String.isBlank(ApexPages.CurrentPage().GetParameters().Get('mobilelink')))
        {
            inSF1 = (ApexPages.CurrentPage().GetParameters().Get('mobilelink') == 'true')?true:false;
        }else{
            inSF1 = false;
        }
        
        // Referring the Apex class 'CreditRequestService' to fetch related account details
        if(!String.isBlank(acctId)){
            Set<ID> accountIdSet = new Set<ID>();
            accountIdSet.add(acctId);
            Map<ID,Account> accountMap = CreditRequestService.getRelatedAccounts(accountIdSet);
            if(!accountMap.isEmpty()){
                queriedAccount = accountMap.values().get(0);
                tempAccountId = accountMap.values().get(0).id;
                queriedAccountBoolean = true;
            }
        }
        
        // Referring the Apex class 'CreditRequestService' to fetch related contact details
        if(!String.isBlank(contId)){
            Set<ID> contactIdSet = new Set<ID>();
            contactIdSet.add(contId);
            Map<ID,Contact> contactMap = CreditRequestService.getRelatedContacts(contactIdSet);
            if(!contactMap.isEmpty()){
                queriedContact = contactMap.values().get(0);
                tempContactId = queriedContact.id;
                queriedContactBoolean = true;
            }
        }
    }
}