November 08, 2014

Visualforce Page to select Multi pick list values

The scenario is, when we select multiple values from left hand side it should display in the right hand side and vice versa when we click left and right arrows.

Steps to create multi pick list values

Step1: Create Visual force Page

VF Page Name: multipicklistex

<apex:page controller="multiselectCon">

    <apex:form >
        <apex:panelGrid columns="3" id="abcd"> 
      
            <apex:selectList id="sel1" value="{!leftselected}" multiselect="true" style="width:100px" size="5">
                <apex:selectOptions value="{!unselectedvalues}" />
            </apex:selectList>
           
                <apex:panelGroup >
                    <br/>
                   <apex:image value="{!$Resource.Right}">
                        <apex:actionSupport event="onclick" action="{!selectclick}" reRender="abcd"/>
                    </apex:image>
                    <br/><br/>
                    <apex:image value="{!$Resource.Left}">
                        <apex:actionSupport event="onclick" action="{!unselectclick}" reRender="abcd"/>
                    </apex:image>
                </apex:panelGroup>
                
            <apex:selectList id="sel2" value="{!rightselected}" multiselect="true" style="width:100px" size="5">
                    <apex:selectOptions value="{!SelectedValues}" />
            </apex:selectList>
          
        </apex:panelGrid>
    </apex:form>
</apex:page>

Step 2: Write Controller for VF Page

Class Name: multiselectCon

public class multiselectCon {
 Set<String>originalvalues= new Set<String>{'Test1','Test2','Test3','Test4','Test5','Test6','Test7'};
    Public List<string> leftselected{get;set;}
    Public List<string> rightselected{get;set;}
    Set<string> leftvalues = new Set<string>();
    Set<string> rightvalues = new Set<string>();
    
    public multiselectCon (){
        leftselected = new List<String>();
        rightselected = new List<String>();
        leftvalues.addAll(originalValues);
    }
    
    public PageReference selectclick(){
        rightselected.clear();
        for(String s : leftselected){
            leftvalues.remove(s);
            rightvalues.add(s);
        }
        return null;
    }
    
    public PageReference unselectclick(){
        leftselected.clear();
        for(String s : rightselected){
            rightvalues.remove(s);
            leftvalues.add(s);
        }
        return null;
    }

    public List<SelectOption> getunSelectedValues(){
        List<SelectOption> options = new List<SelectOption>();
        List<string> tempList = new List<String>();
        tempList.addAll(leftvalues);
        tempList.sort();
        for(string s : tempList)
            options.add(new SelectOption(s,s));
        return options;
    }

    public List<SelectOption> getSelectedValues(){
        List<SelectOption> options1 = new List<SelectOption>();
        List<string> tempList = new List<String>();
        tempList.addAll(rightvalues);
        tempList.sort();
        for(String s : tempList)
            options1.add(new SelectOption(s,s));
        return options1;
    }
}

Step 3: Output



Step 4: Select the values present in the left hand side and click right arrow


Step 5: Now the values will display on right hand side.



Interview Questions:
  • What are Collections present in Apex?
  • Difference between List,Set,Map?
  • What is the use of Static Resources?
  • Difference between <apex:panelgrid> and <apex:panelgroup>
  • What is <apex:actionsupport>?When we have to use?
  • How to write <apex:selectoption> syntax for picklist?

October 28, 2014

Copying Billing Address to Shipping Address Using Custom Functionality

Description:
  • Billing Address and Shipping Address are the fields present in the Address Information of Account, Contact object in Salesforce.
  • Most probably both addresses will be similar and there is a option for us “Copying Billing Address to Shipping Address” in the Account, Contact object so that by clicking that address will be copied automatically.
  • Now the scenario is creating visual force page for the Copying of Billing Address to Shipping Address without using the standard functionality.

