Monday, March 7, 2011

Passing values between pages in Visualforce

I read a lot of information over the web scattered here and there. So I decided to blog about this. Often developers try to figure out what are the different ways through which values can be passed between pages in Visualforce. Well, there might be a number of them, but I would like to talk about the two most common ways to transfer data between the Visualforce web pages.


Method 1. Passing variable value between two pages that implement the same controllers and extensions.
Method 2. Passing values using a Querystring.


Method 1


In order to use the first method above, we need to ensure that the two pages in context need to have the same controller. This might be a Standard controller or a Custom controller.


Say we have to pages Page1 and Page2. Let the Visualforce syntax be as below:


Code for Page1
<apex:page controller="customcontroller1">
   <apex:inputtext id="inpEmployeeName" value="{!EmployeeName}"/><br/>
   <apex:CommandButton value="Next" action="{!next}" id="cmdNext" />
</apex:page>


Code for Page2

<apex:page controller="customcontroller1">
   .The employee name is {!EmployeeName}
</apex:page>

Code for customcontroller1
public with sharing customcontroller1
{
  public string EmployeeName {get;set;} 
  
  // Function for navigating to next page
  public PageReference next()
  {
     PageReference nextpage = new PageReference('/apex/Page2');
     return nextpage;
  }
}

Now there are a few points worthy to note out here. You need to be careful of the method nextpage.setRedirect(true) , a method used to redirect from one page to another. Use it only when you use querystrings in the URL. If you use this method in the method above, it will wipe out all the data stored in variables in page1 being transferred to page2, as page2 in such a scenario will loose all the variable data of the controller, when this page2 loads.


Method 2

This method involves transferring variable data from one page to another, using a query string. This method should be used when the controller of the pages are different. See example below:

Say we have two pages, Page3 and Page4.

Code for Page3
<apex:page controller="customcontroller3">
   <apex:inputtext id="inpEmployeeName" value="{!EmployeeName}"/><br/>
   <apex:CommandButton value="Next" action="{!next}" id="cmdNext" />
</apex:page>

Code for Page4
<apex:page controller="customcontroller4">
   .The employee name is {!EmployeeName}
</apex:page>

Code for customcontroller3
public with sharing customcontroller3
{
  public string EmployeeName {get;set;} 

  // Function for navigating to next page
  public PageReference next()
  {
     PageReference nextpage = new PageReference('/apex/Page4?EN=' + EmployeeName);
     return nextpage.setRedirect(true); //Note that setRedect method is used here
  }
}


Code for customcontroller4
public with sharing customcontroller4
{
  public string EmployeeName {get;set;} 
  
  //Constructor
  public customcontroller4()
  {
    String strEmpName = ApexPages.currentPage().getParameters().get('EN');
  }
 }

This way, you can transfer data from a page to another even if they don't have the same controller. 

There is another way to transfer data between pages, and that is using Javascript and hidden fields in the form. I will blog on that in my coming posts.

Till then, happy reading. Cheers!

Vishal

Saturday, March 5, 2011

Working with the Salesforce.com Database

Salesforce.com comes with its own database. Salesforce defines a database as "an organized collection of information". Databases in the real world are used to pile up information about real world objects such as employees, students, account holders, company names, industry related data etc. In a typical database such as an Oracle SQL Server or a  Microsoft SQL Server, a database is a collection of related tables, collectively called a schema. Each table contains information about one subject such as student information, where in the table name will typically be named as 'student' or 'table_student' because student is the main subject of importance in this table. The subject of importance is also known as an entity. When we talk about the same stuff in the world of Salesforce, we call this as an object. So basically we can also say that an object is a category of information.

It is worth to know here that there are pre-built objects in the Salesforce.com platform, that we call as sObjects (a short for Salesforce Objects). Beyond these pre-built objects, developers can create custom objects of their own. Thats the power of Salesforce.

A table is comprised of rows (the records) and columns (the fields). The same way, an object is also comprised of records and fields. 

The Salesforce.com is powered by the Force.com platform, a platform through which applications can be built using in built wizards, or through the combination of Visualforce (the pages) and Apex (the programming language). Apex is leveraged by a query language called SOQL (Salesforce Object Query Language) that allows querying objects in Salesforce.

However, it is important to note that SOQL does allow DML statements (such as insert, update, delete, upsert) but does not allow DDL statements (such as create, drop, alter).

Now that objects have been discussed, lets talk about what are fields. In short, fields are the column headers that signify what data about that object is there in that column. For example, we can have an object by the name "Employee" and we can have fields defined against this object, such as "Employee Name", "Age", "Designation", "DOB", "DOJ", "Department" etc. 

Moving on to the next level, a database comprises of multiple objects. This is to ensure that everything (ie every piece of data) is not packed into one object. For such scenarios, Salesforce.com allows relating tables. For example, we may associate a Manager with an Employee. That means we are talking about two objects here, the Employee object (that will house all the employees information) and the Manager object (that will house all the managers information).

Now to relate objects, we need unique columns in each table. This can be done by marking one column in a table as a primary key, and the corresponding column on the other object as the foreign key. 


In my next posts, I'm going to talk about the different types of relationships that can be created using the Force.com platform. 




Sunday, February 27, 2011

Ways to deploy Apex to a Sandbox org

The greatness of working on the Force.com platform is that it allows you to work on different environments. This means you can create your environment for Testing, a separate one for Training, and separate ones for Development and Production. A Developer usually works on the Developer org or the Sandbox org.

