• Harrison Lau
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 6
    Replies
Hi friends,
I am an admin making the transition to developer so forgive me if this is/should be considered common knowledge but...
I was wondering if someone would be able to help me write a test class for this Dynamic Search Page controller.  I understand in theory how to write a test class but I'm not sure i would be able to successfully pull it off on my own.  

Any help would be MUCH appreciated!
 
public with sharing class WrapperClass{
    
    //Instantiate Lists that we will be using
    public List<wrapAccount> wrapCTAList {get;set;}
    public List<CTA_Database__c> selectedCTA{get;set;}
    public List<wrapAccount> tempWrapCTAList{get;set;}
    
    //empty Constructor
    public WrapperClass(ApexPages.StandardController WrapperClass) {
        
    } 
    
    //Instantiate lists and runQuery when page loads
    public WrapperClass(){
        list<wrapAccount> wrapCTAList = new list<wrapAccount>();
        List<CTA_Database__c> selectedCTA = new list<CTA_Database__c>();
        
        soql = 'SELECT Name, First_Name__c, Last_Name__c,Chinese_Name__c,Primary_Phone__c,Residential_Building_Address__c, Residential_Unit__c, Checkbox__c, Family_Status__c FROM CTA_Database__c WHERE Id != null';
        runQuery();
    }
    
    //Grab records from wrapCTAList where 'selected' property is true, then set checkbox__c value true/false based on 'selected'
    public void processSelected() {        
        List<wrapAccount> tempWrapCTAList = new List<wrapAccount>();
        //for each record on QueryList        
        for(wrapAccount objWrap: wrapCTAList) {
            //if selected, then checkbox is true            
            if(objWrap.selected == true) {
                objWrap.acc.checkbox__c = true;
                //update checkbox field in database
                update (objWrap.acc);
            } else {
                //if not selected, then checkbox is false
                if (objWrap.selected != true) {
                    objWrap.acc.checkbox__c = false;
                    //update checkbox field in database
                    update (objWrap.acc);
                }
            }
            //Check to see if there are already 'checkboxed records', set their selected property to true
            if (objWrap.acc.checkbox__c == true) {
                string selectedName = objWrap.acc.name;
                for (wrapAccount qWrap: wrapCTAList) {
                    if (qWrap.acc.name == selectedName) {
                        qWrap.selected = true;
                        system.debug(selectedName);
                    }
                }
            } 
        }
        //add records to Results Table
        buildSelections();
    }
    
    //add values to Results Table
    public void buildSelections(){
        tempWrapCTAList = new List <wrapAccount>();       
        string selectSoql = 'SELECT Name, First_Name__c, Last_Name__c,Chinese_Name__c,Primary_Phone__c,Residential_Building_Address__c, Residential_Unit__c, Checkbox__c, Family_Status__c FROM CTA_Database__c WHERE checkbox__c = true'; 
        
        //for each record where checkbox__c is true, add to results list.
        for(CTA_Database__c obj : Database.query(selectSoql + ' order by ' + sortField + ' ' + sortDir)) {
            tempwrapCTAList.add(new wrapAccount(obj));
        }        
    }
    
    //SELECT all records in QueryList
    public PageReference selectAll() {
        for(wrapAccount q: WrapCTAList) {
            if (q.acc.checkbox__c == false) {
                q.selected = true;
                q.acc.checkbox__c = true;
                update(q.acc);
                processSelected();
            }
        }
        return Null;
    }
    
    //UNSELECT all records in QueryList
    public PageReference unselectAll() {
        for(wrapAccount q: WrapCTAList) {
            if (q.acc.checkbox__c == true) {
                q.selected = false;
                q.acc.checkbox__c = false;
                update(q.acc);
                processSelected();
            }
        }
        return Null;
    }
    
    //Flow button, currently to Uncheck Boxes Flow
    public PageReference beginFlow() {
        for (wrapAccount s:tempWrapCTAList) {
            cta_database__c c = s.acc;
            selectedCTA.add(c);
        }
        return Page.CTA_Tab;
    }
    
    
    // This is our wrapper/container class. In this example a wrapper class contains both the standard salesforce object Account and a Boolean value
    public class wrapAccount {
        
        public CTA_Database__c acc {get; set;}
        public Boolean selected {get; set;}
        
        public wrapAccount(CTA_Database__c a) {
            acc = a;
            selected = false;
        }
    }
    
    //----------------------------------//--------------------------------------//
    
    // the soql without the order and limit
    public String soql {get;set;}
    
    // the current sort direction. defaults to asc
    public String sortDir {
        get  { if (sortDir == null) {sortDir = 'asc';} return sortDir;}
        set;
    }
    
    // the current field to sort by. defaults to last name
    public String sortField {
        get  { if (sortField == null) {sortField = 'Last_Name__c'; } return sortField;  }
        set;
    }
    
    // toggles the sorting of query from asc<-->desc
    public void toggleSort() {
        // simply toggle the direction
        sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
        // run the query again
        runQuery();
        buildSelections();
    }
    
    // format the soql for display on the visualforce page
    public String debugSoql {
        get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 200'; }
        set;
    }      
    
    
    // runs the actual query
    public void runQuery() {
        
        wrapCTAList = new List<wrapAccount>();
        
        try {
            //builds QueryList
            for(CTA_Database__c obj : Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 200')) {
                wrapCTAList.add(new wrapAccount(obj));
                
                //sets Select property to TRUE if record is on SelectedList
                if (obj.Checkbox__c == true) {
                    for (wrapAccount c:wrapCTAList) {
                        string cName = c.acc.name;
                        if (obj.name == cName) {
                            c.selected = true;
                        }
                    }
                }
            }            
        } catch (Exception e) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
        }
    }    
    
    // runs the search with parameters passed via Javascript
    public PageReference runSearch() {
        
        String memberNumber = Apexpages.currentPage().getParameters().get('memberNumber');
        String firstName = Apexpages.currentPage().getParameters().get('firstName');
        String lastName = Apexpages.currentPage().getParameters().get('lastName');
        String ChineseName = Apexpages.currentPage().getParameters().get('ChineseName');
        String PrimaryPhone = Apexpages.currentPage().getParameters().get('PrimaryPhone');
        String ResBuilding = Apexpages.currentPage().getParameters().get('ResBuilding');
        String ResUnit = Apexpages.currentPage().getParameters().get('ResUnit');        
        
        soql = 'select Name, First_Name__c, Last_Name__c, Chinese_Name__c, Primary_Phone__c, Residential_Building_Address__c, Residential_Unit__c, Checkbox__c, Family_Status__c FROM CTA_Database__c WHERE Id !=null';
        if (!memberNumber.equals(''))
            soql += ' and Name LIKE \''+String.escapeSingleQuotes(memberNumber)+'%\'';
        if (!firstname.equals(''))
            soql += ' and First_Name__c LIKE \''+String.escapeSingleQuotes(firstName)+'%\'';
        if (!lastName.equals(''))
            soql += ' and Last_Name__c LIKE \''+String.escapeSingleQuotes(lastName)+'%\'';
        if (!ChineseName.equals(''))
            soql += ' and Chinese_Name__c LIKE \''+String.escapeSingleQuotes(ChineseName)+'%\'';  
        if (!PrimaryPhone.equals(''))
            soql += ' and Primary_Phone__c LIKE \''+String.escapeSingleQuotes(PrimaryPhone)+'%\'';
        if (!ResBuilding.equals(''))
            soql += ' and Residential_Building_Address__r.display_address__c LIKE \''+String.escapeSingleQuotes(ResBuilding)+'%\'';
        if (!ResUnit.equals(''))
            soql += ' and Residential_Unit__r.Name LIKE \''+String.escapeSingleQuotes(ResUnit)+'%\'';
       
        // run the query again
        runQuery();
        return null;
    }
    
}

 
Having some issues pushing records from my LIST of sObjects to a Map of boolean values and sobjects.  Any advice?

