Sunday, 16 June 2013

Map usage in Apex

Map usage in APEX:

public with sharing class MapUsage {
        
        public static void mapUse(){
        
            Map<Integer, String> mp = new Map<Integer, String>();
            // kEy is Integer
            // value is String
            
            mp.put(1221,'Mahi');
            //mp.put(1226,'Shyam');
            mp.put(1211,'Rajesh');
            mp.put(1255,'Kiran');
            
            System.debug('---------------Elements in the map are ------------>'+mp);
            System.debug('---------------Size of the map is-------4---------->'+mp.size());
            System.debug('------------Keys in the map are ----------->'+mp.keyset());
            System.debug('------------Values in the map are ----------->'+mp.values());
            
            System.debug('------------------'+mp.get(1211));
            
            mp.remove(1226);
            
            mp.put(1211,'Charan');
            mp.put(1248,'Pavan');
            mp.put(1222,'Kiran');
            
            System.debug('---------------Elements in the map are ------------>'+mp);
            System.debug('---------------Size of the map is-------5---------->'+mp.size());
            
            for(Integer i : mp.keyset()){
                System.debug('-----Key--------'+i);
                System.debug('-----value---------'+mp.get(i));
            }
            
            for(String s: mp.values()){
                System.debug('----------------'+s);
            }
            
        }        
}

Set usage in Apex

Set usage in APEX:

public with sharing class SetUsage {
    
    public void setUse(){
        
        Set<Integer> st = new Set<Integer>();
        
        st.add(1234);
        st.add(1248);
        st.add(1225);
        st.add(1252);
        
        System.debug('----------------Elementa in the set are --------------'+st);
        System.debug('----------------Size of Elementa in the set are ------4--------'+st.size());
        
        st.remove(1252);
        System.debug('----------------Size of Elementa after removing an element in the set are ------3--------'+st.size());
        
        
        st.add(1248);
        st.add(1252);
        st.add(1221);
        System.debug('----------------Size of Elementa in the set after adding duplciates are ------5--------'+st.size());      
        
        Set<Integer> newst = st.clone();
        System.debug('----------------Size of Elementa in the new set are -------5-------'+newst.size());
        
        st.clear();
        System.debug('-----------------NEw set is EMpty or not--------false--------'+newst.isEmpty());
        
        for(Integer i: newst){
            System.debug('----------------->'+i);
        }        
    }
    
}

List usage in Apex

List Usage in APEX:

public with sharing class ListUsage {
        
        public void listUse(){
            
            // Initialising a list
            List<String> lst = new List<String>();
            
            // Adding elemenst into list
            lst.add('ABC');
            lst.add('DEF');
            lst.add('GHI');
            lst.add('JKL');
            
            System.debug('---------Elements in the list are-------------->'+lst);
            System.debug('----------Size of the Elements are-----4-------->'+lst.size());
            System.debug('-----------Element in 2nd index--------GHI-------->'+lst[2]);

            lst.remove(1);
            System.debug('---------Elements in the list after removing are-------------->'+lst);
            System.debug('----------Size of the Elements after removing are----3--------->'+lst.size());
            
            lst.add('MNO');
            lst.add('GHI');
            lst.add('DEF');
            lst.add('1248');
            
            System.debug('---------Elements in the list after adding duplicate values are-------------->'+lst);
            System.debug('----------Size of the Elements after adding duplicate values are--------7----->'+lst.size());
            
            List<String> newlst = lst.clone();
            System.debug('---------Elements in the new list are-------------->'+newlst);
            System.debug('----------Size of the Elements in new list are-------7------>'+newlst.size());
            
            lst.clear();// removes all the elements in the list
            System.debug('-----------------List is Empty or not-------true----------->'+lst.isEmpty());
            System.debug('-----------------New List is Empty or not-------false----------->'+newlst.isEmpty());
            
            //Indexing for loop 
            for(integer i=0; i<newlst.size(); i++){
                System.debug('-----Index------->'+newlst[i]);
            }
            
            //For Each Loop
            for(String s: newlst){
                System.debug('-----For Each---------->'+s);
            }
            
            
        }
        
        
        
}

Saturday, 1 June 2013

Using Hierarchy Custom Settings in Salesforce

1. Create a hierarchy custom setting like below

- As per the above screen shot create a custom field of data type check box
Note: App Setup > Develop > Custom Settings (To open custom settings)

2. Click on Manage button

- As per the above screen shot set the OWD value for isBypassed as 'False'
- Add two users by clicking on New button, set isBypassed as 'True' for one user and 'False' for another user

3. Create a page called 'Change Owner'
<apex:page tabStyle="Case" standardController="Case">
<apex:form >
<apex:sectionHeader title="Change Case Owner"/>
<p>This screen allows you to transfer cases from one user or queue to another. When you transfer ownership, the new owner will own:</p>
<ul><li>all open activities (tasks and events) for this case that are assigned to the current owner</li></ul>
<p>Note that completed activities will not be transferred. Open activities will not be transferred when assigning this case to a queue.</p>
<apex:pageBlock mode="Edit">
<apex:pageBlockButtons location="bottom">
<apex:commandButton value="Save" action="{!save}"/>
<apex:commandButton value="Cancel"/>
</apex:pageBlockButtons>
<br/><apex:pageBlockSection title="Select New Owner" collapsible="false" columns="3">
<apex:pageBlockSectionItem >
<apex:outputLabel value="Owner"></apex:outputLabel>
<apex:inputField value="{!Case.ownerId}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

- Design a visualforce page from the above code

4. Create custom button for the Case Object like below:


if ({!$Setup.Disable_Change_Owner__c.isBypassed__c}) {

window.location = "/apex/changeowner?id={!Case.Id}";

}

else {

alert("You don't have the permission");

}


- Use the above script for OnClick JavaScript
- Add the above custom button 'Change Owner' to Case Page Layout.
- "{!$Setup.Disable_Change_Owner__c.isBypassed__c}" is the way to refer the hierarchy custom setting in JavaScript.
- According to the above button one user can access the page and navigate to the 'Case Owner' page

- Another user is not able to access the page, it will throw JavaScript alert


Note: Above scenario helps to create custom change owner functionality for the case record

***
We can create multiple fields for the hierarchy custom settings and we can assign different values according to the business requirement.
***



Labels