Thursday, January 29, 2009

Inter-window communication (Pop-up & IFrames)

This post explains a Javascript technique called "Inter-window communication". You could use this technique in VisualForce pages to open a pop-up window and have both windows talk among them.

For this post I'll have two HTML windows: A opener window and a pop-up window.

Opener.html:

<html>
<head>
    <title>*** OPENER ***</title>
</head>
<body>
    <form id="form1" runat="server">
    <table border="1">
        <tr>
            <th>
                Counter:
            </th>
            <td>
                <input type="button" onclick="Count();" value="Count" />
                <input type="text" id="ShowCounter" value="0" />
            </td>
        </tr>
        <tr>
            <th>
                Message:
            </th>
            <td>
                <input type="button" onclick="Write();" value="Write" />
                <span id="OutMsg">...</span>
                <input type="text" id="InMsg" value="..." />
            </td>
        </tr>
        <tr>
            <th>
                Other Window:
            </th>
            <td>
                <input type="button" onclick="OpenWindow();" value="Open Window" />
                <input type="button" onclick="UpdatePopPup();" value="Update PopUp" />
                <input type="button" onclick="UpdateIFrame();" value="Update IFrame" />
            </td>
        </tr>
    </table>
    </form>
    <iframe id="MyFrame" name="MyFrame" src="pop-up.html" width="600px" height="150px">
    </iframe>
</body>
</html>

<script type="text/javascript" language="javascript">
    // Local Scripts
    function Count() {
        var objCounter = document.getElementById('showCounter')
        objCounter.value = ++objCounter.value;
    }
    function Write() {
        WriteMessage(document.getElementById('InMsg').value);
    }
    function WriteMessage(inMsg) {
        var objMsg = document.getElementById('OutMsg');
        objMsg.innerHTML = inMsg;
    }
</script>

<script type="text/javascript" language="javascript">
    // Other window scripts
    var newWindow = null;
    function OpenWindow() {
        newWindow = window.open('pop-up.html');
    }
    function UpdateOpener(txtMsg, txtCounter) {
        document.getElementById('InMsg').value = txtMsg;
        document.getElementById('ShowCounter').value = txtCounter;
        Write();
    }
    function UpdatePopPup() {
        if (newWindow != null) {
            Write();
            var txtMsg = document.getElementById('InMsg').value;
            var txtCounter = document.getElementById('ShowCounter').value;
            newWindow.UpdatePopPup(txtMsg, txtCounter);
        }
    }
    function UpdateIFrame() {
        if (document.getElementById("myFrame") != null) {
            Write();
            var txtMsg = document.getElementById('InMsg').value;
            var txtCounter = document.getElementById('ShowCounter').value;
            document.getElementById("myFrame").contentWindow.UpdatePopPup(txtMsg, txtCounter);
        }
    }
</script>
pop-up.html:
<html>  
<head>  
    <title>*** POP-UP ***</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <table border="1">  
        <tr>  
            <th>  
                Counter:   
            </th>  
            <td>  
                <input type="button" onclick="Count();" value="Count" />  
  
                <input type="text" id="ShowCounter" value="0" />  
            </td>  
        </tr>  
        <tr>  
            <th>  
                Message:   
            </th>  
            <td>  
                <input type="button" onclick="Write();" value="Write" />  
                <span id="OutMsg">...</span>  
  
                <input type="text" id="InMsg" value="..." />  
            </td>  
        </tr>  
        <tr>  
            <th>  
                Other Window:   
            </th>  
            <td>  
                <input type="button" onclick="CloseWindow();" value="Close Window" />  
  
                <input type="button" onclick="UpdateOpener();" value="Update Opener" />  
            </td>  
        </tr>  
    </table>  
    </form>  
</body>  
</html>  
  
<script type="text/javascript" language="javascript">  
    // Local Scripts   
    function Count() {   
        var objCounter = document.getElementById('showCounter')   
        objCounter.value = ++objCounter.value;   
    }   
    function Write() {   
        WriteMessage(document.getElementById('InMsg').value);   
    }   
    function WriteMessage(inMsg) {   
        var objMsg = document.getElementById('OutMsg');   
        objMsg.innerHTML = inMsg;   
    }   
