SFDC Randomizer

public class Randomizer {

     //returns a random Integer
     public static Integer getRandomNumber(Integer size){
          Double d = math.random() * size;
          return d.intValue();
     }

     //returns either true or false randomly
     public static Boolean getRandomBoolean(){
          if(math.mod(getRandomNumber(10),2) == 0){
               return true;
          }
          else{
               return false;
          }
     }

     //Get's a random value from a list of strings
     public static String getRandomString(List<String> strings){
          List<Double> ranks = new List<Double>();
          Map<Double,String> rankMap = new Map<Double,String>();

          for(String s : strings){
               Boolean isDup = true;
               Double rank;

               While(isDup){
                    Double x = getRandomNumber(100000);
                    if(!rankMap.containsKey(x)){
                         rank = x;
                         isDup = false;
                    }
               }

               ranks.add(rank);
               rankMap.put(rank,s);
          }

          ranks.sort();
          return rankMap.get(ranks.get(0));
     }

     //Returns a random picklist value 
     public static string getRandomPickListValue(Sobject s_object, String field_name, Boolean allow_blank){
          List<String> Strings = new List<String>();
          if(allow_blank){
               String b = '';
               Strings.add(b);
          }
            Schema.sObjectType sobject_type = s_object.getSObjectType();
            Schema.DescribeSObjectResult sobject_describe = sobject_type.getDescribe();
            Map<String, Schema.SObjectField> field_map = sobject_describe.fields.getMap();
            List<Schema.PicklistEntry> pick_list_values = field_map.get(field_name).getDescribe().getPickListValues();
            for (Schema.PicklistEntry a : pick_list_values) {
                Strings.add(a.getValue());
            }
            return getRandomString(Strings);
     }

     //returns a map of all picklists and multiselect picklists for a givien object
     //the keyset is the field name using proper case
     public static Map<String,List<String>> getPicVals(sObject s_object){
          Map<String,List<String>> valueMap = new Map<String,List<String>>();
          Schema.sObjectType sobject_type = s_object.getSObjectType();
          Schema.DescribeSObjectResult r = sobject_type.getDescribe();
          Map<String, Schema.SObjectField> field_map = r.fields.getMap();

          for(String s : field_map.keyset()){
               List<String> strings = new List<String>();
               Schema.DescribeFieldResult F = field_map.get(s).getDescribe();
               if(f.GetType() == Schema.DisplayType.Picklist || f.GetType() == Schema.DisplayType.MultiPicklist){
                    List<Schema.PicklistEntry> pick_list_values = field_map.get(s).getDescribe().getPickListValues();
                    for (Schema.PicklistEntry a : pick_list_values) {
                         strings.add(a.getValue());
                   }
                   valueMap.put(String.valueOf(field_map.get(s)),strings);
               }
          }
          return valueMap;
     }

     //Returns Lorem Ipsum placeholder text.
     public static String getPlaceholderText(Integer length){
          String firstSentence = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. ';
          List<String> sentenceList = new List<String>();
          sentenceList.add('Vivamus nec lacus eget massa cursus pulvinar. ');
          sentenceList.add('Morbi vel odio eget nunc auctor posuere eget eget ante. ');
          sentenceList.add('Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. ');
          sentenceList.add('Pellentesque lacus eros. ');
          sentenceList.add('Sed suscipit tristique varius. ');
          sentenceList.add('Mauris ultricies, nibh eu fermentum accumsan, justo quam pulvinar tellus, sed tempor quam eros sit amet ante. ');
          sentenceList.add('Duis mi libero, cursus nec facilisis ut, commodo eget nunc. ');
          sentenceList.add('Nulla eros augue, iaculis sed volutpat et, sagittis quis sem. ');
          sentenceList.add('Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Nulla placerat accumsan vulputate. ');
          sentenceList.add('Fusce placerat tellus eget tellus faucibus a gravida sapien fermentum. ');

          String s = firstSentence;

          while (s.length() < length+1){
               s += getRandomString(sentenceList);
          }

          s = s.trim();

          while (s.length() >= length-1){
               s = s.substring(0,s.length()-1).trim();
          } 

          s = s.substring(0,s.length()-1).trim();
          s += '.';
          return s;
     }

     static testMethod void testRandomizer() {
          test.startTest();
          Integer testInt = getRandomNumber(10);
          Boolean testBool = getRandomBoolean();
          List<String> testStringList = new List<String>();
          testStringList.add('one');
          testStringList.add('two');
          testStringList.add('three');
          String testString = getRandomString(testStringList);
          String testString2 = getRandomPickListValue(new Account(), 'Industry', true);
          String testString3 = getRandomPickListValue(new Account(), 'Industry', false);
          Map<String,List<String>> testMap = getPicVals(new Account());
          test.stopTest();
          String testString4 = getPlaceholderText(300);
     }
}

Override a page for some, but not all, users

