Using custom controllers in vf page

custom controllers in visualforce page

What is Custom controllers? By using custom controllers we can write logic to visualforce pages.

There are three types of controllers available in visualforce. Those are  Standard Controllers, Controllers & Extensions.

To understand about visualforce controllers go to visualforce controllers

Below example helps you to understand how to write custom controller.

Custom controller is an apex class which is used to implement logic of visualforce page without leveraging the standard functionality.

Syntax to use custom controllers in visualforce page

<apex:page controller=”your controller name“>

//your vf  page code.

</apex:page>

Now i will create visualforce page called” insertcollage” collage is a custom object in my org. To enter collage record i am using my own custom save functionality instead of standard save functionality.

Visualforce page code for “insertcollagepage”

<apex:page controller=”inserCollage”>
<apex:form >
<apex:pageBlock title=”Collage Details”>
<apex:pageblockSection title=”Collage Information”>
<apex:inputField value=”{!coll.collage_name__c}”/>
<apex:inputField value=”{!coll.collage_id__c}”/>
<apex:inputField value=”{!coll.collage_phone_number__c}”/>
<apex:inputField value=”{!coll.adress__c}”/>
</apex:pageblockSection>
<apex:pageBlockButtons >
<apex:commandButton value=”Save Collage” action=”{!save}”/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

Custom controller for above visualforce page

public class inserCollage {

public collage__c coll;

public void insertCollage(){
coll = [select id,college_name__c,college_id__c,college_phone_number__c,adress__c from collage__c where id =: ApexPages.currentPage().getParameters().get(‘id’)];
}
public collage__c getcoll(){
return coll;
}
public PageReference save() {
upsert coll;
PageReference pref = new ApexPages.StandardController(coll).view();
return pref;
}
}

This is a simple example to under stand to understand custom controllers. In above visualforce i have created a command button called save collage and written logic for that in controller. To under stand this you need basic knowledge about Opps concepts and java.

If you want use standard controller and custom controller at a time then you can go for extension. Below is the syntax to use custom controller.

<apex:page standardController=”standardcontrollernameextensions=”controller name”>

//your vf  page code.

</apex:page>

Overriding Standard button with visualforce page

Overriding Standard button with visualforce page

By using visualforce pages we can override standard buttons. This example helps you to understand how you can overriding standard button with visualforce page.  In this example i am creating visualforce page called “newopportunitycreatepage”  and I am overriding standard new button with above created visualforce page.

below is code for “create newopportunitycreatepage”  ( to crate vf page go to How to create visualforce page?)

<apex:page standardController=”Opportunity” sidebar=”false”>
<apex:form >
<apex:pageBlock title=”Opportunity Detail”>
<apex:pageBlockbuttons >
<apex:commandButton value=”Save Opportunity” action=”{!save}”/>
</apex:pageBlockbuttons>
<apex:pageBlockSection title=”Opportunity Information 1″ columns=”2″ collapsible=”false”>
<apex:inputField value=”{!opportunity.Name}”/>
<apex:inputField value=”{!opportunity.Active__c}”/>
<apex:inputField value=”{!opportunity.DeliveryInstallationStatus__c}”/>
<apex:inputField value=”{!opportunity.TrackingNumber__c}”/>
<apex:inputField value=”{!opportunity.CloseDate}”/>
</apex:pageBlockSection>
<apex:pageBlockSection title=”Opportunity Information 2″ columns=”2″ collapsible=”false”>
<apex:inputField value=”{!opportunity.Description}”/>
<apex:inputField value=”{!opportunity.LeadSource}”/>
<apex:inputField value=”{!opportunity.NextStep}”/>
<apex:inputField value=”{!opportunity.Type}”/>
<apex:inputField value=”{!opportunity.StageName}”/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 This is simple visual force page code. Below is the output of this code.

overriding standard button

Now we are ready with our vf page, next step how to override standard opportunity new button.