</script>  
  
<script type="text/javascript" language="javascript">  
    // Other window scripts   
    function CloseWindow() {   
        window.top.close();   
        UpdateOpener();   
    }   
    function UpdateOpener() {   
        if (window.opener != null) {   
            Write();   
            var txtMsg = document.getElementById('InMsg').value;   
            var txtCounter = document.getElementById('ShowCounter').value;   
            window.opener.UpdateOpener(txtMsg, txtCounter);   
        }
        if (window.parent.frames.length>0) {
            Write();   
            var txtMsg = document.getElementById('InMsg').value;   
            var txtCounter = document.getElementById('ShowCounter').value;   
            window.parent.UpdateOpener(txtMsg, txtCounter);   
        }
    }   
    function UpdatePopPup(txtMsg, txtCounter) {   
        document.getElementById('InMsg').value = txtMsg;   
        document.getElementById('ShowCounter').value = txtCounter;   
        Write();   
    }   
</script>  
The pop-up works because:
  • When the window is opened (opener.html, line 63), the window is assigned to a variable "newWindow".
  • On the pop-up window, the javascript refers to the "window.opener" object (pop-up.html, line 70).
The IFrame works on a similar concept, but the syntax is a bit different.

How do I prevent duplicates based on more than one field?

Recently, I had the need to create a unique index based on more than one field.

I required a sObject whose name field is unique for each user. A user can not have two or more records with the same "name", but it is possible for different users to have records with the same name.

Let's suppose I have an sObject with this data:

Owner Name
user1 name1
user2 name2

If "user1" tries to add a row named "name1", the system should prevent him from doing so because "user1" already has a row with "name1". On the other hand if "user2" tries to add a row named "name1" the system should allow this, because "user2" does not have a row named "name1".

My first idea, was to have a formula field that merged the two values and make that formula field be a primary key. But formula fields can not keys.

So I decided on a trigger, and this worked fine...

trigger PreventDuplicateNameForUser on TPName__c (before insert, before update) {
    for (TPName__c TPName : Trigger.new) {
        List<TPName__c> listFound = [SELECT name
                                       FROM TPName__c
                                      WHERE ownerid = :UserInfo.getUserID()
                                        AND name = :TPName.name];
        if (listFound.size() > 0) {
            Trigger.new[0].addError('You already defined a Parameter Name called ['
                                     + TPName.name +']. Please use another name and try again');
        }
    }
}

How can the controller read the page?

Let's suppose you have a page (or part of a page) that you want to send via email. How can you get the contents of the page in the controller?

Since the contents of the page cannot be obtained by the controller, why not let the page send its contents to the controller?

VisualForce page:

<apex:page controller="MyEmail">
    <div ID="renderedPage">
        ... Put here the HTML content you want to get (it could be the entire page) ...
    </div>
    <apex:form >
        <apex:actionFunction name="setApexHTML" action="{!sendEmail}" rerender="AJAXSection">
            <apex:param name="strHTML" value="" />
        </apex:actionFunction>
    </apex:form>
    <apex:outputPanel id="AJAXSection"></apex:outputPanel>
    <script language="javascript">
        function getJSHTMLData() {
            var divPage = element = document.getElementById('renderedPage');
            var strHTML = divPage.innerHTML;
            setApexHTML(strHTML);
        }

        // Execute on load
        var previousOnload = window.onload;
        window.onload = function() {
            if (previousOnload) {
                previousOnload();
            }
            getJSHTMLData();
            alert('Email sent');
        }
    </script>