Some of your users should use a custom Visualforce page, while others should use a standard Salesforce.com page.To override the Account tab with a Visualforce page for most of your users, but send users with the “System Administrator” profile to the standard Salesforce.com Account home page:

  1. Create a Visualforce page called conditionalAccountOverride.
    • Click Setup | Develop | Pages.
    • Click New.
    • In the label field, enter Override the Account Page for Most Users.
    • In the name field, enter accountOverride.
    • In the description, enter This page will display for all users, except System Administrators, when they click the Account tab.
    • In the editor, enter the following markup:
<apex:page action=
  "{!if($Profile.Name !='System Administrator', 
    null, 
	urlFor($Action.Account.Tab, $ObjectType.Account,
	null, true))}"
  standardController="Account" 
  recordSetVar="accounts" 
  tabStyle="Account">

  <!-- Replace with your markup -->  
	
  This page replaces your Account home page for 
  all users except Administrators.
</apex:page>
    • Click Save.

2. Set the page level security to allow all users to view the page.

    • Click Security for the page you just created.
    • Select all the profiles that will be using this page in the Available Profiles list and click Add to add them to the Enabled Profiles list.
    • Click Save.

3. Create an override that directs users to your new page.

    • Click Setup | Customize | Accounts | Buttons and Links.
    • In the Standard Buttons and Links list, click Override for the Accounts Tab.
    • Set the content type to Visualforce Page.
    • From the Content Name drop-down list, select conditionalAccountOverride.
    • Click Save.

This solution uses the action attribute on the apex:page component to test the user’s profile. Using the action attribute is a good way to ensure an action is taken when the page loads. If instead of limiting the standard page to a particular group of users, you want to limit the override to a particular group of users, such as all “Marketing Users” and “Solution Managers,” you need to create a controller extension. To override the Account tab with a custom Visualforce page only for users with the “Marketing User” or the “Solution Manager” profile:

  1. Create a Visualforce page called standardAcctPage.
    • Click Setup | Develop | Pages.
    • Click New.
    • In the label field, enter Override the Account Page for Most Users.
    • In the name field, enter standardAcctPage.
    • In the description, enter This page will display for all users, except Marketing Users and Solution Managers, when they click the Account tab.
    • In the editor, enter the following markup:
<apex:page standardController="account" extensions="overrideCon" action="{!redirect}">
  <apex:detail/>
</apex:page>

This page overrides your current Account home page. It uses a controller extension to test the user profile. If the user is a “Marketing User” or “Solution Manager,” they are redirected to a different page.

2. Using the procedure in step 1, create a second Visualforce page called customAcctPage:

<apex:page standardController="account">
  <h1>Override Account page for two profiles</h1>
  <apex:detail />
</apex:page>

This is the page that only the “Marketing Users” and “Solutions Managers” will see. They only get to this page through redirection.

3. Grant access to both pages for all profiles.

    • Click Security for the each of the pages you just created.
    • Select all the profiles that will be using this page in the Available Profiles list and click Add to add them to the Enabled Profiles list.
    • Click Save.

4. Create a controller extension called overrideCon.

    • Click Setup | Develop | Apex Classes.
    • Click New.
    • In the editor, add the following content:
public class overrideCon {
   String recordId;
   
public overrideCon(ApexPages.StandardController 
       controller) {recordId = controller.getId();}

public PageReference redirect() {
  Profile p = [select name from Profile where id = 
			   :UserInfo.getProfileId()];
  if ('Marketing User'.equals(p.name) 
	  || 'Solution Manager'.equals(p.name)) 
	  {
	   PageReference customPage =  Page.customAccountPage;
	   customPage.setRedirect(true);
	   customPage.getParameters().put('id', recordId);
	   return customPage;
	  } else {
		  return null; //otherwise stay on the same page  
	
	  }
   }
}
    • Click Save

5.  Create an override that directs users to the standardAcctPage page when they click on the Accounts tab.

    • Click Setup | Customize | Accounts | Buttons and Links.
    • In the Standard Buttons and Links list, click Override for the Accounts Tab.
    • Set the content type to Visualforce Page.
    • From the Content Name drop-down list, select standardAcctPage.
    • Click Save.

When a user clicks on the Account tab, if their profile is “Marketing User” or “Solution Manager” the controller extension will automatically redirect them to the customAcctPage.

 

Add support:clickToDial tag for Salesforce CTI integration

The support:clickToDial component lets you add a phone field to custom Visualforce detail and edit pages. When users in organizations with Open CTI or Salesforce CRM Call Center click on the phone field, the phone number is dialed automatically and connects to the SoftPhone.

  • The User must be logged into the CTI to use
  • The showHeader=”true” attribute must be set in the <apex:page> tag or ommitted altogether to enable
<div class="customer">
    <apex:outputLabel value="Phone: ">
        <support:clickToDial number="{!Lead.phone}" entityId="{!Lead.Id}" /> 
    </apex:outputLabel>            					
</div>	

Results:

Capture