Category Archives: Salesforce

Get IP Address of User in Apex

Apex Class:

public class IPAddressController
    public string IPAddress {get;set;}
    public IPAddressController() {
        IPAddress = ApexPages.currentPage().getHeaders().get('True-Client-IP');
        //Get IP address when no caching (sandbox, dev, secure urls)
        if (String.isBlank(IPAddress)) {
            IPAddress = ApexPages.currentPage().getHeaders().get('X-Salesforce-SIP');
        if (String.isBlank(IPAddress)) {
            IPAddress = ApexPages.currentPage().getHeaders().get('X-Forwarded-For');

Visualforce Page:

<apex:page controller="IPAddressController">
    Your Login IP Address : {!IPAddress}


Disable Copy and Paste in Visualforce Page

Visualforce Page:

<apex:page standardcontroller="Contact">
        function DisableCtrlKey(e){
            var code = (document.all) ? event.keyCode:e.which;
            if (parseInt(code)==17){
                alert("Please re-type your email address");           
        <apex:pageblock title="Create Contact">
                <apex:commandbutton value="Save" action="{!save}"/>
            <apex:pageblocksection columns="1">
                <apex:inputfield value="{!Contact.Firstname}"/>
                <apex:inputfield value="{!Contact.LastName}"/>
                <apex:inputfield value="{!Contact.Email}"/>
                <apex:inputtext value="{!Contact.Email}" label="Re-Type Email Id" onkeydown="DisableCtrlKey(event)"/>              
                <apex:inputfield value="{!Contact.Phone}"/>              


Different Ways of Making a Field Mandatory in Salesforce

There are 4 ways of making the field mandatory:

Page Layout: Field can be made mandatory from the page layout when it needs to be made mandatory for a set of users.
Field Level Security: Field can be made mandatory from the FLS when it needs to be made mandatory for all the users in the Organization and even from the API’s.
Validation Rule: Field can be made mandatory from the Validation Rule, when it needs to be made mandatory for user who is using the same Page layout used by other users.
Before Triggers: we can also make a field mandatory using before trigger

Note: recommends using the Page Layout option for making the field mandatory.