</apex:page>
Controller:
public class MyEmail {
    public void sendEmail() {
        List<String> emailAddress;
        String htmlContent = ApexPages.currentPage().getParameters().get('strHTML');

        // Send email
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

        // To
        emailAddress = new List<String>();
        emailAddress.add('user@acme.com');
        mail.setToAddresses(emailAddress);

        // Cc
        emailAddress = new List<String>();
        emailAddress.add('smith@gmail.com');
        mail.setCcAddresses(emailAddress);

        // Reply to
        mail.setReplyTo('support@acme.com');
        mail.setSenderDisplayName('Acme Support');

        // Bcc
        mail.setBccSender(true); // Set to True if you want to BCC yourself on the email.

        // Contents
        mail.setSubject('You have mail!');
        mail.setHtmlBody(htmlContent);
        mail.setUseSignature(false);

        // Send email
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}
How does this code works?
  1. When the VisualForce page loads, part of the JavaScript code (lines 19-25) gets executed.
  2. This calls the getJSHTMLData() method (lines 12-16) which
    1. Gets the contents of the <DIV> (lines 2-4)
    2. Calls the setApexHTML(strHTML) method with that information
  3. The setApexHTML(strHTML) method (lines 6-8) sends the data in the strHTML parameter to the SendEmail() method in the controller.
  4. The SendEmail() method
    1. Gets the data from the parameter (line 4)
    2. Creates and sends the email (line 6-32).
Althogh this code calls the Javascript when it loads, it could easily be changed to have the method called by JavaScript when an event happens (for example the onClick event of a HTML button)

Building a multi-page wizards

I recently tried to develop a set of related pages, something like a multi-page wizard where the context of the data would be preserved from one page to the next.

I started out with these ideas:

  • I tried sending the object as a parameter, but only strings (or numbers in string format) can be sent out as parameters.
  • I tried static objects but the state was not preserved.

I tried a few other tricks, but the system did not preserve the state... All the objects were being instatiated, rather than using the previous instance. I knew it could be done, because I had seen the documentation... And re-reading it I found the answer...

There is a comment for the wizzard's controller code sample in the Visualforce Developer's Guide, that reads:

... Note that the redirect attribute does not need to be set on the PageReference because the URL does not need to change when users move from page to page.

This information should be re-worded to something like this:

Note that the redirect attribute on the PageReference must not be set to true (set it to false or do not set it at all) because the URL must not change when users move from page to page.

Anyways, this is how it should be set up: VisualForce Page:

<apex:page controller="theData">
    <H1>Page1</H1>
    <apex:form >
        Data value: <apex:inputText value="{!DataText}" />
        <apex:commandButton action="{!Page1}" value="Page1" />
        <apex:commandButton action="{!Page2}" value="Page2" />
        <apex:commandButton action="{!Page3}" value="Page3" />
    </apex:form>
</apex:page>

Controller:

public class aaData {
// Data
    private String strText = null;
    public String getDataText() {
        return strText;
    }
    public void setDataText(String value) {
        strText = value;
    }

// Navigation
    public PageReference Page1() {
        return getPage('/apex/aaPage1');
    }
    public PageReference Page2() {
        return getPage('/apex/aaPage2');
    }
    public PageReference Page3() {
        return getPage('/apex/aaPage3');
    }
    private PageReference getPage(String URL) {
        PageReference page = new PageReference(URL);
        page.setRedirect(false);
        return page;
    }
}

I noticed that if the URL does not change, the state is preserved, but if the URL changes the state is lost. In addition to the "page.setRedirect(false)" (line 23 on the previous code), you must be careful as to the use of controllers and extensions... The only way I was able to not change the URL (and preserve the state) was when all the pages use exactly the same set of controllers and extensions... You may use a standard or custom controller and zero or more extensions, but every page in the wizard must use the same set of classes.

All the pages must have the same heading. Something like this:

<apex:page controller="theData">
Or this:
<apex:page controller="theData" extensions="ext1">
Or this:
<apex:page controller="theData" extensions="ext1,ext2">
Or this:
<apex:page standardController="Account" extensions="theData">
Or this:
<apex:page standardController="Account" extensions="theData,ext1">
The important thing is that all the pages in the wizard must use the same set of controllers and extensions, otherwise the state will be lost! I noticed that if I had something like this: Page 2:
<apex:page controller="theData" extensions="step2,step3">
Page 3:
<apex:page controller="theData" extensions="step3">
I was able to go from page 2 to page 3 preserving the state, but if I went back from page 3 to page 2, the state would be lost. Something simpler that you could try is not using extensions, but rather having an instance of the "extension class" in the controller "theData"... This would probably simplify your life :-)

Wednesday, January 28, 2009

