Sunday, April 5, 2015

Convert Complex String into Normal Format in Apex

// An array contains lst of Strings 
// Name, Title/Role, Company Name 
List<String> lstIntroductionWords = new List<String>{'Marc Benioff', 'CEO', 'Salesfore.com'};

// A string contains senetence template for the introduction
String templateIntroduction = 'My name is {0}. I am The {1} of {2}.';    

// A string contains sentence with complete words populated in a template sentence
String completeIntroduction = String.format(templateIntroduction, lstIntroductionWords);    

// See the complete sentence for the introduction
System.Debug('>>Introduction<<'+completeIntroduction);

Result should be as below sentence
>>Introduction<< My name is Marc Benioff. I am The CEO of Salesfore.com.

Convert String into Acceptable DateTime in Apex

// API Response DateTime = 2015-03-16T16:04:56.0000000+00:00
// Acceptable Format = 2015-03-16 16:04:56

// DateTime in String format
String dateTimeInString = '2015-03-16T16:04:56.0000000+00:00';

// Convert String into DateTime using "replace" method of String and "Valueof" method of DateTime
DateTime acceptableDateTime = DateTime.Valueof(dateTimeInString.replace('T', ' ')); 

System.Debug('>>Acceptable DateTime :<<'+acceptableDateTime);

Result

>>Acceptable DateTime :<< 2015-03-16 16:04:56

Why didn't the Force.com Platform Developers use SQL?

SOQL (Salesforce.com Object Query Language)

As a developer, you are no doubt already familiar with some query languages. The most common query language is SQL, or Standard Query Language, the universal standard for interacting with relational databases.

Just as the Force Platform data repository is similar to a relational database, SOQL is similar to SQL. In most places where there is overlapping functionality, the syntax for SOQL is identical to SQL. But there are a few important differences between the two data access languages.
  • SOQL is a query-only language – SQL has syntax for both reading and writing data, while SOQL is only used to retrieve records from your Force Platform objects. Later in this chapter, you will learn about how to write data using Apex data manipulation language.
  • SOQL uses relationships, not joins – In SQL, you implement relationships between tables using join syntax in the where clause of the query. SOQL syntax recognises relationship fields to identify the connections between objects, but does not support join syntax. You will see how to use relationships in your SOQL statements in the next section.
  • SOQL does not support all SQL keywords – SOQL syntax is described in brief in the following section, and in more detail in the Force Platform API documentation. You should assume that SQL syntax not covered in the documentation is not accepted in SOQL.

Why not SQL?

As a developer, your career might have encompassed using several different relational databases, and all of them used a similar form of SQL. You might be asking yourself the question in the title of this sidebar—why didn't the Force Platform developers use SQL?

The simple answer is that Force Platform objects are not the same as SQL tables and the Force Platform data store is not the same as a relational database. Access to data is always through some dialect, rather than directly, whether that interface is SQL or something else. In this fashion, SOQL provides query capabilities for Force Platform objects. The Force Platform environment offers you a lot of functionality and developer productivity based on the use of these objects, which are not the same as relational tables.

The complete Force Platform platform gives you everything you need to store and manipulate your data–the language and methods used to accomplish these ends are just a little different than those used for classic relational databases.

Journey from SQL to SOQL

Pagination with a Standard List Controller


In Visualforce you can add pagination logic in a visualforce page using a Standard List Controller by helping standard functions of pagination "first", "previous", "next", "last". This page also have a capability to mass update records using Inline Editing Support.


If you create a visualforce page with the following markup:

<apex:page standardController="Account" recordSetVar="accounts" tabstyle="Account">

    <apex:form >

        <apex:pageblock id="customerPageBlock" title="Mass Update Customers">

            <apex:pageblocktable value="{!accounts}" var="acct" id="customerTable">

                <apex:column headerValue="Customer ID">
                    <apex:outputField value="{!acct.Id}"/>
                </apex:column> 
                <apex:column headerValue="Customer Name">
                    <apex:outputField value="{!acct.Name}"/>
                </apex:column> 
                <apex:column headerValue="Industry">
                    <apex:outputField value="{!acct.Industry}"/>
                </apex:column>                             
                <apex:inlineEditSupport event="onClick"/>
            </apex:pageblocktable>

            <center>
                <apex:panelGrid columns="7">
                    <apex:commandButton action="{!quickSave}" value="Save"/>
                    <apex:commandButton action="{!Cancel}" value="Cancel"/>
                    <apex:inputHidden />              
                    <apex:commandButton disabled="{!NOT(hasPrevious)}" action="{!first}" value="First"/>
                    <apex:commandButton disabled="{!NOT(hasPrevious)}" action="{!previous}" value="Previous"/>
                    <apex:commandButton disabled="{!NOT(hasNext)}" action="{!next}" value="Next"/>
                    <apex:commandButton disabled="{!NOT(hasNext)}" action="{!last}" value="Last"/>
                </apex:panelGrid>
            </center>

        </apex:pageblock>
    </apex:form>
</apex:page>








Considerations

  • By default, a standard list controller returns 20 records on the page.
  • To control the number of records displayed on each page, use a controller extension to set the pageSize. See Controller Extensions.

Saturday, April 4, 2015

Visualforce Tips & Tricks

outputLink in Visualforce

<apex:page >
    <apex:pageBlock title="Visualforce Pages">
        <apex:outputLink value="{!$Page.SearchPage}?id=101" target="_blank">Search Page</apex:outputLink>    
    </apex:pageBlock>
</apex:page>


MVC & Visualforce

What is MVC?

Model–View–Controller (MVC) is a software architectural pattern for implementing user interfaces. It divides a given software application into three interconnected parts:

  • Model - A model consists of application data, business rules, logic and functions
  • View - A view can be any output representation of information
  • Controller - A controller accepts input and converts it to commands for the model or view

MVC Paradigm






Visualforce and the MVC Pattern






MVC Example in Visualforce






Declarative v/s Programmatic Tools in Salesforce

In Salesforce.com we have 2 ways to customise our Salesforce.com organisation:

  • Declarative (Point-And-Click)
  • Programmatic (Apex / Visualforce / API)



Advantages of Declarative v/s Programmatic




Most complete and robust solutions actually use a combination of declarative and programmatic solutions.

Developers should understand how to develop using both declarative and programmatic features of Salesforce.com.

Always try to customise Salesforce.com using OOTB (out-of-the-box) "or" declarative "or" Point-And-Click features of Salesforce.com.