Steps to overriding standard buttons

Go to setup -> opportunity -> Buttons,links, and actions, then click edit on new action. see below image for reference.

Override New Button

Next select override with visualforce option and select “newopportunitycreatepage” you created above and save your changes. See the below image for reference.

Override New Button2

Now you are done with overriding your standard opportunity new button.

How to test this?

To test this just go to opportunity tab and click on new button. you will get newly created visualforce page. And also you can see the difference in url. Your url will navigate to your visualforce page url.

Hope you under stand how to override standard buttons with visualforce page. By using this you can override stadard buttons for any object(Standard or Custom). But for custom object navigation is different.

For custom object just go to setup -> build -> create ->object  -> and select your object there you can find buttons and links section.

Visualforce – Creating simple page

Creating visualforce pages in salesforce:

visualforce is very interesting framework in salesforce. It is a markup language you can use this to develop user interface according to your requirements in salesforce. visualforce runs on Force.com platform.  To design UI you can write visualforce pages and by using controllers you can write business logic to your visaulforce pages. In visualforce pages to write business logic we have there types of controllers. – A standard controller – A custom controller – A controller extension We will discuss about these controllers later. Here I will give you very simple example to create visualforce page. Will provide more information in later posts. To create VF page go to setup -> Build -> develop -> Pages and click on new button. Enter required fields and you can write pages in specified section. Visualforce Page   In the above image you can write your page in red colored marked space.

<apex:page >
<h1>Welcome to Salesforce</h1><br/>
This is your new Page
</apex:page>

Above snippet is simple VF page. Here will explain about about this. Every Page should start and end with  <apex:page> </apex:page> To see the output for above go to url and type /apex/pagename.  https://c.ap1.visual.force.com/apex/MyFirstVisualforcepage .

Other Way

Now i will explain second way to create visualforce page. Go to url and type /apex/pagename after visual.force.com https://c.ap1.visual.force.com/apex/MyVFpage and click on enter.

If the page already available you will see output, If that page is not available, it will ask to create that page. See the below image for reference. visualforceClick on link. The page will create and you can create that page and after saving you see output. See the  the below image for reference. visualforce Vf 3 See the above image for reference. This option is available only when you enable developer mode at user level.

Below is the simple example, how to use simple simple formulas, functions in vf page.

<apex:page sidebar=”false” showHeader=”false”>
          <h1>Hello “{!$User.LastName}” Welcome to Salesforce</h1>
          <p> Today’s Date is {! TODAY()} </p>
          <p> Next week it will be {! TODAY() + 7} </p>
          <p>The year today is {! YEAR(TODAY())}</p>
          <p>Tomorrow will be day number {! DAY(TODAY() + 1)}</p>
          <p>Let’s find a maximum: {! MAX(1,2,3,4,5,6,5,4,3,2,1)} </p>
          <p>The square root of 49 is {! SQRT(49)}</p>
          <p>Is it true? {! CONTAINS(‘salesforce.com’, ‘force.com’)}</p>
</apex:page>

{!$User.LastName} is simple formula. By using this formula we can get the user name. User is the standard object which stores user information in your salesforce org.

{! TODAY()} : It will display today date.

{! TODAY() + 7} : It will display date after 7 days.

{! YEAR(TODAY())} : It will display year of the today

{! DAY(TODAY() + 1)}: It will display day of the today

{! MAX(1,2,3,4,5,6,5,4,3,2,1)}: It will display maximum number.

{! SQRT(49)} : It will display square root of given number.

{! CONTAINS(‘salesforce.com’, ‘force.com’)} : It will display Boolean value based on the given condition

Hope you understand how to create simple vf page. Later we will discuss about more concepts in visualforce pages.

Apex Trigger – How to create simple trigger

Apex Trigger

what is apex trigger ? and how to create simple apex trigger ? Here in this post i will provide information about apex trigger.