public with sharing class WrapperClass{
    
    //Our collection of the class/wrapper objects wrapAccount
    public List<wrapAccount> wrapCTAList {get;set;}
    public Map<boolean,CTA_Database__c> ctaMap {get {
        if(ctaMap == null) {
            ctaMap = new Map<boolean, CTA_Database__c>();
        }
        return ctaMap;
    }
                                                set {
                                                    for (CTA_Database__c member:ctaMember) {
                                                        boolean selected = false;
                                                        ctaMap.put(selected, member);
                                                    }
                                                }
                                               }
    public List<CTA_Database__c> selectedCTA{get;set;}
    public List<CTA_Database__c> ctaMember{get;set;}
    
    public WrapperClass(ApexPages.StandardController WrapperClass) {
        //empty Constructor           
    } 
    
    public WrapperClass(){
        list<wrapAccount> wrapCTAList = new list<wrapAccount>();
        Map<boolean,CTA_Database__c> ctaMap = new Map<boolean,CTA_Database__c>();
        List<CTA_Database__c> selectedCTA = new list<CTA_Database__c>();
        List<CTA_Database__c> ctaMember = new list<CTA_Database__c>();
        
        if(wrapCTAList == null) {
            for(CTA_Database__c a: [SELECT ID, Name, First_Name__c, Last_Name__c,Chinese_Name__c,Primary_Phone__c,Residential_Building_Address__c, Residential_Unit__c FROM CTA_Database__c WHERE Id != null]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapCTAList.add(new wrapAccount(a));
                for (wrapAccount wrapcta : wrapCTAList) {
                    ctaMap.put(wrapcta.selected = false, wrapcta.acc);
                }
            }
        }
        soql = 'SELECT Name, First_Name__c, Last_Name__c,Chinese_Name__c,Primary_Phone__c,Residential_Building_Address__c, Residential_Unit__c FROM CTA_Database__c WHERE Id != null';
        runQuery();
        buildMap();
    }
    
    public void buildMap() {
        try {
            for (CTA_Database__c member: ctaMember) {
                boolean selected = false;
                ctaMap.put(selected, member);
            }            
        } catch (exception e) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Could not build ctaMap!'));
        }   

    }
    
    public void processSelected() {
        List<wrapAccount> wrapCTAList = new List<wrapAccount>();
        for(wrapAccount wrapcta : wrapCTAList) {
            if(wrapcta.selected == true) {
                selectedCTA.add(wrapcta.acc);
            }
        }
    }
    
    // This is our wrapper/container class. In this example a wrapper class contains both the standard salesforce object Account and a Boolean value
    public class wrapAccount {
        
        public CTA_Database__c acc {get; set;}
        public Boolean selected {get; set;}
        public Map<boolean, CTA_Database__c> ctaMap {get;set;}
        
        public wrapAccount(CTA_Database__c a) {
            acc = a;
            selected = false;
        }
    }
    
    //----------------------------------//--------------------------------------//
    //// the soql without the order and limit
    public String soql {get;set;}
    
    // the current sort direction. defaults to asc
    public String sortDir {
        get  { if (sortDir == null) {sortDir = 'asc';} return sortDir;}
        set;
    }
    
    // the current field to sort by. defaults to last name
    public String sortField {
        get  { if (sortField == null) {sortField = 'Last_Name__c'; } return sortField;  }
        set;
    }
    
    // toggles the sorting of query from asc<-->desc
    public void toggleSort() {
        // simply toggle the direction
        sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
        // run the query again
        runQuery();
    }
    // format the soql for display on the visualforce page
    public String debugSoql {
        get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 200'; }
        set;
    }      
    // init the controller and display some sample data when the page loads
    //public WrapperClass() {
    //soql = 'SELECT Name, First_Name__c, Last_Name__c,Chinese_Name__c,Primary_Phone__c,Residential_Building_Address__c, Residential_Unit__c FROM CTA_Database__c WHERE Id != null';
    //runQuery();
    //}
    
    // runs the actual query
    public void runQuery() {
        
        try {
            ctaMember = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 200');
            buildMap();
        } catch (Exception e) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
        }
    }
    
    
    // runs the search with parameters passed via Javascript
    public PageReference runSearch() {
        
        String memberNumber = Apexpages.currentPage().getParameters().get('memberNumber');
        String firstName = Apexpages.currentPage().getParameters().get('firstName');
        String lastName = Apexpages.currentPage().getParameters().get('lastName');
        String ChineseName = Apexpages.currentPage().getParameters().get('ChineseName');
        String PrimaryPhone = Apexpages.currentPage().getParameters().get('PrimaryPhone');
        String ResBuilding = Apexpages.currentPage().getParameters().get('ResBuilding');
        String ResUnit = Apexpages.currentPage().getParameters().get('ResUnit');
        
        
        soql = 'select Name, First_Name__c, Last_Name__c, Chinese_Name__c, Primary_Phone__c, Residential_Building_Address__c, Residential_Unit__c FROM CTA_Database__c WHERE Id !=null';
        if (!memberNumber.equals(''))
            soql += ' and Name LIKE \''+String.escapeSingleQuotes(memberNumber)+'%\'';
        if (!firstname.equals(''))
            soql += ' and First_Name__c LIKE \''+String.escapeSingleQuotes(firstName)+'%\'';
        if (!lastName.equals(''))
            soql += ' and Last_Name__c LIKE \''+String.escapeSingleQuotes(lastName)+'%\'';
        if (!ChineseName.equals(''))
            soql += ' and Chinese_Name__c LIKE \''+String.escapeSingleQuotes(ChineseName)+'%\'';  
        if (!PrimaryPhone.equals(''))
            soql += ' and Primary_Phone__c LIKE \''+String.escapeSingleQuotes(PrimaryPhone)+'%\'';
        if (!ResBuilding.equals(''))
            soql += ' and Residential_Building_Address__r.display_address__c LIKE \''+String.escapeSingleQuotes(ResBuilding)+'%\'';
        if (!ResUnit.equals(''))
            soql += ' and Residential_Unit__r.Name LIKE \''+String.escapeSingleQuotes(ResUnit)+'%\'';
        
        // run the query again
        runQuery();
        system.debug('hi');
        return null;
    }
}
Having some issues pushing records from my LIST of sObjects to a Map of boolean values and sobjects.  Any advice?

