Sunday, 9 July 2017

Example Programs using Set, Apex & Visual force

Example Programs using Set, Apex & Visual force 

Write an apex class to add list of elements to set 

Public class set example 
     { 
Public set <string> name {get; set;}; 
Public set example () 
     { 
List <string> my list = new list <string> ()
My list. add (‘sushi’); 
My list. add (‘sushi’);
My list. add (‘Rakesh’);
My list. add (‘Rakesh’); 
names = new set <string> (); 
names. add (‘one’); 
names. add (‘two’);
names. add (‘one’);
names. add (‘one’);
names. add (my list);
           }
     }

Public void retains all (list) // this will keep only the values that are existing in list and set. 
Remove rest of the value. 
Public class set example 
 {
Public set <string> names {get; set;};
Public Boolean test {get; set;}
Public set example ()
            {
               List <string> my list = new list <string>(); 
Mylist.add(‘sashi’);
Mylist.add(‘ram’);
Mylist.add(‘kumar’);
test = names.retainAll(mylist);
}
}

Visual force Page : 

<apex:page controller = “setExample”>
<apex:pageBlock>
<apex:datalist value =”{!names}” var =”a”>{!a}
</apex: datalist>
</apex: pageBlock>
</apex:page> 

Program to Demonstrate usage of set in Apex

Program to Demonstrate usage of set in Apex

write an apex class to demonstrate set

public class set example 
                { 
                  Public set<string> names {get; set;}
                  Public example ()
                  { 
                   names = new set <string> ();
                   names. add (‘one’);
                   names. add (‘two’); 
                   names. add (‘sam’); 
                   names. add (‘one’);
                   names. add(‘one’); 
                             }
                      } 

Visual force page: 

     <apex: page controllers = “set Example”>   
     <apex: page block>
     <apex: page block table value = “{! Names}” var = ‘a’>
     <apex: column value = “{! a}”/>
     </apex: page block table> 
     </apex: page block> 

     </apex: page> 

SET in Apex Salesforce Introduction

SET in Apex Salesforce Introduction 


Set: 
It is an unordered collection of elements where elements can be of any data type.Primitive types, and built – in apex types. 

1. Set don’t allow the duplicate values.
2. Insertion order is not preserved in the set. 
3. It grows dynamically at run time. 

Ex:  
          Set <string> names = new set<string> (); 
          Set <account> acc = new set <account> (); 
          Set <costumer – c> my costumers = new set <costumer—c> (); 
Public void add (object) // this method will add elements to the set. 
          Set <string> names = new set<string> (); 
          name. add (‘one’);
          name. add (‘two’);
          name. add (‘one’); 

Note: set will not allow duplicates, but if we insert it will not raise any error it will not takes values 

Public void add all (list) //this method will add all elements to the set. 
  
List <string> my list = new list <string> (); 
           My list. add (‘one’);
           My list. add (‘two’); 
Set<string> my names = new set <string> (); 
           My names. add (‘Sam’);
           My names. add (my list); 
Public integer size ()// this method will return no. of elements in the set.
           Set <string> names = new set <string> ();
           names. add (‘one’); 
           names. add (‘two’);
           names. add (‘one’); 

 Integer my size = names. size (); // this will return 3 and stored to my size variable.

Public void removes (integer index)// this method will remove the elements at the specified index. 
             names. remove (1); 

Example Programs using List, Apex and Visualforce

Example Programs using List, Apex and Visualforce


List of objects demo: 

        Public class example { 
        Public list <account> result {set; get;} 
        Public list example () 
               { 
         Account a1 = new account (name = ‘Sam’ industry = ‘banking’) 
         Account a2 = new account (name = ‘ram’ industry = ‘energy); 
                result = new list <account> (); 
                result. add (a1); 
                result. add (a2); 
                      } 
                  }
               <apex: page controller = “list example”> 
               <apex: page Block> 
               <apex: page block Table> 
               <apex: page> 

Create a list of apex class object:
1. Student apex:
Global student 
   { 
    Public string name {get; set;} 
    Public integer age {get; set;} 
    Public student (string name, integer age) 
            { 
              This. name = name; 
               This. age = age; 
                         }
                 }  