Step 1: Create Visual force Page
https://c.ap1.visual.force.com/apex/ BillingtoShipping
VF Page Name: BillingtoShipping 
Visualforce Code:
 <apex:page controller="Billing_Shipping_Con" >
   <apex:form id="myform">
    <script type="text/javascript">
        function addressCopy(bstreet1, bcity1, bstate1, bPostalCode1, bcountry1, sstreet1, scity1, sstate1, SPostalCode1, scountry1) {
    document.getElementById(sstreet1).value = document.getElementById(bstreet1).value;
    document.getElementById(scity1).value = document.getElementById(bcity1).value;
  document.getElementById(sstate1).value = document.getElementById(bstate1).value;
document.getElementById(SPostalCode1).value=document.getElementById(bPostalCode1).value;
 document.getElementById(scountry1).value = document.getElementById(bcountry1).value;
     return false;
  }
    </script>
     <apex:pageBlock title="Billing Address to Shipping Address" id="page">
        <apex:pageBlockSection columns="2" id="pgblock">
          <apex:facet name="header">
                   <span class="pbSubExtra">
                        <span class="bodySmall">
    <a href="javascript:addressCopy('{!$Component.bstreet1}','{!$Component.bcity1}', 
                 '{!$Component.bstate1}','{!$Component.bPostalCode1}',
              '{!$Component.bcountry1}','{!$Component.sstreet1}','{!$Component.scity1}', 
       '{!$Component.sstate1}','{!$Component.SPostalCode1}','{!$Component.scountry1}')">
                      Copy Billing Address to Shipping Address</a>
                        </span> </span>
                   <h3>Address Information<span class="titleSeparatingColon">:</span></h3>   
            </apex:facet>
          <apex:inputTextarea label="Billing Street" value="{!BStreet}" id="bstreet1" />
          <apex:inputTextarea label="Shipping Street" value="{!SStreet}" id="sstreet1" />
          <apex:inputText label="Billing City" value="{!BCity}" id="bcity1"/>
          <apex:inputText label="Shipping City" value="{!SCity}" id="scity1"/>
          <apex:inputText label="Billing State" value="{!BState}" id="bstate1"/>
        <apex:inputText label="Shipping State" value="{!SState}" id="sstate1"/>
         <apex:inputText label="Billing Postal Code" value="{!BZip}" id="bPostalCode1"/>
          <apex:inputText label="Shipping Postal Code" value="{!SZip}" id="SPostalCode1"/>
          <apex:inputtext label="Billing Country" value="{!BCountry}" id="bcountry1"/>
          <apex:inputtext label="Shipping Country" value="{!SCountry}" id="scountry1"/>
        </apex:pageBlockSection>
     </apex:pageBlock>
  </apex:form>
</apex:page>

Step 2: Write a  Custom Controller for the visualforce page
Class Name: Billing_Shipping_Con
Controller:
public class Billing_Shipping_Con {
    public String BCountry { get; set; }
    public String BZip { get; set; }
    public String BState { get; set; }
    public String BCity { get; set; }
    public String BStreet { get; set; }
    public String SCountry { get; set; }
    public string SZip { get; set; }
    public String SState { get; set; }
    public String SCity { get; set; }
    public String SStreet { get; set; }
}

Step 3: Output of the Page


Step 4: Enter the data in the Billing Address (i.e. left side of the page) and click the Copy Billing Address to Shipping Address link.


Step 5: Now the data is copied to Shipping address.


 Interview Questions
  •  Difference between Standard Controller and Custom Controller?
  •  Difference between <apex: inputtext> and <apex: inputtextarea> tags?
  •  How many Controllers in Visualforce page?
  •  What is <apex:facet>?
  •  What is Javascript?



October 27, 2014

Disable Reset My Security Token option in Salesforce

Description
  • For Enterprise, Performance, Unlimited, Developer, and Database.com editions, we can set the Login IP Range addresses from which users can log in on an individual profile.
  • Users outside of the Login IP Range set on a profile can’t access your Salesforce organization.
  • We can remove the requirement for the security token based on IP address, either at the organization level or the Profile level.
  • If all of the users of this app are the same Profile, add a Login IP Range (at the Profile, not Organizational level) that includes the IP addresses they will be using. You could even add the range 0.0.0.0 through 255.255.255.255

Steps to Disable the Option:

Step 1 : Goto -> My Settings -> Personal

Here you can see Reset My Security Token option



Step 2 : Goto -> Setup -> Manage Users -> Profiles -> Login Ip Ranges



Step 3 : Enter the Start IP, End IP Address and save it



Step 4 : Check it whether the Reset Security option is disable or not.

Output: The option is disable now.



Interview Questions
  • Why Login IP Ranges is required in Salesforce?
  • What is the use of Security Token?

October 26, 2014