Sorting any List<sObject>.

I recently developed an utility class that sorts any List retrieved using SOQL. This blog explains how you can use the class and how I developed it (maybe you can grab some ideas for similar classes you are building).

Using this class is quite simple, and because I have written unit tests that validates 100% of the code you can easily use it in production sytems.

This class can can sort any List on any field. It can sort the list either in ascending or descending order. The class does not care what type of sObjects are in the list, so you could use it for custom and/or standard sObjects.

Performance

  • The class performs quite well because the sorting is done in memory (using Maps, Sets and Lists).
  • The class also caches previous sorts, so if it detects that you are re-sorting on a previously sorted field, it uses the information from the cache and does not re-sort.
  • If you sort in ascending order and then request a sort in descending order for the same field, the class uses the data in the cache.
How to use the class?

Before going into details of the Appex class, let me show you how the class can be used. For this example, I'm going to build a VisualForce page that shows a datatable with information from the Contact sObject. The table has three columns with three columns (Contact name, Contact phone, Account related to this contact).

The VisualForce page:

<apex:page controller="SorterContact">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection columns="1" ID="AjaxTable">
                <apex:datatable value="{!List}" var="acc" Border="1" cellspacing="1" cellpadding="5">
                    <apex:column >
                        <apex:facet name="header">
                            <apex:commandButton action="{!SortByName}" value="Sort By Name" rerender="AjaxTable" />
                        </apex:facet>
                        <apex:outputText value="{!acc.Name}"></apex:outputText>
                    </apex:column>
                    <apex:column >
                        <apex:facet name="header">
                            <apex:commandButton action="{!SortByPhone}" value="Sort By Phone" rerender="AjaxTable" />
                        </apex:facet>
                        <apex:outputText value="{!acc.Phone}"></apex:outputText>
                    </apex:column>
                    <apex:column >
                        <apex:facet name="header">
                            <apex:commandButton action="{!SortByAccount}" value="Sort By Account" rerender="AjaxTable" />
                        </apex:facet>
                        <apex:outputText value="{!acc.Account.Name}"></apex:outputText>
                    </apex:column>
                </apex:datatable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Nothing fancy here... Just building a page like this:

When the buttons on the header are clicked, the table is sorted either ascending or descending. If the user selects a different column from the previous one, then the table gest sorted ascending by that column. But when the user clicks on the button for the same column, then the sort order gets reversed from ascending to descending and viceversa.

The controller:

Couple things going in here, but that is just to make the page look nice... Nothing really to do with the sorting.

public class SorterContact {
    private String sortedBy = null;
    private Boolean sortAscending = null;
    private AP_SortHelper sorter = new AP_SortHelper();
    private List<Contact> sortedList = null;

    public SorterContact() {
        sorter.originalList = [SELECT Name, Phone, Account.Name FROM Contact];
    }
    public PageReference SortByName() {
        setSortedBy('NAME');
        sortedList = (List<Contact>) sorter.getSortedList('Name', sortAscending);
        return null;
    }
    public PageReference SortByAccount() {
        setSortedBy('ACCOUNT');
        sortedList = (List<Contact>) sorter.getSortedList('Account.Name', sortAscending);
        return null;
    }
    public PageReference SortByPhone() {
        setSortedBy('PHONE');
        sortedList = (List<Contact>) sorter.getSortedList('Phone', sortAscending);
        return null;
    }
    public List<Contact> getList() {
        if (sortedList == null) {
            SortByName();
        }
        return sortedList;
    }
    private void setSortedBy(String value) {
        if (sortedBy == value) {
             sortAscending = !sortAscending;
        } else {
            sortAscending = true;
        }
        sortedBy = value;
    }
}
Let me talk about the easy part first...

There are methods that answer the calls from the commandbuttons on the page:

  • SortByName()
  • SortByAccount()
  • SortByPhone()
These methods follow the same structure:

public PageReference SortByName() {
   setSortedBy('NAME');
   sortedList = (List<contact>) sorter.getSortedList('Name', sortAscending);
   return null;
}
First, it calls a method setSortedBy() to find out the ascending or descending order. If the user clicks on a different button, the table is sorted ascending by that column, ortherwise the order is inverted from Ascending to descending and viceversa.