In this blog post, I'm going to point out the different ways through which deploy Apex code to a Sandbox Organization from your Force.com IDE (i.e. your local environment in Eclipse).

The first way is to use the Force.com Component Deployment Wizard. This allows developers to select only the Metadata components that need to be deployed, on the Sandbox org. Checkout this link for more details: http://wiki.developerforce.com/index.php/Deploy_Force.com_Applications_Faster

The second way is to use the deploy() metadata API call. This method allows moving metadata (XML files) from the local system to the Salesforce organization. Checkout this link for more details: http://www.salesforce.com/us/developer/docs/api_meta/Content/file_based.htm

Basically, this command can be executed in a console window. On a similar note, the retrieve() API call is used to pull data from the Salesforce org to the local environment.

Happy reading.

 









Sunday, February 20, 2011

Visualforce Markup and Controllers

A Visualforce page is comprised of two key elements:

1. Visualforce Markup
2. A Visualforce Controller

That means the page behaviour is a result of these two components. Visualforce is built on the MVC model. The markup is the VIEW part of the MVC model, whereas the controller is the CONTROLLER part of the MVC model. The MODEL is hidden behind, which is essentially the metadata repository. 

Visualforce Markup comprises of Visualforce tags, HTML, Javascript, Style components, any web-enabled code (for example : widgets), which are embedded within a single <apex:page> tag. The markup drives how the page should look like, when the page renders in a browser.

Visualforce Controllers are basically set of instructions created out of classes that may be standard or custom, that are responsible for driving how a page or rather to be precise, the page components, should behave when an event or action takes place on that page component. For example, if you are wondering what part of Visualforce takes care of the behavior of the application when a button click or a link click takes place, well, this is taken care by the controller.

There are two types of controllers - Standard Controller and Custom Controllers. A Standard Controller is a controller that is provided by the Force.com platform. On the other hand, a Custom Controller is basically a controller class written using the Apex language.

A Standard Controller comprises of logic and rules that are used in native Salesforce.com pages. For example, if we are using a standard Contacts controller and implementing the EDIT functionality, the same behavior is exhibited when EDIT action is being executed in the Salesforce.com Contacts page.  

And as obvious, if a Standard Controller is being used on a page where a user does not have privilege on that controller, a message is displayed that displays that the user does not have sufficient privileges over the controller. And such messages can ofcourse be avoided well in advance, by checking user's privileges on objects.

Whereas Standard Controllers act on single records, Standard List Controllers  act on multiple records. There are some examples of Salesforce.com pages that use Standard List Controllers such as:
1. List Pages
2. Related Lists
3. Mass action pages.

Now talking about Custom Controllers, these are basically classes that implement the entire logic of a page, without using Standard Controllers. However, some functionality of Standard Controllers are reimplemented.

System Mode vs. User Mode

It is interesting to know that Custom Controllers work in System Mode, which means that when Custom Controllers are used to render a Visualforce page, the object level permissions and field level permissions of the current user are ignored. However, it can be specified in the User's profile whether he/she can executed a method in a Custom Controller. This is a very powerful feature of a custom controller. Standard Controllers run in User Mode. This means that pages built on Standard Controllers will inherit the field level security plus the object level security of the user.

Now that we have discussed what are Standard Controllers and Custom Controllers, it is important to mention what Controller Extensions are. Well, Controller Extensions are nothing but classes built on Apex, that add to or override the functionalities of Standard or Custom controllers. This way, developers can make custom and standard controllers even more feature packed. Note that Controller Extensions run in System mode.

In my coming posts, I will talk about the optional modifier with sharing which allows the current user to get permissions even to pages that use Custom Controllers or Controller extensions.

Happy Reading.





Monday, February 14, 2011

Special escape sequence in Salesforce for Regex Validations

I found this out last week but simply wasn't able to share it. But better share than never! Well, it is somewhat tricky, though clearly specified in the Regex documentation page at developer.salesforce.com.

In a scenario when you want to speciy a regular expression that has a slash ie the '\' symbol, for example, a commonly used regex is '\w', the Salesforce validation interface doesn't allow a single slash, rather it accepts double slash in order to accept the '\w' that we normally would have written, the Salesforce platform accepts this as a '\\w' instead. Perhaps this is a special case wherein the slash symbol itself has to be represented as an escape sequence, and that is why we use double slash.

The gist of the story, whenever you wanna use a Regular expression with a slash, make sure you use double slash (ie '\\w') and that is when Salesforce will understand the regex what you wanted to set.

Happy coding!


Saturday, February 5, 2011

Introduction to Salesforce, Visualforce, Force.com, Apex, Triggers

Its been some time now since I've been on the Microsoft platform working on MS SQL, ASP.NET (all versions), C#, VB.NET, MVC, JQuery, SSIS, Ajax, SSRS, Azure.....well the list is very long. As the technical world is tilting heavily towards the cloud, I have decided to venture into the cloud and to begin, I've picked up the Salesforce platform. Though I'll still keep on writing new stuff for my popular technical site www.dotnetuncle.com , my focus for the near time to come would be exploring Salesforce.com and its underlying rapid application building framework and supporting tools such as the mighty Force.com, Apex, Visualforce, Triggers, Sites, Database.com and AppExchange.

Welcome to http://visualsalesforce.blogspot.com .

In the next blog posts, I'll be writing a lot on cloud technologies. Watchout and keep on following. Cheers!

Vishal