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
justynjustyn 

Randomise search results

Hi

 

I have a search controller that I use for 'finding a counsellor' in our database. It works in terms of presenting the correct results based on the criteria given. I want to make the search function publicly available but want the results of the search to be randomised somehow so that the name contact isn't always at the top of the page. Does my enquiry make sense and is there anything that I can do to achieve this?

 

As I say the search filters work nicely, but I want to be fair to those in our database so that they all have a chance of being at the top of the search results table.

 

Thanks in advance

 

Justyn

stcforcestcforce

in the controller, query a list and then use a second list to show up in the visual force page. Have a loop (condition based on loop size) that for the given size selects a random element in the first list, adds it to the second and then delete the record from the first list. Pretty simple really.

justynjustyn

Hi 

 

Thanks for the helpful info and advice on what can be done. My challenge is that I am a novice at this. I wonder if you could have a look at my controller code and advise/help with the additional coding?

 

}

 

public class ReferralSearchController {

 

  // the soql without the order and limit

  private String soql {get;set;}

  // the collection of contacts to display

  public List<Counselling_Practice__c> counselling_practices {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 = 'Current_Year_Referrals__c'; } return sortField;  }

    set;

  }

 

  // format the soql for display on the visualforce page

  public String debugSoql {

    get { return soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20'; }

    set;

  }

 

  // init the controller and display some sample data when the page loads

  public ReferralSearchController() {

    soql = 'select name, ID, member__r.Name, Counselling_Service__c, Therapeutic_Approach__c, fee_amount__c, Interests_Skills__c, Language__c, Referral_County__c, Practice_Area_Town__c, Practice_City_County__c, for_whom__c, gender__c, donations__c, negotiable__c, free_service__c from counselling_practice__c where name != null and currently_active__c = True';

    runQuery();

  }

 

  // 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();

  }

 

  // runs the actual query

  public void runQuery() {

 

    try {

      counselling_practices = Database.query(soql + ' order by ' + sortField + ' ' + sortDir + ' limit 20');

    } catch (Exception e) {

      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Ooops!'));

    }

 

  }

 

  // runs the search with parameters passed via Javascript

  public PageReference runSearch() {

 

    String name = Apexpages.currentPage().getParameters().get('name');

    String ID = ApexPages.currentPage().getParameters().get('id');

    String member = Apexpages.currentPage().getParameters().get('member');

    String Counselling_Service = Apexpages.currentPage().getParameters().get('Counselling_Service');

    String Therapeutic_Approach = Apexpages.currentPage().getParameters().get('Therapeutic_Approach');

    String fee_amount = Apexpages.currentPage().getParameters().get('fee_amount');

    String Interests_Skills = Apexpages.currentPage().getParameters().get('Interests_Skills');

    String Language = Apexpages.currentPage().getParameters().get('Language');

    String Referral_County = Apexpages.currentPage().getParameters().get('Referral_County');

    String Practice_Area_Town = Apexpages.currentPage().getParameters().get('Practice_Area_Town');

    String Practice_City_County = Apexpages.currentPage().getParameters().get('Practice_City_County');

    String for_whom = Apexpages.currentPage().getParameters().get('for_whom');

    String gender = Apexpages.currentPage().getParameters().get('gender');

    String donations = Apexpages.currentPage().getParameters().get('donations');

    String negotiable = Apexpages.currentPage().getParameters().get('negotiable');

    String free_service = Apexpages.currentPage().getParameters().get('free_service');

 

    soql = 'select name, ID, member__r.Name, Counselling_Service__c, Therapeutic_Approach__c, fee_amount__c, Interests_Skills__c, Language__c, Referral_County__c, Practice_Area_Town__c, Practice_City_County__c, for_whom__c, gender__c, donations__c, negotiable__c, free_service__c from counselling_practice__c where name != null and currently_active__c = True';

    if (!name.equals(''))

      soql += ' and name LIKE \''+String.escapeSingleQuotes(name)+'%\'';

    if (!member.equals(''))

      soql += ' and member__r.Name LIKE \''+String.escapeSingleQuotes(member)+'%\'';

     if (!Counselling_Service.equals(''))

      soql += ' and Counselling_Service__c includes (\''+Counselling_Service+'\')';

      if (!Therapeutic_Approach.equals(''))

      soql += ' and Therapeutic_Approach__c includes (\''+Therapeutic_Approach+'\')';

    if (!fee_amount.equals(''))

      soql += ' and fee_amount__c LIKE \''+String.escapeSingleQuotes(fee_amount)+'%\'';

    if (!Interests_Skills.equals(''))

      soql += ' and Interests_Skills__c LIKE \''+String.escapeSingleQuotes(Interests_Skills)+'%\'';

    if (!Language.equals(''))

      soql += ' and Language__c = \''+Language+'\'';

    if (!Referral_County.equals(''))

      soql += ' and Referral_County__c = \''+Referral_County+'\'';

    if (!Practice_Area_Town.equals(''))

      soql += ' and Practice_Area_Town__c LIKE \''+String.escapeSingleQuotes(Practice_Area_Town)+'%\'';

      if (!Practice_City_County.equals(''))

      soql += ' and Practice_City_County__c LIKE \''+String.escapeSingleQuotes(Practice_City_County)+'%\'';

    if (!for_whom.equals(''))

      soql += ' and for_whom__c includes (\''+for_whom+'\')';

    if (!gender.equals(''))

      soql += ' and gender__c LIKE \''+String.escapeSingleQuotes(gender)+'%\'';

    if (!donations.equals(''))

      soql += ' and donations__c = ' + donations;

    if (!negotiable.equals(''))

      soql += ' and negotiable__c = ' + negotiable; 

    if (!free_service.equals(''))

      soql += ' and free_service__c = ' + free_service; 

 

    // run the query again

    runQuery();

 

    return null;

  }

 

  // use apex describe to build the picklist values

  public List<String> Counselling_Service{

    get {

      if (Counselling_Service== null) {

 

        Counselling_Service = new List<String>();

        Schema.DescribeFieldResult field = counselling_practice__c.Counselling_Service__c.getDescribe();

 

        for (Schema.PicklistEntry f : field.getPicklistValues())

          Counselling_Service.add(f.getLabel());

 

      }

      return Counselling_Service;         

    }

    set;

  }

 

  public List<String> Therapeutic_Approach{

    get {

      if (Therapeutic_Approach== null) {

 

        Therapeutic_Approach = new List<String>();

        Schema.DescribeFieldResult field = counselling_practice__c.Therapeutic_Approach__c.getDescribe();

 

        for (Schema.PicklistEntry f : field.getPicklistValues())

          Therapeutic_Approach.add(f.getLabel());

 

      }

      return Therapeutic_Approach;         

    }

    set;

  }

 

  public List<String> Referral_County{

    get {

      if (Referral_County== null) {

 

        Referral_County = new List<String>();

        Schema.DescribeFieldResult field = counselling_practice__c.Referral_County__c.getDescribe();

 

        for (Schema.PicklistEntry f : field.getPicklistValues())

          Referral_County.add(f.getLabel());

 

      }

      return Referral_County;         

    }

    set;

  }

 

   public List<String> Language{

    get {

      if (Language== null) {

 

        Language = new List<String>();

        Schema.DescribeFieldResult field = counselling_practice__c.Language__c.getDescribe();

 

        for (Schema.PicklistEntry f : field.getPicklistValues())

          Language.add(f.getLabel());

 

      }

      return Language;         

    }

    set;

  }

 

  public List<String> for_whom{

    get {

      if (for_whom== null) {

 

        for_whom = new List<String>();

        Schema.DescribeFieldResult field = counselling_practice__c.for_whom__c.getDescribe();

 

        for (Schema.PicklistEntry f : field.getPicklistValues())

          for_whom.add(f.getLabel());

 

      }

      return for_whom;         

    }

    set;

  }

 

 