Second, it calls the method in the Appex class that does the sorting. (I will explain on detail how to use the Appex class, keep reading)

Finally, the controller's method returns a null value to the page.

The controller's constructor gets the list from the database.

public SorterContact() {
   sorter.originalList = [SELECT Name, Phone, Account.Name FROM Contact];
}
Since the buttons use the rerendered propery, and therefore a partial page refresh is performed using AJAX, the class constructor is only called at the initial page load rather than every time the buttons are clicked. This means the SOQL statement gets called only once regardless of how many times the data table gets sorted.

AP_SortHelper:

Finally, the more interesting part... This is the Appex class that does the sorting. Please note that you do not need to understand how this class is built in order to be able to use it (just grab the source code below and paste it into salesforce.com).

public class AP_SortHelper {
    private Map<String, Integer> listPosition = null; // <ID, Position>
    private Map<String, List<String>> sortedFieldValuesPerFieldName = null; // <FieldName, <FieldValues>>
    private Map<String, Map<String, List<String>>> sObjectIDsPerFieldNames = null; // <FieldName, <FieldValue, <IDs>>>

// Properties
    public List<sObject> originalList {get; set;}

// Constructor
    public AP_SortHelper() {
        originalList = null;
    }

// Public Method
    public List<sObject> getSortedList(String fieldName, Boolean ascending) {
        if (originalList == null) {
            // Assume that originalList has a not NULL value.
            // If the class who uses this method has not assigned a value it will get an Exception which
            //    needs to be handled by the calling class.

            // Force the exception...
            originalList.clear();
        }

        // Make field name uppercase
        fieldName = fieldName.toUpperCase();

        // Get sorted list
        return makeSortedList(fieldName, ascending);
    }
    public List<sObject> getSortedList(List<sObject> originalList, String fieldName, Boolean ascending) {
        this.originalList = originalList;
        sortedFieldValuesPerFieldName = null;
        return getSortedList(fieldName, ascending);
    }
    public static String getValue(sObject sObj, String fieldName) {
        // This returns the sObject desired in case the fieldName refers to a linked object.
        Integer pieceCount;
        String[] fieldNamePieces;
   
        fieldNamePieces = fieldName.split('\\.');
        pieceCount = fieldNamePieces.size();
        for (Integer i = 0; i < (pieceCount-1); i++) {
            sObj = sObj.getSObject(fieldNamePieces[i]);
        }
        return String.valueOf(sObj.get(fieldNamePieces[pieceCount-1]));
    }


// Private Methods
    private void InitializeFieldName(String fieldName) {
        String sObjectID;
        Integer position;
        String fieldValue;
        List<String> sObjectIDs = null;
        Set<String> valuesForFieldSet = null;    // Sets automatically omit duplicate values
        List<String> valuesForFieldList = null;
        Map<String, List<String>> sObjectIDsPerFieldValues = null;
   
        // Make sortedFieldValuesPerFieldName
        if (sortedFieldValuesPerFieldName == null) {
            listPosition = new Map<String, Integer>();
            sortedFieldValuesPerFieldName = new Map<String, List<String>>();
            sObjectIDsPerFieldNames = new Map<String, Map<String, List<String>>>();
        }
   
        // Get (or create) map of sObjectIDsPerFieldValues
        sObjectIDsPerFieldValues = sObjectIDsPerFieldNames.get(fieldName);
        if (sObjectIDsPerFieldValues == null) {
            sObjectIDsPerFieldValues = new Map<String, List<String>>();
            sObjectIDsPerFieldNames.put(fieldName, sObjectIDsPerFieldValues);
        }
        if (!sortedFieldValuesPerFieldName.keySet().contains(fieldName)) {
            // Objects need to be initialized
            position = 0;
            valuesForFieldSet = new Set<String>();
            listPosition = new Map<String, Integer>();
       
            for (sObject sObj : originalList) {
                sObjectID = sObj.ID;
                fieldValue = getValue(sObj, fieldName);
           
                // Add position to list
                listPosition.put(sObjectID, position++);
           
                // Add the value to the set (sets rather than lists to prevent duplicates)
                valuesForFieldSet.add(fieldValue);
           
                // Get (or create) map of sObjectIDs
                sObjectIDs = sObjectIDsPerFieldValues.get(fieldValue);
                if (sObjectIDs == null) {
                    sObjectIDs = new List<String>();
                    sObjectIDsPerFieldValues.put(fieldValue, sObjectIDs);
                }
           
                // Add ID to sObjectIDs
                sObjectIDs.add(sObjectID);
            }
       
            // Sort set items (Need to convert to list)
            valuesForFieldList = new List<String>();
            valuesForFieldList.addAll(valuesForFieldSet);
            valuesForFieldList.sort();
       
            // Now add it to the map.
            sortedFieldValuesPerFieldName.put(fieldName, valuesForFieldList);
        }
    }
    private List<sObject> makeSortedList(String fieldName, Boolean ascending) {
        Integer position;
        List<String> sObjectIDs = null;
        List<String> valuesForFieldList = null;

        // Initialize objects
        InitializeFieldName(fieldName);

        // Get a list of the same type as the "originalList"
        List<sObject> outputList = originalList.clone();
        outputList.clear();

        // Get a list of sorted values
        valuesForFieldList = sortedFieldValuesPerFieldName.get(fieldName);
   
        // for each sorted value
        for (String fieldValue : valuesForFieldList) {
            // Get lisft of IDs
            sObjectIDs = sObjectIDsPerFieldNames.get(fieldName).get(fieldValue);
       
            // for each ID
            for (String ID : sObjectIDs) {
                // Get position in originalList
                position = listPosition.get(ID);

                // Add each sObject to the list.
                if ((ascending)  (outputList.size()==0)) {
                    outputList.add(originalList[position]);
                } else {
                    outputList.add(0, originalList[position]);
                }
            }
        }
        return outputList;
    }

// Unit testing
/*
    static testMethod void testSortCustomObject() {
        List<TPValue__c> TPValues;
        AP_SortHelper sorter = new AP_SortHelper();
        String fieldName;
   
        TPValues = [SELECT TPName__r.TPName__c, Value__c FROM TPValue__c LIMIT 50];
        fieldName = 'Value__c';
        testOrderedList(sorter.getSortedList(TPValues, fieldName, true), fieldName, true);
   
        fieldName = 'TPName__r.TPName__c';
        testOrderedList(sorter.getSortedList(TPValues, fieldName, true), fieldName, true);
    }
*/
    static testMethod void testSimpleField_Ascending() {
        testSortingContacts('Name', true);
    }
    static testMethod void testSimpleField_Descending() {
        testSortingContacts('Name', False);
    }
    static testMethod void testLookupField_Ascending() {
        testSortingContacts('Account.Name', True);
    }
    static testMethod void testLookupField_Decending() {
        testSortingContacts('Account.Name', False);
    }
    static testMethod void testMultipleCalls() {
        AP_SortHelper sorter;
        sorter = testSortingContacts(null, 'Name', true);
        testSortingContacts(sorter, 'Name', False);
        testSortingContacts(sorter, 'Account.Name', True);
        testSortingContacts(sorter, 'Account.Name', False);
    }
    static testMethod void testForgotOriginalList() {
        Boolean exceptionDetected = false;
        AP_SortHelper sorter = new AP_SortHelper();
        try {
            sorter.getSortedList('Name', true);
        } catch (NullPointerException e) {
            exceptionDetected = true;
        }
        System.assert(exceptionDetected);
    }
    static testMethod void testPassingList() {
        AP_SortHelper sorter = new AP_SortHelper();
        List<Contact> contacts = [SELECT Name, Phone, Account.Name FROM Contact LIMIT 50];
        List<Contact> sortedList = (List<Contact>) sorter.getSortedList(contacts, 'Name', true);
        testOrderedList(sortedList, 'Name', true);
    }
    private static void testSortingContacts(string fieldName, Boolean isAscending) {
        testSortingContacts(null, fieldName, isAscending);
    }
    private static AP_SortHelper testSortingContacts(AP_SortHelper sorter, string fieldName, Boolean isAscending) {
        // If sorted is null,create it.   
        if (sorter == null) {
            sorter = new AP_SortHelper();
            sorter.originalList = [SELECT Name, Phone, Account.Name FROM Contact LIMIT 50];
        }
   
        // Sort list
        List<Contact> sortedList = (List<Contact>) sorter.getSortedList(fieldName, isAscending);

        // Test sort order
        testOrderedList(sortedList, fieldName, isAscending);
   
        return sorter;   
    }
    private static void testOrderedList(List<sObject> sortedList, string fieldName, Boolean isAscending) {
        String lastValue = null;
        String currentValue = null;

        for (sObject sObj : sortedList) {
            currentValue = getValue(sObj, fieldName);
            if ((lastValue != null) && (currentValue != null)) {

                String strDebug = '';
                strDebug += '\n--------------------------------------------------------------';
                strDebug += '\nSTART';
                strDebug += '\n--------------------------------------------------------------';
                strDebug += '\n[Ascending:'+isAscending+']';
                strDebug += '\n[Previous:'+lastValue+'] [IsNull():'+(lastValue==null)+']';
                strDebug += '\n[Current:'+currentValue+'] [IsNull():'+(currentValue==null)+']';
                strDebug += '\n[CompareTo:'+(currentValue.compareTo(lastValue))+']';
                strDebug += '\n--------------------------------------------------------------';
                strDebug += '\nEND';
                strDebug += '\n--------------------------------------------------------------';
                System.debug(strDebug);

                if (isAscending) {
                    System.assertEquals(currentValue.compareTo(lastValue)>=0, true);
                } else {
                    System.assertEquals(currentValue.compareTo(lastValue)<=0, true);
                }
            }
            lastValue = currentValue;
        }
    }
}
Summary: How to use this class?

  1. Copy the source code above into Salesforce.com
  2. Create an instance of this class AP_SortHelper()
  3. Assign the list to sort (this list is created when you execute a valid SOQL statement)
  4. Call the getSortedList() method which takes two parameters:
    1. The name of the field as it was used in the SOQL
    2. The order (true for ascending, false for descending)
