URL Parameters within Apex and Visualforce

28 Sep

Get a URL parameter from a Visualforce page

String MyParameter = ApexPages.currentPage().getParameters().get('urlparm');

Get a map of URL parameters from a Visualforce page

Map UrlParameterMap = ApexPages.currentPage().getParameters();
// check for a specific parameter
if (UrlParameterMap.containsKey('urlparm')){
  //do something with the parameter... UrlParameterMap.get('urlparm')
}       

Get URL Parameters in a Visualforce page

This assumes the URL is looks like this, “http://www.salesforce.com/apex/MyPage?urlparm=something”.


Put values into Visualforce page’s URL and load a new page

Method 1: write out the URL

public PageReference MyPageMethod() {
  String MyParameter = 'something';

  // redirect to a Visualforce page containing a url parm
  PageReference MyNewPage = new PageReference('/apex/newvfpage?urlparm=' + MyParameter);    
  // -or- redirect to a records page
  PageReference MyNewPage = new PageReference('/' + MyObj.Id);

  MyNewPage.setRedirect(true);
  return MyNewPage;
}

Method 2: Put parameters into a PageReference

public PageReference MyPageMethod() {
  String MyParameter = 'something';

  PageReference MyNewPage = Page.PageTwo;
  MyNewPage.getParameters().put('urlparm',MyParameter);

  MyNewPage.setRedirect(true);
  return MyNewPage;
}

Put values into the current test page’s URL in a Test Class

@isTest
private class MyControllerTest {
  static testMethod void myUnitTest() {
    // create a new page reference, set it as the current page, 
    // and use the ApexPages.currentPage() method to put the values
    PageReference PageRef = Page.MyPage;
    Test.setCurrentPage(PageRef);
    ApexPages.currentPage().getParameters().put('urlparm', 'theValue');      

    // load the controller and proceed with testing
    MyController controller = new MyController();
  }
}

Salesforce Help

$CurrentPage Documentation
Controller Navigation Methods
Visualforce Parameters

Apex Loops, The Simple Things

18 Sep

For loop over a map’s keys (KeySet)

map MyAwesomeMap;
//... other code
for (String KeyStr: MyAwesomeMap.keySet()){
  system.debug(KeyStr);
}

For loop over a map’s values

map MyAwesomeMap;
//... other code
for (CustomObject__c AnObject: MyAwesomeMap.values()){
  system.debug(AnObject);
}

For loop over a list

list MyAwesomeList;
//... other code
for (CustomObject__c AnObject: MyAwesomeList){
  system.debug(AnObject);
}

For loop over a set

set MyAwesomeSet;
//... other code
for (CustomObject__c AnObject: MyAwesomeSet){
  system.debug(AnObject);
}

Code Snipits for Converting Salesforce Collections

8 Sep

Convert a Map to a List of keys

Map MyMap = new Map();
List IdList = new List();
// ... logic to load map ...
IdList.addAll(MyMap.keySet());

Convert a Map Values to a List of values

Map MyMap = new Map();
List CustomObjectList = new List();
// ... logic to load map ...
CustomObjectList.addAll(MyMap.values());

Convert an sObject List to a Map – Example 1 – New map with the List passed into the map’s constructor

Use this method when the map’s key value is the object’s Id. Thanks to this post for explaining the concept: King Koo

List MyList = new List();
// ... logic to load list ...
Map MyMap = new Map(MyList);

Convert an sObject List to a Map – Example 2 – Loop and Add

Use this method when the map’s key must be a field that is not an object’s id. This is useful when you need to find an object with a name instead of an Id value.

List MyList = new List();
Map MyMap = new Map();
// ... logic to load list ...
for(CustomObject__c c : MyList) {
  MyMap.put(c.Name, c);
}

Report on Field Changes in Salesforce

26 Nov

Tracking field history in Salesforce allows you to report on the original and new values of a field. This functionality can be very useful when determining who changed a field value and when it happened. This built-in change tracking has some limits:

  • Only the following standard objects allow field history tracking: Accounts, Cases, Contacts, Entitlements, Service contracts, Contract line items, Contracts, Leads, Opportunities, and Solutions
  • Only 20 fields may be tracked per object
  • Not all field types can be tracked

Continue reading