Apex Trigger is an action which gets fired on particular event. In salesforce trigger is apex code that executes before or after the below types of operations.

– Insert

– Update

– Delete

– Undelete

Triggers will run before object records are inserted, updated, deleted into the database or after records are inserted, updated, deleted and restored.

Apex Triggers can be classified into two types:

1. Before triggers can be used to update or validate record values before they are saved to the database.

2. After triggers can be used to access field values that are set by the database, and to affect changes in other records.

Events in triggers:

Before Insert, Before Update, Before Delete

After Insert, After Update, After delete, After Undelete

Syntax to create sample trigger:

Use below syntax to create trigger.

trigger <TriggerName> on ObjectName (<events>) {

// you can write your code here.

}

Now i will give simple example to under stand how trigger works.

Here i will give example, when account is inserted, automatically contact also created for that account. this example is to under stand how trigger works.

trigger insertContact on Account (after insert)
{
Contact cont = new Contact();
cont.LastName = Trigger.new[0].name;
cont.AccountId = Trigger.new[0].ID;
insert cont;
}

Above one is a simple trigger to insert contact when you create account. the text which is mentioned in green color is trigger name. And text which is mentioned in red color is event name.

Trigger.New is a context variable. which returns a list of new records of the sobjects which we are going to insert into the database.

Here in salesforce there many context variable available. Those are Trigger.New, Trigger.old, Trigger.NewMap, Trigger.OldMap, Trigger.isAfter, Trigger.isBefore, Trigger.isInsert, Trigger.isUpdate, Trigger.isDelete and Trigger.isUndelete.

We will discuss about each and every context variable later.

Here one more thing i will tell. If you want to move your trigger to Production, you should cover at least 1% code coverage for trigger.

Hope this helps you to under stand how to create simple trigger. Will post more later.

Salesforce app exchange

Salesforce app exchange

Salesforce App Exchange is a cloud computing marketplace, developed and hosted by salesforce.com and it is launched in 2005 by salesforce.com.

Salesforce app exchange is world’s largest, top most and first cloud computing application. Salesfroce app exchange is custom developed applications where u can build, customize and develop salesforce applications.

Using salesforce app exchange you can share applications to million of other users all over the world.

Benefits of using Salesfroce AppExchange

  • Companies/Users can use direct the app to download to your salesforce instance.
  • It is a gateway to a lot of prospects and has made it possible for many companies to build a rapport with potential customers
  • Customers can post reviews about their experience with apps and services, which are a great help when other customers are shopping for a particular app.
  • AppExchange provides a lot of scope to build and develop innovative applications.

How to install AppExchange app in salesforce?

Here i will explain how to install app exchange application in your salesforce organization. Now i will install a simple app and will explain. Now i need an app to generate documents. Sdocs is a free app to generate pdf, word and excel. To install this app follow below steps.

Step1: Login into your salesforce and go to app menu at and select app exchange . See the below image for reference

App Exchange

after clicking on app exchange it will navigate to “https://appexchange.salesforce.com/”

or you can directly type “https://appexchange.salesforce.com/” this url in your browser.

Step2: In this app exchange site you can search the app you need. Now i will search for Sdocs app. see the below image for reference.

App Exchange

Click on that app. After clicking on that app you will navigate to app detail page. That page contain details of that page like pricing, when it was released, and video’s or images about that app. see the below screen for reference.

App Exchange 3

To get the app click on Get it now button which is in green color in above image.

Step3:  After clicking on get it now, it will ask for where you are going to install. In Production? or in Sandbox?

Select the environment you are going to install the app and next accept the terms and conditions and Click on “Confirm and install ” button.

Step4: And next enter your environment credentials and log in. you will navigate to package installation details. see the below image for reference.

App Exchange

If you want to continue to install, click on continue button and click on NEXT .

Step5: Select the security levels. For whom you are giving access. To admins? or to all users? or by profiles?

