Posts

Showing posts from February, 2018

Collection Data Types in Salesforce

Collection-- A collection is a group of similar type element. It is the framework that provides Classes and Interfaces for grouping of the similar type elements. There are three types of collection in Apex-- 1. List--               A list is the ordered collection of elements which are distinguished by their index. List element can be of any data type(Integer, String, or sObject). so use list collection whenever you want to identify an element by their index. List<String> sList = new List<String>();   sList.add('Jon');   sList.add('smith');   system.debug(sList.size()); 2. Set-- A set is an unordered collection of elements that don't contain any duplicate element. Set can be any dataType (Integer, String, or sObject). So use set when you don't want any duplication in your collection. Set<String> str = new Set<String>();    str.add('Jane');     str.add('...

How to redirect to another Vf page after saving a record

To redirect to another page which is saved in your organization, use the standard feature provided by Salesforce  PageReference . A PageReference is a reference to an instantiation of a page. This example explains the process of redirecting to another VF Page when you save an account record.                                            Example Controller-- public class MyExtension  {     public Account ac {get;set;}          public MyExtension(ApexPages.StandardController controller)      {         ac = new Account();            }          public PageReference save()     {         insert ac;         PageReference pg = new PageReference('/apex/SecondVFPage');//Second //VFPage ...

Update a sObject record related with another sObject using Visualforce

Image
In this part, I am going to explain how to update a contact record related to an account object in the visualforce page. Scenario--  A VF Page showing a list of account record with a radio button and when you select an account from given list then it shows related contacts of selected account in the page and then you can update your record. Controller-- public with sharing class AllAccounts  {     public List<Account> acList {get;set;}     public List<Contact> conList {get;set;}        public string accId {get;set;}     public string eml {get;set;}          public AllAccounts()     {            acList = new List<Account>();         for(Account a : [select id,name,accountnumber from account limit 10])         {             acList.add(a);     ...

Dependent Picklist in Visualforce Page

Image
In this part, I am going to explain how to create a dependent picklist via some code. You can do this stuff without coding i.e. (point and click) tool for a sObject record. There are two terms that depict about FieldDependencies. 1.   Controlling Field 2.    Dependent Field Field dependencies are the filters that allow us to change the content of a picklist based on a value of another field. Controlling field that controls available values in one or more corresponding dependent picklist. A dependent field display values based on a value selected in the corresponding controlling field.                                            Example Contoller -- public with sharing class CreateDependentPicklist  {     public String selectedtState {get;set;}      public String selectedCtr {get;set;}     ...

Display total number of contacts associated with an account.

In this part, I am going to explain how to calculate the total number of contacts associated with an account object in a very simple way. After calculating the total number of contacts you can display this totals in a field of an account object.  This trigger works in all scenario after insert, after delete , after Undelete and after update. Requirement -- Create a Number type field named NumberOfContact on account object. Trigger -- trigger NumberOfContactOnAccount on Contact (after insert,after update,after delete,after undelete)  {     set<Id> ids = new set<Id>();          if(Trigger.isInsert)     {         for(Contact c : Trigger.new)         {             if(c.AccountId != null)             {                 ids.add(c.AccountId);       ...

Trigger for display or update account field after creating a related contact record

Here I am going to create a trigger that update value of TotalAmount field on Account object after creating a contact record. Requirement-- 1. create a currency type field name TotalAmount on Account object. 2. create two currency type field name AmountX and AmountY on Contact object. Scenario-- When we create or update a contact record with providing value of above created field then total value of AmountX and AmountY displays on TotalAmount field on Account object. Trigger-- trigger TotalAmount on Contact (after insert,after update) {          set<Id> ids = new set<Id>();     for(Contact c : Trigger.new)     {         if(c.AccountId != null){             ids.add(c.AccountId);         }     }          List<Account> acList = [select id,TotalAmount__c,(select id,AmountX__c,AmountY__c from contacts) ...

Dynamically Add/Delete rows in Visualforce Page

Image
Here is a simple example of adding or deleting rows dynamically in a VF page using the concept of Wrapper Class. Controller-- public with sharing class AddNewRows {     public List<ContactWrapper> cwrap {get;set;}     public Contact con {get;set;}     public Integer rowIndex {get;set;}          public AddNewRows(ApexPages.StandardController controller)     {         rowIndex = 0;         cwrap = new List<ContactWrapper>();         con = new Contact();         cwrap.add(new ContactWrapper(con,0));  //call ContactWrapper class //constructor     }          public void addRows()     {         rowIndex += 1;         Contact c = new Contact(); // create new instance of object         cwrap.add(new Contact...

How to search a sObject record using visualforce page

Here is a simple example that searches a standard or custom object record like we do in our salesforce organization's search bar. After searching record, you can do your custom action like delete, edit etc. VF Page-- <apex:page controller="AccountSearch">     <apex:form >       <apex:pageBlock >                          <apex:inputText value="{!searchStr}"/>               <apex:commandButton action="{!doSearch}" value="Find" reRender="lab"/>                  </apex:pageBlock>              <apex:pageBlock >           <apex:pageBlockTable value="{!acList}" var="ac" id="lab">               <apex:column headerValue="Name">         ...

How to use Javascript in visualforce page

Here I am taking a basic example of using Javascript in a visualforce page that illustrates how to call javascript function from VF page and how to reference id of a visualforce component. Use $component global variable to referencing DOM Id that is generated for a visualforce component. <apex:page>   <script>       function show(tid,sec){                     document.getElementById(sec).value = document.getElementById(tid).value;              }             </script>   <apex:form >       <apex:pageBlock >           <apex:pageBlockSection id="lab" >               <apex:inputText id="name" label="AccountName"/>               <apex:inputText id="second" label="Rating"/> ...

Individually Delete sObject records in Visualforce Page using concept of Wrapper Class

Image
Wrapper Class means a class inside another class. Definition-- A wrapper class is a class whose instances are the collection of other objects. Wrapper class is used to display different object on a visualforce page in the same table Scenario is like this when we create a contact record in visualforce page, then VF page show recently created record like this-- First the controller public with sharing class SingleRecordDelete {     public String fname {get;set;}     public String lname {get;set;}     public String did {get;set;}          public List<ContactWrp> crp {get;set;}          public SingleRecordDelete(){         crp = new List<ContactWrp>();              }          public void save(){       Contact c = new Contact();       c.firstname = fname; ...