2. List example apex: 
Public class list example 
    { 
     Public list <student> result {set; get;}
     Public list example (). 
            { 
             result = new list <student> ();
student s1 = new student (‘sam’,20);  
student s2 = new student (‘Ram’,40); 
student s3 = new student (Praveen’,40); 
           result. add (s1); 
           result. add (s2); 
           result. add(s3); 
                   }
             } 

List example visual force:
<apex: page controller = “List example”> 
<apex: page block> 
<apex: page block Table value = ‘’ {! Result} “var= ‘a’> 
<apex: column value = “{! a.name}’/>
<apex: column value = “{! a. age}”/> 
<apex:  page block Table> 
<apex: page> 

Write an apex program to generate list of select options: 

Public class list select 
         { 
          Public string my val {get; set;} 
Public list < select option > my option; 
Public list <select option > get my options () 
 { 
   Return my options; 
      } 
   Public List select () 
          { 
My options = new List <select option> (); 
Select option s = new selection (‘null’,’-none- ‘); 
Select option s1 = new select option (‘one’, ‘Jan’); 
My options. add (s); 
My options. add (s1); 
My options. Add (new select option (‘two’,’feb’); 
My options. add (new select option (‘three’, ‘mar’); 
                   } 
              } 
<apex: page controller = “list select”>
<apex: from> 
<apex: select list size = “1” value = “{! my value}”/
<apex: select options value = “{! My options} “/> 
<apex: action support event =” unchanged’’ retender = “one”/> 
<apex: select List> 
<apex: output label id = “one” > you have selected {! My val}
<apex: output label>
<apex: from>
<apex: page>

Program to Demonstrate usage of List in Apex

Program to Demonstrate usage of List in Apex


Public class list example 

Public list <string> result {set; get;} 
Public list example (). 

Result = new list <string> (); 
Result. add (‘Sam’);
Result. add (‘ram’); 
Result. add (‘hair’); 
Result. add (1,’kumar’); 
}

<Apex: page controller = “list example”>
<apex: page block> 
<apex: page block table value = “{! result}” var = “a” > 
<apex: column value = “{! a}”/> 
<apex: page block > 
<apex: page> 

Methods in List class

Methods in List class

Add (object): Adds an element to the end of the list.
add (integer, object): -   Insert an element in to the list at the specified index position.

addAll(List): Adds all of these elements in the specified list to the list that calls the method. Both list must be same type.

addAll(set): Add all of the elements in specified set to the list that calls method. The set and the list must of the same type.

Clear(): Removes all elements from a list consequently setting the list’s length is zero.

Clone (): makes a duplicate copy of a list.

Deep clone (Boolean, Boolean, Boolean): makes a duplicate copy of a list of subject records, including the subject records themselves.

Equals(List): compares this list with the specified list and returns true if both list or equal. Otherwise false.

Get(integer): Returns the list elements stored at the specified index.

Gets object type (): return the token of the object type that makes up a list of subject. 

Hash code (): return the hash code corresponding to this list and its contents.

IsEmpty (): returns true if the list has zero elements.

Iterator (): returns an instance of an iterator for this list. 

Remove (integer): remove the list element stored at the specified index, returning the element that was removed 

Set (integer, object): sets the specified value for the element at the given index. 

Size (): Returns the numbers of elements in the list.

Sort (): sorts the items in the list in ascending orders.

Ex: List <string> str = new list <string> ();
        String s1 = ’Sam’;
        String s2 = ‘ram’;
        String s3 =’rave’;
        Star. add (s1);
        Star. add(s2); 
        Star. Add (1, s3); 
List <string> finalist = new list <string> ();
Finalist. add all (star); 
String x = star. get (1); // Ravi 

List Introduction in Apex Salesforce

List Introduction in Apex Salesforce

  • list is an interface.
  • A list is an ordered collection of elements that are distinguished by their indices.
  • list elements can be of any data type – primitive types, collections, subjects, usual – defined types and built – in apex types.

Index0                             Index1                         Index2                     Index3                   Index4
Green
Blue
Yellow
Red
Block


  • Insertion order is preserved.
  • can grow dynamically at run time.
  • Duplicate values are allowed.
  • Null values are accepted.