public with sharing class WrapperClass{
    
    //Our collection of the class/wrapper objects wrapAccount
    public List<wrapAccount> wrapCTAList {get;set;}
    public Map<boolean,CTA_Database__c> ctaMap {get {
        if(ctaMap == null) {
            ctaMap = new Map<boolean, CTA_Database__c>();
        }
        return ctaMap;
    }
                                                set {
                                                    for (CTA_Database__c member:ctaMember) {
                                                        boolean selected = false;
                                                        ctaMap.put(selected, member);
                                                    }
                                                }
                                               }
    public List<CTA_Database__c> selectedCTA{get;set;}
    public List<CTA_Database__c> ctaMember{get;set;}
    
    public WrapperClass(ApexPages.StandardController WrapperClass) {
        //empty Constructor           
    } 
    
    public WrapperClass(){
        list<wrapAccount> wrapCTAList = new list<wrapAccount>();
        Map<boolean,CTA_Database__c> ctaMap = new Map<boolean,CTA_Database__c>();
        List<CTA_Database__c> selectedCTA = new list<CTA_Database__c>();
        List<CTA_Database__c> ctaMember = new list<CTA_Database__c>();
        
        if(wrapCTAList == null) {
            for(CTA_Database__c a: [SELECT ID, Name, First_Name__c, Last_Name__c,Chinese_Name__c,Primary_Phone__c,Residential_Building_Address__c, Residential_Unit__c FROM CTA_Database__c WHERE Id != null]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapCTAList.add(new wrapAccount(a));
                for (wrapAccount wrapcta : wrapCTAList) {
                    ctaMap.put(wrapcta.selected = false, wrapcta.acc);
                }
            }
        }
        soql = 'SELECT Name, First_Name__c, Last_Name__c,Chinese_Name__c,Primary_Phone__c,Residential_Building_Address__c, Residential_Unit__c FROM CTA_Database__c WHERE Id != null';
        runQuery();
        buildMap();
    }
    
    public void buildMap() {
        try {
            for (CTA_Database__c member: ctaMember) {
                boolean selected = false;
                ctaMap.put(selected, member);
            }            
        } catch (exception e) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Could not build ctaMap!'));
        }   

    }
    
    public void processSelected() {
        List<wrapAccount> wrapCTAList = new List<wrapAccount>();
        for(wrapAccount wrapcta : wrapCTAList) {
            if(wrapcta.selected == true) {
                selectedCTA.add(wrapcta.acc);
            }
        }
    }
    
    // This is our wrapper/container class. In this example a wrapper class contains both the standard salesforce object Account and a Boolean value
    public class wrapAccount {
        
        public CTA_Database__c acc {get; set;}
        public Boolean selected {get; set;}
        public Map<boolean, CTA_Database__c> ctaMap {get;set;}
        
        public wrapAccount(CTA_Database__c a) {
            acc = a;
            selected = false;
        }
    }
    
    //----------------------------------//--------------------------------------//
    //// the soql without the order and limit
    public String soql {get;set;}
    
    // the current sort direction. defaults to asc
    public String sortDir {
        get  { if (sortDir == null) {sortDir = 'asc';} return sortDir;}
        set;
    }
    
    // the current field to sort by. defaults to last name
    public String sortField {
        get  { if (sortField == null) {sortField = 'Last_Name__c'; } return sortField;  }
        set;
    }
    
    // toggles the sorting of query from asc<-->desc
    public void toggleSort() {
        // simply toggle the direction
        sortDir = sortDir.equals('asc') ? 'desc' : 'asc';
        // run the query again
        runQuery();
    }
    // format the soql for display on the visualforce page
    public String debugSoql {
        get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 200'; }
        set;
    }      
    // init the controller and display some sample data when the page loads
    //public WrapperClass() {
    //soql = 'SELECT Name, First_Name__c, Last_Name__c,Chinese_Name__c,Primary_Phone__c,Residential_Building_Address__c, Residential_Unit__c FROM CTA_Database__c WHERE Id != null';
    //runQuery();
    //}
    
    // runs the actual query
    public void runQuery() {
        
        try {
            ctaMember = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 200');
            buildMap();
        } catch (Exception e) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));
        }
    }
    
    
    // runs the search with parameters passed via Javascript
    public PageReference runSearch() {
        
        String memberNumber = Apexpages.currentPage().getParameters().get('memberNumber');
        String firstName = Apexpages.currentPage().getParameters().get('firstName');
        String lastName = Apexpages.currentPage().getParameters().get('lastName');
        String ChineseName = Apexpages.currentPage().getParameters().get('ChineseName');
        String PrimaryPhone = Apexpages.currentPage().getParameters().get('PrimaryPhone');
        String ResBuilding = Apexpages.currentPage().getParameters().get('ResBuilding');
        String ResUnit = Apexpages.currentPage().getParameters().get('ResUnit');
        
        
        soql = 'select Name, First_Name__c, Last_Name__c, Chinese_Name__c, Primary_Phone__c, Residential_Building_Address__c, Residential_Unit__c FROM CTA_Database__c WHERE Id !=null';
        if (!memberNumber.equals(''))
            soql += ' and Name LIKE \''+String.escapeSingleQuotes(memberNumber)+'%\'';
        if (!firstname.equals(''))
            soql += ' and First_Name__c LIKE \''+String.escapeSingleQuotes(firstName)+'%\'';
        if (!lastName.equals(''))
            soql += ' and Last_Name__c LIKE \''+String.escapeSingleQuotes(lastName)+'%\'';
        if (!ChineseName.equals(''))
            soql += ' and Chinese_Name__c LIKE \''+String.escapeSingleQuotes(ChineseName)+'%\'';  
        if (!PrimaryPhone.equals(''))
            soql += ' and Primary_Phone__c LIKE \''+String.escapeSingleQuotes(PrimaryPhone)+'%\'';
        if (!ResBuilding.equals(''))
            soql += ' and Residential_Building_Address__r.display_address__c LIKE \''+String.escapeSingleQuotes(ResBuilding)+'%\'';
        if (!ResUnit.equals(''))
            soql += ' and Residential_Unit__r.Name LIKE \''+String.escapeSingleQuotes(ResUnit)+'%\'';
        
        // run the query again
        runQuery();
        system.debug('hi');
        return null;
    }
}

HI,

 

I would like to find out how I could make a flow end at a custom location, more specifically make it end at the place where it was started/clicked, which is a VF page within a button on a record detail page.

 

Here is the VF page that I've created, I only need it to end at the Contact Id page where it was clicked (the flow already has a variable with the Contact Id called ContactID.

 

All help is much appreciatted!

 

<apex:page standardController="Account" tabStyle="Account">

    <flow:interview name="Flow_Testing" finishLocation="{!URLFOR('/003/o')}">
        <apex:param name="AccountID" value="{!account.Id}"/>
        <apex:param name="UserID" value="{!$User.Id}"/>
    </flow:interview>
    
</apex:page>