stcforcestcforce

i wrote something to illustrate what i was suggesting. keep in mind i wrote it in notepad - it'll likely have errors. i don't have enough time just now to write it using your object or suggest where to fit it in your code- sorry.

 

list<account> acs = [select a.id from account a limit 20];
list<account> someaccounts = new list<account>();
integer x = 20;

while(x>0)
{
    //calculate index
    double y = Math.random() ; //between 0 and 1
    y = x * y;
    integer index = math.round(y);//cast to int and should never exceed index limits.
    //add accoutn to new list
    someaccount.add(acs[index]);
    //remove account from list, decrement x;
    acs.remove(index);
    x--;
}

 

you can create a set of indexes and use the random as means to remove from your set and thus keep the original list intact, if you wish to do so. Also based on the code you posted, you're less of a novice than i am. anyhow, good luck.

 

justynjustyn

Hi

 

Thanks for the helpful sample code. I've started to amend it for my controller. I've corrected some of the errors that appear but one I get says: 

 

Error: ReferralSearchController Compile Error: expecting right curly bracket, found 'while' at line 24 column 0

 

That is the line: while(x>0)

 

I've copied the amended code below. Any observations would be of great help.

 

 

}

list<counselling_practice__c> acs = [select counselling_practice__c.id from counselling_practice__c a limit 20];

list<counselling_practice__c> someaccounts = new list<counselling_practice__c>();

integer x = 20;

 

while(x>0)

{

    //calculate index

    double y = Math.random() ; //between 0 and 1

    y = x * y;

    integer index = math.round(y);//cast to int and should never exceed index limits.

    //add accoutn to new list

    someaccount.add(acs[index]);

    //remove account from list, decrement x;

    acs.remove(index);

    x--;

 

 

Regards

 

Justyn