Here i am giving permission to admins only. see the below image for reference.

App Exchange

And click on NEXT. And finally click on INSTALL button. Once installation complted. you will notified by an email.

If you want to check the app is installed or not, Go to Setup -> Build -> click on installed apchages. Here you can see installed package details in your org.

Apex Class – Simple Class to understand Apex

Apex Class

Here i this post i will explain about hoe to create a simple Apex Class?

Apex is strongly typed Object oriented programming language and it will run on Force.com platform.

Here i will give you the info about how to create class in salesforce.

Below is the example to create a simple class

Public class MyFirstApexClass

{

//Body of the Apex class. Here we can define variables and methods

}

This is the simple class definition. Generally in salesforce to define class you must use “class” keyword followed by access specifier. here access specifier and ‘class’ and class names are mandatory for every  class in salesforce.

Now i will tell you how to create class i salesforce?

To create a class in salesforce go to Setup -> Build -> Develop -> Apex Class and click on NEW button and create class  there.

Apex class

now i will create below call there.

public Class CreatingAccount
{
public Account createAccount(String name)  //method to create account
{
Account acc = new Account();
acc.Name = name;
return acc;
}
}

Above class is to create/insert new account. This is simple example to create an apex class. We will see more later.

Now here i will give you an example to create test class.

What is TEST class? Test class are allow to create test methods weather your functionality is working or not.

In salesforce Test classes are very important to deploy your code to PRODUCTION.

You need to cover at least 75% ( Average coverage of all classes) code coverage by using test methods in salesoforce to deploy your classes to PRODUCTION.

Here i will explain how to write test class for above class. Later will discuss more about test classes.

@isTest
public class CreateAccountTest
{
  static testMethod void testInsertAccount()
  {
  CreatingAccount ca = new CreatingAccount();
  ca.createAccount(‘TestclassAcc1’);
  
  }
}

Above class is simple test class. Which covers the code for above defined class.

 Now how can know percentage covered by mytest class to my main Apex Calss?

After saving your test class, you will get a button called Run Test. Click on that button, your test class will run.

TO see percent code coverage go to you main class and see the percentage. See the below image for reference.

apex class

 This is a small example to create class and test class for that. We will more about Apex classes and Test classes later.

Import Leads from setup Menu

How to Import Leads from setup Menu?

Here i will explain how to import leads from setup menu.

By using import leads option you can import up to 50,000 lead records.

By using this wizard you can avoid loading duplicate records.

Very easy to use, directly from set up menu you can load records.

Step by Step process to load Leads:

Go to Setup -> Administer -> Data Management -> import Leads and click on start import Wizard

See the below screen for reference.

import leads

After clicking on Start import wizard a new window will open that contains below information.

1. Launch the application and view the lead list

2. Select the Save As… or Export… menu option, usually from the File menu

3. Follow the directions to save the file in a Comma Separated Values or CSV format

4. You can optionally read the CSV File into Excel to edit the data or filter the leads to import

5. Now choose the file to import into salesforce.com:

Note: it may take a few minutes to upload your file, depending on the file size and your connection speed

6. Assign all new leads to this lead source:

7.  

8. Use assignment rule settings to send notification emails to record owners

9. The selection below is set to a default value. Override this default value only if your import file has a different character encoding.

10. Matching Type

Fill the requirements in above steps like. choose your file you want up load, If want to assign one lead source at the time loading you can select here, If you want assign assignment rules you can assign, you can send email notifications to record owners.

And you  can also select matching type to avoid duplicates.

See the below screen for reference.

import leads

After filling above info click on NEXT and map the excel columns with salesforce fields. See the below screen for reference.

Import Leads

And click on import now option to import your selected file.  And click on Finish.

You will get an email notification when your import completed.

For more information see below references.

https://help.salesforce.com/HTViewHelpDoc?id=import_my_organizations_leads.htm&language=en_US