Please let me know if there are people interested in the explanation of how the sorter class works. If there are enough people interested, I'll revisit this post and explain how I did it.

Tuesday, January 27, 2009

Salesforce Very Useful Documents

I keep this documents open all the time

Force.com Sites Developer Challange

While learning about salesforce.com and developing software for the cloud, I found the "Force.com Sites Developer Challenge". One of the things that attracted my attention was that any site submitted to them would receive a T-Shirt.

I developed this site on December 2008 while I was looking for work, to solve a problem I was having:

  • There are many jobs posted daily on the job boards that a person may want to apply, but it is so time consuming to personalize each cover letter (which Human Resource departments love to receive).
  • Additionally, there needs to be a way to keep track of the networking activities that a person looking for a job needs to do.

It started being a simple cover letter creator, but it turned out to be a really good "mail merge" application which uses special functions (NOT_EMPTY(), IF(), ...) to do the merging and the merged letter can use system parameters or custorm parameters with default values or values specific per document.

Use this information to check out the site: URL: http://aperez-developer-edition.na6.force.com/ResumeHome Username: test2@apc.com Password: testpwd2a I created an users guide document that explains the site. You can download the document here.

Let me know if you want to see the code for this site and I will be glad to expand this post.

Who am I?

I am a Salesforce.com consultant in Markham, Ontario and I love helping other salesforce.com developers with their tough questions.

I learned about salesforce.com in December 2008, and I was immediatly captured by the revolutionary idea of Developement-as-a-service. While learning about the technology and the tools used to develop for the cloud, I learned about the "Force.com Sites Developer Challenge" which were giving away a t-shirt for creating a site using their technology. I decided to join the challenge and get a t-shirt, although I did not win any of the prizes... I did get my T-shirt.

Once I had become familiar with the technology, I decided to start helping users in the community boards. One day, I designed a Apex class that Sorts Any List and I had no place to post it so that users like you would be able to read about it, download it and use it. This is when I decided to create this blog.

Enjoy.