Deleting child records automatically when parent records are deleted using Lookup Relationship

Description

Generally we have relationships in Salesforce i.e. Master-Detail and Lookup Relationships. The main difference between these two relationships is when we delete parent record the child record will delete automatically in Master-Detail but not in Lookup. Now the scenario is, by using Lookup relationship also the records should delete as in Master-Detail. This we can achieve by writing trigger on Parent Object.

Steps for the scenario

Step 1: Create Custom Object,Fields and Records

Object Name : Parent__c



Step 2 : Create Custom Object,Fields and Records
Object Name : Child__c



Step 3 : Create Lookup Relationship between the two objects(i.e. Parent and Child) which you have created.



Step 4 : Write Trigger on Parent Object. Check the active check box



Step 5 : Delete the record in Parent object and check the child object records

Output:






Interview Questions

  • What is the difference between Master-Detail and Lookup Relationship?
  • What is the relationship between Account and Contact objects?
  • What is Trigger and its syntax ?
  • How many events present in Trigger?
  • What is the difference between Trigger.new and Trigger.old?
  • Why we are using before event in this Trigger?
  • Why we are using Trigger.old in this scenario?

October 24, 2014

Mobile Verification Code Setting for Salesforce.com Login Users

Description:

In Salesforce when we login it will be sending 6 digit Pass-code directly to your mobile or email Inbox. After Winter’14 release you can able to setup Time-Based Tokens for Identity Confirmation for any user’s in your organization.

Procedure to get mobile verification code when we login:

Step 1 : Goto  Setup->Security Controls->Session Settings

Check the Enable SMS based identity confirmation.



Step 2 : Goto Setup->Manage Users->Users

Enter the mobile number in the users profile if it is not entered and save it.



 Step 3 : After the changes have been done logout the org and login again.When you try to login now it will ask mobile verification as below.


October 23, 2014

How to uploading a file as an attachment using Visual force Page and Custom Controller

Steps to Upload a File:

Step 1 : Creating a Custom object Employee.



Step 2 : Create the custom fields which required in Employee Object.



Step 3 : Create Visual force Page on Employee Object

https://c.ap1.visual.force.com/apex/UploadFileEx

Visual force Page Name: UploadFileEx



Step 4 : Write Controller for VF Page

Controller Name : UploadFileController



Step 5 : Output



Step 6 : Enter the  Details in fields



Step 7 : Records saved in Employee Object






Step 8 : Test Class for the Apex Class(UploadFileController)




Step 9 : Run the Test Class and check code coverage.

Org Name->Developer Console->Test->New Run-->Success

Code Coverage:100%



Interview Questions:

  • What is the difference between <apex:inputtext> and <apex:inputfield> ?
  • Which tag is used to upload a file in Visual force page?
  • Which annotation is used to write test class?
  • What is the syntax to write test class?
  • What is the total code coverage should be there to deploy code from Sandbox to Production?
  • What is the minimum code coverage should be there for Test Class and Trigger to do Deployment?
  • What are the attributes present in <apex:inputtext>and <apex:inputfield>?
  • What is <apex:outputpanel> and what are its attributes ?
  • What is Constructor?

October 21, 2014

Field Sets in Salesforce

Description:
  • A field set is a grouping of fields. Field sets can be referenced onVisualforce pages dynamically.
  •  If the page is added to a managed package, administrators can add, remove, or reorder fields in a field set to modify the fields presented on the Visualforce page without modifying any code.
  • We can use dynamic bindings to display field sets on our Visualforce pages.
  • Fieldset is very useful when we are working with managed package.
  • Field sets are available for visualforce pages on API version of 21.0 or above. You can have up to 50 field sets referenced on a single page.

Example:

Step 1: Create Field set in Opportunity Object.

Setup->Customize->Opportunity->Field Sets



Step 2: Drag and Drop the fields required for the Field Set


Step 3:Create records in opportunity Object


Step 4: Create Visual Force Page

https://c.ap1.visual.force.com/apex/FieldSetExample

Visual Force Name: FieldSetExample


Step 5: Write the Controller for the VisualForce Page

Apex Class: OpportunityFieldSetController


Step 6: Output


Interview Questions:
  • Explain Field Sets in Salesforce?
  • Can Field Sets used in both Standard and Custom objects?